Skip to content

Commit 04687e6

Browse files
committed
feat: handle internal caches in commands
1 parent 393047e commit 04687e6

File tree

4 files changed

+120
-17
lines changed

4 files changed

+120
-17
lines changed

packages/cache/src/Commands/CacheClearCommand.php

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
use Tempest\Console\Middleware\CautionMiddleware;
1313
use Tempest\Container\Container;
1414
use Tempest\Container\GenericContainer;
15+
use Tempest\Core\DiscoveryCache;
1516
use Tempest\Support\Str;
17+
use Tempest\View\IconCache;
18+
use Tempest\View\ViewCache;
1619

1720
use function Tempest\Support\arr;
1821
use function Tempest\Support\str;
@@ -21,6 +24,8 @@
2124
{
2225
use HasConsole;
2326

27+
private const DEFAULT_CACHE = 'default';
28+
2429
public function __construct(
2530
private Cache $cache,
2631
private Container $container,
@@ -32,21 +37,24 @@ public function __invoke(
3237
?string $tag = null,
3338
#[ConsoleCommand(description: 'Whether to clear all caches')]
3439
bool $all = false,
40+
#[ConsoleCommand(description: 'Whether to clear internal caches')]
41+
bool $internal = false,
3542
): void {
3643
if (! ($this->container instanceof GenericContainer)) {
3744
$this->console->error('Clearing caches is only available when using the default container.');
3845
return;
3946
}
4047

41-
if ($tag && $all) {
42-
$this->console->error('You cannot specify both a tag and clear all caches.');
43-
return;
48+
if ($internal) {
49+
$this->clearInternalCaches($all);
50+
} else {
51+
$this->clearUserCaches($tag, $all);
4452
}
53+
}
4554

46-
$caches = arr($this->container->getSingletons(CacheConfig::class))
47-
->map(fn ($_, string $key) => $key === CacheConfig::class ? 'default' : Str\after_last($key, '#'))
48-
->filter(fn ($_, string $key) => in_array($tag, [null, 'default'], strict: true) ? true : str($key)->afterLast('#')->equals($tag))
49-
->values();
55+
private function clearInternalCaches(bool $all = false): void
56+
{
57+
$caches = [ViewCache::class, IconCache::class, DiscoveryCache::class];
5058

5159
if ($all === false && count($caches) > 1) {
5260
$caches = $this->ask(
@@ -61,10 +69,50 @@ public function __invoke(
6169
return;
6270
}
6371

64-
$this->console->header('Clearing caches');
72+
$this->console->header('Internal caches');
73+
74+
foreach ($caches as $cache) {
75+
$cache = $this->container->get($cache);
76+
$cache->clear();
77+
78+
$this->console->keyValue(
79+
key: $cache::class,
80+
value: "<style='bold fg-green'>CLEARED</style>",
81+
);
82+
}
83+
}
84+
85+
private function clearUserCaches(?string $tag = null, bool $all = false): void
86+
{
87+
if ($tag && $all) {
88+
$this->console->error('You cannot specify both a tag and clear all caches.');
89+
return;
90+
}
91+
92+
/** @var GenericContainer $container */
93+
$container = $this->container;
94+
$cacheTags = arr($container->getSingletons(CacheConfig::class))
95+
->map(fn ($_, string $key) => $key === CacheConfig::class ? self::DEFAULT_CACHE : Str\after_last($key, '#'))
96+
->filter(fn ($_, string $key) => in_array($tag, [null, self::DEFAULT_CACHE], strict: true) ? true : str($key)->afterLast('#')->equals($tag))
97+
->values();
98+
99+
if ($all === false && count($cacheTags) > 1) {
100+
$cacheTags = $this->ask(
101+
question: 'Which caches do you want to clear?',
102+
options: $cacheTags,
103+
multiple: true,
104+
);
105+
}
106+
107+
if (count($cacheTags) === 0) {
108+
$this->console->info('No cache selected.');
109+
return;
110+
}
111+
112+
$this->console->header('User caches');
65113

66-
foreach ($caches as $tag) {
67-
$cache = $this->container->get(Cache::class, $tag === 'default' ? null : $tag);
114+
foreach ($cacheTags as $tag) {
115+
$cache = $this->container->get(Cache::class, $tag === self::DEFAULT_CACHE ? null : $tag);
68116
$cache->clear();
69117

70118
$this->console->keyValue(

packages/cache/src/Commands/CacheStatusCommand.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
use Tempest\Console\HasConsole;
1414
use Tempest\Container\Container;
1515
use Tempest\Container\GenericContainer;
16+
use Tempest\Core\DiscoveryCache;
1617
use Tempest\Support\Str;
18+
use Tempest\View\IconCache;
19+
use Tempest\View\ViewCache;
1720
use UnitEnum;
1821

1922
use function Tempest\Support\arr;
@@ -28,7 +31,7 @@ public function __construct(
2831
) {}
2932

3033
#[ConsoleCommand(name: 'cache:status', description: 'Shows which caches are enabled')]
31-
public function __invoke(): void
34+
public function __invoke(bool $internal = true): void
3235
{
3336
if (! ($this->container instanceof GenericContainer)) {
3437
$this->console->error('Clearing caches is only available when using the default container.');
@@ -39,8 +42,24 @@ public function __invoke(): void
3942
->map(fn ($_, string $key) => $this->container->get(Cache::class, $key === CacheConfig::class ? null : Str\after_last($key, '#')))
4043
->values();
4144

42-
$this->console->header('Cache status');
43-
$this->console->keyValue('Total caches', (string) count($caches));
45+
if ($internal) {
46+
$this->console->header('Internal caches');
47+
48+
foreach ([ViewCache::class, IconCache::class, DiscoveryCache::class] as $cacheName) {
49+
/** @var Cache $cache */
50+
$cache = $this->container->get($cacheName);
51+
52+
$this->console->keyValue(
53+
key: $cacheName,
54+
value: match ($cache->enabled) {
55+
true => '<style="bold fg-green">ENABLED</style>',
56+
false => '<style="bold fg-red">DISABLED</style>',
57+
},
58+
);
59+
}
60+
}
61+
62+
$this->console->header('User caches');
4463

4564
/** @var Cache $cache */
4665
foreach ($caches as $cache) {

tests/Integration/Cache/CacheClearCommandTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Tempest\Cache\Commands\CacheClearCommand;
88
use Tempest\Cache\Config\InMemoryCacheConfig;
9-
use Tempest\Cache\ProjectCache;
9+
use Tempest\Core\DiscoveryCache;
10+
use Tempest\View\IconCache;
11+
use Tempest\View\ViewCache;
1012
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1113

1214
/**
@@ -90,4 +92,24 @@ public function test_cache_clear_filter(): void
9092
->assertSee('my-cache')
9193
->assertSeeCount('CLEARED', expectedCount: 2);
9294
}
95+
96+
public function test_clear_internal_caches(): void
97+
{
98+
$this->console
99+
->call(CacheClearCommand::class, ['all' => true, 'internal' => true])
100+
->assertSee(ViewCache::class)
101+
->assertSee(IconCache::class)
102+
->assertSee(DiscoveryCache::class)
103+
->assertSeeCount('CLEARED', expectedCount: 3);
104+
}
105+
106+
public function test_clear_internal_cache(): void
107+
{
108+
$this->console
109+
->call(CacheClearCommand::class, ['internal' => true])
110+
->submit('0')
111+
->submit('yes')
112+
->assertSee(ViewCache::class)
113+
->assertSeeCount('CLEARED', expectedCount: 1);
114+
}
93115
}

tests/Integration/Cache/CacheStatusCommandTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Tempest\Cache\Cache;
66
use Tempest\Cache\Commands\CacheStatusCommand;
77
use Tempest\Cache\Config\InMemoryCacheConfig;
8+
use Tempest\Core\DiscoveryCache;
9+
use Tempest\View\IconCache;
10+
use Tempest\View\ViewCache;
811
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
912

1013
final class CacheStatusCommandTest extends FrameworkIntegrationTestCase
@@ -18,7 +21,7 @@ protected function setUp(): void
1821
public function test_cache_status(): void
1922
{
2023
$this->console
21-
->call(CacheStatusCommand::class)
24+
->call(CacheStatusCommand::class, ['internal' => false])
2225
->assertSeeCount('ENABLED', expectedCount: 1);
2326
}
2427

@@ -28,7 +31,7 @@ public function test_cache_status_when_disabled(): void
2831
$cache->enabled = false;
2932

3033
$this->console
31-
->call(CacheStatusCommand::class)
34+
->call(CacheStatusCommand::class, ['internal' => false])
3235
->assertSeeCount('DISABLED', expectedCount: 1);
3336
}
3437

@@ -37,8 +40,19 @@ public function test_cache_status_with_multiple_caches(): void
3740
$this->container->config(new InMemoryCacheConfig(tag: 'test-cache'));
3841

3942
$this->console
40-
->call(CacheStatusCommand::class)
43+
->call(CacheStatusCommand::class, ['internal' => false])
4144
->assertSee('test-cache')
4245
->assertSeeCount('ENABLED', expectedCount: 2);
4346
}
47+
48+
public function test_with_internal_caches(): void
49+
{
50+
$this->console
51+
->call(CacheStatusCommand::class, ['internal' => true])
52+
->assertSee(ViewCache::class)
53+
->assertSee(IconCache::class)
54+
->assertSee(DiscoveryCache::class)
55+
->assertSeeCount('DISABLED', expectedCount: 1)
56+
->assertSeeCount('ENABLED', expectedCount: 3);
57+
}
4458
}

0 commit comments

Comments
 (0)