Skip to content

Commit 2e466a3

Browse files
authored
chore(core): discovery cache improvements (#768)
1 parent e850fb5 commit 2e466a3

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/Tempest/Cache/src/CacheConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Tempest\Cache;
66

77
use Psr\Cache\CacheItemPoolInterface;
8-
use Tempest\Core\Commands\DiscoveryGenerateCommand;
8+
use Tempest\Core\DiscoveryCache;
99
use function Tempest\env;
1010

1111
final class CacheConfig
@@ -62,7 +62,7 @@ private function resolveDiscoveryCacheStrategy(): DiscoveryCacheStrategy
6262
return $current;
6363
}
6464

65-
$original = DiscoveryCacheStrategy::make(@file_get_contents(DiscoveryGenerateCommand::CURRENT_DISCOVERY_STRATEGY));
65+
$original = DiscoveryCacheStrategy::make(@file_get_contents(DiscoveryCache::CURRENT_DISCOVERY_STRATEGY));
6666

6767
if ($current !== $original) {
6868
return DiscoveryCacheStrategy::INVALID;

src/Tempest/Core/src/Commands/DiscoveryGenerateCommand.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
{
1919
use HasConsole;
2020

21-
public const string CURRENT_DISCOVERY_STRATEGY = __DIR__ . '/../.cache/current_discovery_strategy';
22-
2321
public function __construct(
2422
private Kernel $kernel,
2523
private DiscoveryCache $discoveryCache,
@@ -45,7 +43,7 @@ public function __invoke(): void
4543

4644
$this->generateDiscoveryCache($strategy);
4745

48-
$this->storeDiscoveryCacheStrategy($strategy);
46+
$this->discoveryCache->storeStrategy($strategy);
4947
}
5048

5149
public function clearDiscoveryCache(): void
@@ -100,17 +98,6 @@ private function resolveDiscoveryCacheStrategy(): DiscoveryCacheStrategy
10098
return DiscoveryCacheStrategy::make(env('DISCOVERY_CACHE'));
10199
}
102100

103-
private function storeDiscoveryCacheStrategy(DiscoveryCacheStrategy $strategy): void
104-
{
105-
$dir = dirname(self::CURRENT_DISCOVERY_STRATEGY);
106-
107-
if (! is_dir($dir)) {
108-
mkdir($dir, recursive: true);
109-
}
110-
111-
file_put_contents(self::CURRENT_DISCOVERY_STRATEGY, $strategy->value);
112-
}
113-
114101
public function resolveKernel(): Kernel
115102
{
116103
$container = new GenericContainer();

src/Tempest/Core/src/DiscoveryCache.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
final class DiscoveryCache implements Cache
1616
{
17-
use IsCache;
17+
use IsCache {
18+
clear as parentClear;
19+
}
20+
21+
public const string CURRENT_DISCOVERY_STRATEGY = __DIR__ . '/.cache/current_discovery_strategy';
1822

1923
private CacheItemPoolInterface $pool;
2024

@@ -68,8 +72,26 @@ public function isValid(): bool
6872
return $this->cacheConfig->discoveryCache->isValid();
6973
}
7074

75+
public function clear(): void
76+
{
77+
$this->parentClear();
78+
79+
$this->storeStrategy(DiscoveryCacheStrategy::INVALID);
80+
}
81+
7182
public function getStrategy(): DiscoveryCacheStrategy
7283
{
7384
return $this->cacheConfig->discoveryCache;
7485
}
86+
87+
public function storeStrategy(DiscoveryCacheStrategy $strategy): void
88+
{
89+
$dir = dirname(self::CURRENT_DISCOVERY_STRATEGY);
90+
91+
if (! is_dir($dir)) {
92+
mkdir($dir, recursive: true);
93+
}
94+
95+
file_put_contents(self::CURRENT_DISCOVERY_STRATEGY, $strategy->value);
96+
}
7597
}

0 commit comments

Comments
 (0)