Skip to content

Commit 2bf762d

Browse files
committed
refactor(view)!: use internal_storage_path to build view cache
Closes #1694
1 parent 40cb976 commit 2bf762d

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

docs/1-essentials/02-views.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ keywords: "Experimental"
88

99
Views in Tempest are parsed by Tempest View, our own templating engine. Tempest View uses a syntax that can be thought of as a superset of HTML. If you prefer using a templating engine with more widespread support, [you may also use Blade, Twig, or any other](#using-other-engines) — as long as you provide a way to initialize it.
1010

11-
If you'd like to Tempest View as a standalone component in your project, you can read the documentation on how to do so [here](../5-extra-topics/02-standalone-components.md#tempest-view).
11+
If you'd like to Tempest View as a standalone component in your project, you can read the documentation on how to do so [here](../5-extra-topics/02-standalone-components.md#tempest-view).
1212

1313
### Syntax overview
1414

@@ -447,7 +447,7 @@ $title = 'foo';
447447
<!-- $title will need to be passed in explicitly,
448448
otherwise `x-post` wouldn't know about it: -->
449449

450-
<x-post :title="$title"></x-post>
450+
<x-post :title="$title"></x-post>
451451
```
452452

453453
```php
@@ -463,7 +463,6 @@ final class HomeController
463463
```
464464

465465
```html x-base.view.php
466-
467466
<h1>{{ $siteTitle }}</h1>
468467
```
469468

@@ -680,7 +679,7 @@ use Tempest\View\Renderers\TempestViewRenderer;
680679
use Tempest\View\ViewCache;
681680

682681
$renderer = TempestViewRenderer::make(
683-
cache: ViewCache::enabled(),
682+
cache: ViewCache::create(),
684683
);
685684
```
686685

@@ -690,8 +689,7 @@ It's recommended to turn view caching on in production environments. To clear th
690689
use Tempest\View\Renderers\TempestViewRenderer;
691690
use Tempest\View\ViewCache;
692691

693-
$viewCache = ViewCache::enabled();
694-
692+
$viewCache = ViewCache::create();
695693
$viewCache->clear();
696694

697695
$renderer = TempestViewRenderer::make(

packages/view/src/Renderers/TempestViewRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function make(
5151

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

54-
$viewCache ??= ViewCache::disabled();
54+
$viewCache ??= ViewCache::create(enabled: false);
5555

5656
return new self(
5757
compiler: $compiler,

packages/view/src/ViewCache.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\View;
66

77
use Closure;
8+
use Throwable;
89

910
use function Tempest\internal_storage_path;
1011
use function Tempest\Support\path;
@@ -16,23 +17,15 @@ public function __construct(
1617
private ?ViewCachePool $pool = null,
1718
) {
1819
$this->pool ??= new ViewCachePool(
19-
directory: internal_storage_path('cache/views'),
20+
directory: self::getCachePath(),
2021
);
2122
}
2223

23-
public static function enabled(?string $path = null): self
24+
public static function create(bool $enabled = true, ?string $path = null): self
2425
{
2526
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'),
27+
enabled: $enabled,
28+
pool: new ViewCachePool($path ?? self::getCachePath()),
3629
);
3730
}
3831

@@ -55,4 +48,13 @@ public function getCachedViewPath(string $path, Closure $compiledView): string
5548

5649
return path($this->pool->directory, $cacheItem->getKey() . '.php')->toString();
5750
}
51+
52+
private static function getCachePath(): string
53+
{
54+
try {
55+
return internal_storage_path('cache/views');
56+
} catch (Throwable) {
57+
return __DIR__ . '/../.tempest/cache';
58+
}
59+
}
5860
}

packages/view/tests/StandaloneViewRendererTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function test_invalid_view_component_paths_within_config(): void
7171

7272
public function test_with_cache_enabled(): void
7373
{
74-
$viewCache = ViewCache::enabled();
74+
$viewCache = ViewCache::create();
7575
$viewCache->clear();
7676

7777
$renderer =
@@ -93,7 +93,7 @@ public function test_with_cache_enabled(): void
9393
public function test_with_cache_disabled(): void
9494
{
9595
$renderer = TempestViewRenderer::make(
96-
viewCache: ViewCache::disabled(),
96+
viewCache: ViewCache::create(enabled: false),
9797
);
9898

9999
$html = $renderer->render(

0 commit comments

Comments
 (0)