|
22 | 22 | use function Tempest\Support\arr; |
23 | 23 | use function Tempest\Support\str; |
24 | 24 |
|
25 | | -final readonly class CacheClearCommand |
26 | | -{ |
27 | | - use HasConsole; |
28 | | - |
29 | | - private const string DEFAULT_CACHE = 'default'; |
30 | | - |
31 | | - public function __construct( |
32 | | - private Cache $cache, |
33 | | - private Container $container, |
34 | | - ) {} |
35 | | - |
36 | | - #[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches', middleware: [ForceMiddleware::class, CautionMiddleware::class])] |
37 | | - public function __invoke( |
38 | | - #[ConsoleArgument(description: 'Name of the tagged cache to clear')] |
39 | | - ?string $tag = null, |
40 | | - #[ConsoleCommand(description: 'Whether to clear all caches')] |
41 | | - bool $all = false, |
42 | | - #[ConsoleCommand(description: 'Whether to clear internal caches')] |
43 | | - bool $internal = false, |
44 | | - ): void { |
45 | | - if (! $this->container instanceof GenericContainer) { |
46 | | - $this->console->error('Clearing caches is only available when using the default container.'); |
47 | | - return; |
48 | | - } |
49 | | - |
50 | | - if ($internal) { |
51 | | - $this->clearInternalCaches($all); |
52 | | - } else { |
53 | | - $this->clearUserCaches($tag, $all); |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - private function clearInternalCaches(bool $all = false): void |
| 25 | +if (class_exists(\Tempest\Console\ConsoleCommand::class, false)) { |
| 26 | + final readonly class CacheClearCommand |
58 | 27 | { |
59 | | - $caches = [ConfigCache::class, ViewCache::class, IconCache::class, DiscoveryCache::class]; |
60 | | - |
61 | | - if ($all === false && count($caches) > 1) { |
62 | | - $caches = $this->ask( |
63 | | - question: 'Which caches do you want to clear?', |
64 | | - options: $caches, |
65 | | - multiple: true, |
66 | | - ); |
| 28 | + use HasConsole; |
| 29 | + |
| 30 | + private const string DEFAULT_CACHE = 'default'; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private Cache $cache, |
| 34 | + private Container $container, |
| 35 | + ) {} |
| 36 | + |
| 37 | + #[ConsoleCommand(name: 'cache:clear', description: 'Clears all or specified caches', middleware: [ForceMiddleware::class, CautionMiddleware::class])] |
| 38 | + public function __invoke( |
| 39 | + #[ConsoleArgument(description: 'Name of the tagged cache to clear')] |
| 40 | + ?string $tag = null, |
| 41 | + #[ConsoleCommand(description: 'Whether to clear all caches')] |
| 42 | + bool $all = false, |
| 43 | + #[ConsoleCommand(description: 'Whether to clear internal caches')] |
| 44 | + bool $internal = false, |
| 45 | + ): void { |
| 46 | + if (! $this->container instanceof GenericContainer) { |
| 47 | + $this->console->error('Clearing caches is only available when using the default container.'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if ($internal) { |
| 52 | + $this->clearInternalCaches($all); |
| 53 | + } else { |
| 54 | + $this->clearUserCaches($tag, $all); |
| 55 | + } |
67 | 56 | } |
68 | 57 |
|
69 | | - if (count($caches) === 0) { |
70 | | - $this->console->info('No cache selected.'); |
71 | | - return; |
| 58 | + private function clearInternalCaches(bool $all = false): void |
| 59 | + { |
| 60 | + $caches = [ConfigCache::class, ViewCache::class, IconCache::class, DiscoveryCache::class]; |
| 61 | + |
| 62 | + if ($all === false && count($caches) > 1) { |
| 63 | + $caches = $this->ask( |
| 64 | + question: 'Which caches do you want to clear?', |
| 65 | + options: $caches, |
| 66 | + multiple: true, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + if (count($caches) === 0) { |
| 71 | + $this->console->info('No cache selected.'); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $this->console->header('Internal caches'); |
| 76 | + |
| 77 | + foreach ($caches as $cache) { |
| 78 | + $cache = $this->container->get($cache); |
| 79 | + $cache->clear(); |
| 80 | + |
| 81 | + $this->console->keyValue( |
| 82 | + key: $cache::class, |
| 83 | + value: "<style='bold fg-green'>CLEARED</style>", |
| 84 | + ); |
| 85 | + } |
72 | 86 | } |
73 | 87 |
|
74 | | - $this->console->header('Internal caches'); |
75 | | - |
76 | | - foreach ($caches as $cache) { |
77 | | - $cache = $this->container->get($cache); |
78 | | - $cache->clear(); |
79 | | - |
80 | | - $this->console->keyValue( |
81 | | - key: $cache::class, |
82 | | - value: "<style='bold fg-green'>CLEARED</style>", |
83 | | - ); |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - private function clearUserCaches(?string $tag = null, bool $all = false): void |
88 | | - { |
89 | | - if ($tag && $all) { |
90 | | - $this->console->error('You cannot specify both a tag and clear all caches.'); |
91 | | - return; |
92 | | - } |
93 | | - |
94 | | - /** @var GenericContainer $container */ |
95 | | - $container = $this->container; |
96 | | - $cacheTags = arr($container->getSingletons(CacheConfig::class)) |
97 | | - ->map(fn ($_, string $key) => $key === CacheConfig::class ? self::DEFAULT_CACHE : Str\after_last($key, '#')) |
98 | | - ->filter(fn ($_, string $key) => in_array($tag, [null, self::DEFAULT_CACHE], strict: true) ? true : str($key)->afterLast('#')->equals($tag)) |
99 | | - ->values(); |
100 | | - |
101 | | - if ($all === false && count($cacheTags) > 1) { |
102 | | - $cacheTags = $this->ask( |
103 | | - question: 'Which caches do you want to clear?', |
104 | | - options: $cacheTags, |
105 | | - multiple: true, |
106 | | - ); |
107 | | - } |
108 | | - |
109 | | - if (count($cacheTags) === 0) { |
110 | | - $this->console->info('No cache selected.'); |
111 | | - return; |
112 | | - } |
113 | | - |
114 | | - $this->console->header('User caches'); |
115 | | - |
116 | | - foreach ($cacheTags as $tag) { |
117 | | - $cache = $this->container->get(Cache::class, $tag === self::DEFAULT_CACHE ? null : $tag); |
118 | | - $cache->clear(); |
119 | | - |
120 | | - $this->console->keyValue( |
121 | | - key: $tag, |
122 | | - value: "<style='bold fg-green'>CLEARED</style>", |
123 | | - ); |
| 88 | + private function clearUserCaches(?string $tag = null, bool $all = false): void |
| 89 | + { |
| 90 | + if ($tag && $all) { |
| 91 | + $this->console->error('You cannot specify both a tag and clear all caches.'); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + /** @var GenericContainer $container */ |
| 96 | + $container = $this->container; |
| 97 | + $cacheTags = arr($container->getSingletons(CacheConfig::class)) |
| 98 | + ->map(fn ($_, string $key) => $key === CacheConfig::class ? self::DEFAULT_CACHE : Str\after_last($key, '#')) |
| 99 | + ->filter(fn ($_, string $key) => in_array($tag, [null, self::DEFAULT_CACHE], strict: true) ? true : str($key)->afterLast('#')->equals($tag)) |
| 100 | + ->values(); |
| 101 | + |
| 102 | + if ($all === false && count($cacheTags) > 1) { |
| 103 | + $cacheTags = $this->ask( |
| 104 | + question: 'Which caches do you want to clear?', |
| 105 | + options: $cacheTags, |
| 106 | + multiple: true, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + if (count($cacheTags) === 0) { |
| 111 | + $this->console->info('No cache selected.'); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + $this->console->header('User caches'); |
| 116 | + |
| 117 | + foreach ($cacheTags as $tag) { |
| 118 | + $cache = $this->container->get(Cache::class, $tag === self::DEFAULT_CACHE ? null : $tag); |
| 119 | + $cache->clear(); |
| 120 | + |
| 121 | + $this->console->keyValue( |
| 122 | + key: $tag, |
| 123 | + value: "<style='bold fg-green'>CLEARED</style>", |
| 124 | + ); |
| 125 | + } |
124 | 126 | } |
125 | 127 | } |
126 | 128 | } |
0 commit comments