Skip to content

Commit d80bfa9

Browse files
innocenzibrendt
andauthored
refactor(framework): centralize internal caches (#948)
Co-authored-by: brendt <[email protected]>
1 parent bbfb4ee commit d80bfa9

28 files changed

+147
-67
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
"Tempest\\Router\\Tests\\": "src/Tempest/Router/tests",
160160
"Tempest\\Support\\Tests\\": "src/Tempest/Support/tests",
161161
"Tempest\\Validation\\Tests\\": "src/Tempest/Validation/tests",
162+
"Tempest\\View\\Tests\\": "src/Tempest/View/tests",
162163
"Tempest\\Vite\\Tests\\": "src/Tempest/Vite/tests",
163164
"Tests\\Tempest\\": "tests/"
164165
}

src/Tempest/Cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"psr/cache": "^3.0",
77
"symfony/cache": "^7.2",
88
"tempest/core": "dev-main",
9+
"tempest/container": "dev-main",
910
"tempest/highlight": "^2.11.2"
1011
},
1112
"require-dev": {

src/Tempest/Cache/src/CacheConfig.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Cache\CacheItemPoolInterface;
88
use Tempest\Core\DiscoveryCache;
99
use function Tempest\env;
10+
use function Tempest\internal_storage_path;
1011

1112
final class CacheConfig
1213
{
@@ -22,16 +23,20 @@ final class CacheConfig
2223
public DiscoveryCacheStrategy $discoveryCache;
2324

2425
public function __construct(
25-
public string $directory = __DIR__ . '/../../../../.cache',
26+
/**
27+
* Path to the storage directory, relative to the internal storage.
28+
*/
29+
public string $directory = 'cache',
2630
public ?CacheItemPoolInterface $projectCachePool = null,
2731

2832
/** Used as a global override, should be true in production, null in local */
2933
?bool $enable = null,
3034
) {
31-
$this->enable = $enable ?? env('CACHE') ?? null;
35+
$this->enable = $enable ?? env('CACHE');
3236
$this->projectCache = (bool) env('PROJECT_CACHE', false);
3337
$this->viewCache = (bool) env('VIEW_CACHE', false);
3438
$this->discoveryCache = $this->resolveDiscoveryCacheStrategy();
39+
$this->directory = internal_storage_path($directory);
3540
}
3641

3742
/** @param class-string<\Tempest\Cache\Cache> $className */
@@ -62,7 +67,7 @@ private function resolveDiscoveryCacheStrategy(): DiscoveryCacheStrategy
6267
return $current;
6368
}
6469

65-
$original = DiscoveryCacheStrategy::make(@file_get_contents(DiscoveryCache::CURRENT_DISCOVERY_STRATEGY));
70+
$original = DiscoveryCacheStrategy::make(@file_get_contents(DiscoveryCache::getCurrentDiscoverStrategyCachePath()));
6671

6772
if ($current !== $original) {
6873
return DiscoveryCacheStrategy::INVALID;

src/Tempest/Cache/src/ProjectCache.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Psr\Cache\CacheItemPoolInterface;
88
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9-
use function Tempest\Support\path;
109

1110
final class ProjectCache implements Cache
1211
{
@@ -18,7 +17,7 @@ public function __construct(
1817
private readonly CacheConfig $cacheConfig,
1918
) {
2019
$this->pool = $this->cacheConfig->projectCachePool ?? new FilesystemAdapter(
21-
directory: path($this->cacheConfig->directory, 'project')->toString(),
20+
directory: $this->cacheConfig->directory . '/project',
2221
);
2322
}
2423

src/Tempest/Cache/tests/.gitkeep

Whitespace-only changes.

src/Tempest/Console/src/Scheduler/GenericScheduler.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99
use Tempest\Console\Scheduler;
1010
use Tempest\Core\ShellExecutor;
1111
use function Tempest\event;
12+
use function Tempest\internal_storage_path;
1213

1314
final readonly class GenericScheduler implements Scheduler
1415
{
15-
public const string CACHE_PATH = __DIR__ . '/../../../../.cache/tempest/last-schedule-run.cache.php';
16-
1716
public function __construct(
1817
private SchedulerConfig $config,
1918
private ConsoleArgumentBag $argumentBag,
2019
private ShellExecutor $executor,
2120
) {
2221
}
2322

23+
public static function getCachePath(): string
24+
{
25+
return internal_storage_path('scheduler', 'last-schedule-run.cache.php');
26+
}
27+
2428
public function run(?DateTime $date = null): void
2529
{
2630
$date ??= new DateTime();
@@ -80,11 +84,11 @@ private function getInvocationsToRun(DateTime $date): array
8084
*/
8185
private function getPreviousRuns(): array
8286
{
83-
if (! file_exists(self::CACHE_PATH)) {
87+
if (! file_exists(self::getCachePath())) {
8488
return [];
8589
}
8690

87-
return unserialize(file_get_contents(self::CACHE_PATH), ['allowed_classes' => false]);
91+
return unserialize(file_get_contents(self::getCachePath()), ['allowed_classes' => false]);
8892
}
8993

9094
/** @param ScheduledInvocation[] $ranInvocations */
@@ -96,12 +100,12 @@ private function markInvocationsAsRun(array $ranInvocations, DateTime $ranAt): v
96100
$lastRuns[$invocation->handler->getName()] = $ranAt->getTimestamp();
97101
}
98102

99-
$directory = dirname(self::CACHE_PATH);
103+
$directory = dirname(self::getCachePath());
100104

101105
if (! is_dir($directory)) {
102106
mkdir(directory: $directory, recursive: true);
103107
}
104108

105-
file_put_contents(self::CACHE_PATH, serialize($lastRuns));
109+
file_put_contents(self::getCachePath(), serialize($lastRuns));
106110
}
107111
}

src/Tempest/Console/src/Stubs/cache.config.stub.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
65
use Tempest\Cache\CacheConfig;
76

8-
return new CacheConfig(
9-
projectCachePool: new FilesystemAdapter(
10-
directory: __DIR__ . '/../../../../.cache',
11-
),
12-
);
7+
return new CacheConfig();

src/Tempest/Core/src/ConfigCache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\Cache\Cache;
1010
use Tempest\Cache\IsCache;
1111
use function Tempest\env;
12+
use function Tempest\internal_storage_path;
1213

1314
final readonly class ConfigCache implements Cache
1415
{
@@ -19,7 +20,7 @@
1920
public function __construct()
2021
{
2122
$this->pool = new FilesystemAdapter(
22-
directory: __DIR__ . '/.cache',
23+
directory: internal_storage_path('config'),
2324
);
2425
}
2526

@@ -30,6 +31,6 @@ protected function getCachePool(): CacheItemPoolInterface
3031

3132
public function isEnabled(): bool
3233
{
33-
return env('CACHE') ?? env('CONFIG_CACHE', false);
34+
return (bool) (env('CACHE') ?? env('CONFIG_CACHE', false));
3435
}
3536
}

src/Tempest/Core/src/DiscoveryCache.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@
1212
use Tempest\Cache\IsCache;
1313
use Tempest\Discovery\Discovery;
1414
use Tempest\Discovery\DiscoveryItems;
15-
use function Tempest\Support\path;
15+
use Throwable;
16+
use function Tempest\internal_storage_path;
1617

1718
final class DiscoveryCache implements Cache
1819
{
1920
use IsCache {
2021
clear as parentClear;
2122
}
2223

23-
public const string CURRENT_DISCOVERY_STRATEGY = __DIR__ . '/.cache/current_discovery_strategy';
24-
2524
private CacheItemPoolInterface $pool;
2625

2726
public function __construct(
2827
private readonly CacheConfig $cacheConfig,
2928
?CacheItemPoolInterface $pool = null,
3029
) {
3130
$this->pool = $pool ?? new PhpFilesAdapter(
32-
directory: path($this->cacheConfig->directory, 'discovery')->toString(),
31+
directory: $this->cacheConfig->directory . '/discovery',
3332
);
3433
}
3534

@@ -88,12 +87,21 @@ public function getStrategy(): DiscoveryCacheStrategy
8887

8988
public function storeStrategy(DiscoveryCacheStrategy $strategy): void
9089
{
91-
$dir = dirname(self::CURRENT_DISCOVERY_STRATEGY);
90+
$dir = dirname(self::getCurrentDiscoverStrategyCachePath());
9291

9392
if (! is_dir($dir)) {
9493
mkdir($dir, recursive: true);
9594
}
9695

97-
file_put_contents(self::CURRENT_DISCOVERY_STRATEGY, $strategy->value);
96+
file_put_contents(self::getCurrentDiscoverStrategyCachePath(), $strategy->value);
97+
}
98+
99+
public static function getCurrentDiscoverStrategyCachePath(): string
100+
{
101+
try {
102+
return internal_storage_path('current_discovery_strategy');
103+
} catch (Throwable) {
104+
return __DIR__ . '/current_discovery_strategy';
105+
}
98106
}
99107
}

src/Tempest/Core/src/FrameworkKernel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ final class FrameworkKernel implements Kernel
2424

2525
public array $discoveryClasses = [];
2626

27+
public string $internalStorage;
28+
2729
public function __construct(
2830
public string $root,
2931
/** @var \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
@@ -50,6 +52,7 @@ public static function boot(
5052
->loadEnv()
5153
->registerKernelErrorHandler()
5254
->registerShutdownFunction()
55+
->registerInternalStorage()
5356
->registerKernel()
5457
->loadComposer()
5558
->loadDiscoveryLocations()
@@ -166,6 +169,19 @@ public function registerErrorHandler(): self
166169
return $this;
167170
}
168171

172+
public function registerInternalStorage(): self
173+
{
174+
$path = $this->root . '/vendor/.tempest';
175+
176+
if (! is_dir($path)) {
177+
mkdir($path, recursive: true);
178+
}
179+
180+
$this->internalStorage = realpath($path);
181+
182+
return $this;
183+
}
184+
169185
public function finishDeferredTasks(): self
170186
{
171187
$this->container->invoke(FinishDeferredTasks::class);

0 commit comments

Comments
 (0)