Skip to content

Commit 1e33722

Browse files
authored
feat(view): cache Blade and Twig templates in internal storage (#1061)
1 parent 48e7894 commit 1e33722

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

src/Tempest/View/src/Renderers/BladeInitializer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Tempest\Container\Singleton;
1111
use Tempest\Reflection\ClassReflector;
1212

13+
use function Tempest\internal_storage_path;
14+
1315
final readonly class BladeInitializer implements DynamicInitializer
1416
{
1517
public function canInitialize(ClassReflector $class): bool
@@ -28,7 +30,7 @@ public function initialize(ClassReflector $class, Container $container): object
2830

2931
return new Blade(
3032
viewPaths: $bladeConfig->viewPaths,
31-
cachePath: $bladeConfig->cachePath,
33+
cachePath: internal_storage_path($bladeConfig->cachePath ?? 'cache/blade'),
3234
);
3335
}
3436
}

src/Tempest/View/src/Renderers/TwigConfig.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
namespace Tempest\View\Renderers;
66

7+
use function Tempest\internal_storage_path;
8+
79
final readonly class TwigConfig
810
{
911
/**
1012
* @see \Twig\Environment::__construct()
1113
*/
1214
public function __construct(
1315
public array $viewPaths = [],
14-
public ?string $cachePath = null,
16+
public null|false|string $cachePath = null,
1517
public bool $debug = false,
1618
public string $charset = 'utf-8',
1719
public bool $strictVariables = false,
@@ -28,7 +30,7 @@ public function toArray(): array
2830
'charset' => $this->charset,
2931
'strict_variables' => $this->strictVariables,
3032
'autoescape' => $this->autoescape,
31-
'cache' => $this->cachePath ?: false,
33+
'cache' => $this->cachePath === false ? false : internal_storage_path($this->cachePath ?? 'cache/twig'),
3234
'auto_reload' => $this->autoReload,
3335
'optimizations' => $this->optimizations,
3436
];

tests/Integration/View/BladeViewRendererTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\View\ViewConfig;
1010
use Tempest\View\ViewRenderer;
1111
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
12+
1213
use function Tempest\view;
1314

1415
/**
@@ -24,18 +25,17 @@ public function test_blade(): void
2425

2526
$this->container->config(new BladeConfig(
2627
viewPaths: [__DIR__ . '/blade'],
27-
cachePath: __DIR__ . '/../../../../.cache/tempest/blade/cache',
28+
cachePath: 'blade-cache',
2829
));
2930

3031
$renderer = $this->container->get(ViewRenderer::class);
3132

3233
$html = $renderer->render(view('index'));
3334

3435
$this->assertSame(<<<HTML
35-
<html>
36-
Hi
37-
</html>
38-
HTML
39-
, $html);
36+
<html>
37+
Hi
38+
</html>
39+
HTML, $html);
4040
}
4141
}

tests/Integration/View/TwigViewRendererTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\View\ViewConfig;
1010
use Tempest\View\ViewRenderer;
1111
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
12+
1213
use function Tempest\view;
1314

1415
/**
@@ -24,18 +25,17 @@ public function test_twig(): void
2425

2526
$this->container->config(new TwigConfig(
2627
viewPaths: [__DIR__ . '/twig'],
27-
cachePath: __DIR__ . '/../../../.cache/tempest/twig/cache',
28+
cachePath: 'twig-cache',
2829
));
2930

3031
$renderer = $this->container->get(ViewRenderer::class);
3132

3233
$html = $renderer->render(view('index.twig', ...['foo' => 'bar']));
3334

3435
$this->assertStringEqualsStringIgnoringLineEndings(<<<HTML
35-
<html>
36-
<span>bar</span>
37-
</html>
38-
HTML
39-
, $html);
36+
<html>
37+
<span>bar</span>
38+
</html>
39+
HTML, $html);
4040
}
4141
}

0 commit comments

Comments
 (0)