Skip to content

Commit e3856da

Browse files
committed
wip
1 parent ed91080 commit e3856da

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

docs/1-essentials/02-views.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,15 +673,31 @@ You can choose whichever way you prefer. Chances are that, if you use the minima
673673

674674
### A note on caching
675675

676-
When you're using the minimal setup, view caching can be enabled by passing in a `$cache` paremeter into `TempestViewRenderer::make()`:
676+
When you're using the minimal setup, view caching can be enabled by passing in a `$viewCache` paremeter into `TempestViewRenderer::make()`:
677677

678678
```php
679+
use Tempest\View\Renderers\TempestViewRenderer;
680+
use Tempest\View\ViewCache;
681+
679682
$renderer = TempestViewRenderer::make(
680-
cache: true,
683+
cache: ViewCache::enabled(),
681684
);
682685
```
683686

684-
It's recommended to turn view caching on in production environments. To clear the view cache, you'll have to manually delete the cache directory, which is located at `./vendor/tempest/view/.tempest/cache`.
687+
It's recommended to turn view caching on in production environments. To clear the view cache, you can call the `clear()` method on the `ViewCache` object:
688+
689+
```php
690+
use Tempest\View\Renderers\TempestViewRenderer;
691+
use Tempest\View\ViewCache;
692+
693+
$viewCache = ViewCache::enabled();
694+
695+
$viewCache->clear();
696+
697+
$renderer = TempestViewRenderer::make(
698+
cache: $viewCache,
699+
);
700+
```
685701

686702
## Separate view directories
687703

packages/view/src/Renderers/TempestViewRenderer.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535

3636
public static function make(
3737
?ViewConfig $viewConfig = null,
38-
bool $cache = false,
38+
?ViewCache $viewCache = null,
3939
Environment $environment = Environment::PRODUCTION,
4040
): self {
4141
$viewConfig ??= new ViewConfig();
@@ -52,10 +52,7 @@ public static function make(
5252

5353
$elementFactory->setViewCompiler($compiler);
5454

55-
$viewCache = new ViewCache(
56-
enabled: $cache,
57-
pool: new ViewCachePool(__DIR__ . '/../../.tempest/cache'),
58-
);
55+
$viewCache = $viewCache ?? ViewCache::disabled();
5956

6057
return new self(
6158
compiler: $compiler,

packages/view/src/ViewCache.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ public function __construct(
2020
);
2121
}
2222

23+
public static function enabled(?string $path = null): self
24+
{
25+
return new self(
26+
enabled: true,
27+
pool: new ViewCachePool($path ?? __DIR__ . '/../.tempest/cache')
28+
);
29+
}
30+
31+
public static function disabled(?string $path = null): self
32+
{
33+
return new self(
34+
enabled: false,
35+
pool: new ViewCachePool($path ?? __DIR__ . '/../.tempest/cache')
36+
);
37+
}
38+
2339
public function clear(): void
2440
{
2541
$this->pool->clear();

packages/view/tests/StandaloneViewRendererTest.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Tempest\View\Tests;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
67
use Tempest\View\Exceptions\ViewComponentPathWasInvalid;
78
use Tempest\View\Exceptions\ViewComponentPathWasNotFound;
89
use Tempest\View\Renderers\TempestViewRenderer;
10+
use Tempest\View\ViewCache;
911
use Tempest\View\ViewComponent;
1012
use Tempest\View\ViewConfig;
1113

@@ -19,10 +21,9 @@ public function test_render(): void
1921
__DIR__ . '/Fixtures/x-standalone-base.view.php',
2022
);
2123

22-
$renderer =
23-
TempestViewRenderer::make(
24-
viewConfig: $viewConfig,
25-
);
24+
$renderer = TempestViewRenderer::make(
25+
viewConfig: $viewConfig,
26+
);
2627

2728
$html = $renderer->render(
2829
view(__DIR__ . '/Fixtures/standalone.view.php'),
@@ -67,6 +68,43 @@ public function test_invalid_view_component_paths_within_config(): void
6768
}
6869
}
6970

71+
public function test_with_cache_enabled(): void
72+
{
73+
$viewCache = ViewCache::enabled();
74+
$viewCache->clear();
75+
76+
$renderer = TempestViewRenderer::make(
77+
viewCache: $viewCache,
78+
);
79+
80+
$html = $renderer->render(
81+
view(__DIR__ . '/Fixtures/standalone.view.php'),
82+
);
83+
84+
$this->assertSnippetsMatch(<<<'HTML'
85+
<x-standalone-base>
86+
Hi
87+
</x-standalone-base>
88+
HTML, $html);
89+
}
90+
91+
public function test_with_cache_disabled(): void
92+
{
93+
$renderer = TempestViewRenderer::make(
94+
viewCache: ViewCache::disabled(),
95+
);
96+
97+
$html = $renderer->render(
98+
view(__DIR__ . '/Fixtures/standalone.view.php'),
99+
);
100+
101+
$this->assertSnippetsMatch(<<<'HTML'
102+
<x-standalone-base>
103+
Hi
104+
</x-standalone-base>
105+
HTML, $html);
106+
}
107+
70108
protected function assertSnippetsMatch(string $expected, string $actual): void
71109
{
72110
$expected = str_replace([PHP_EOL, ' '], '', $expected);

0 commit comments

Comments
 (0)