1212use Tempest \Console \Middleware \CautionMiddleware ;
1313use Tempest \Container \Container ;
1414use Tempest \Container \GenericContainer ;
15+ use Tempest \Core \DiscoveryCache ;
1516use Tempest \Support \Str ;
17+ use Tempest \View \IconCache ;
18+ use Tempest \View \ViewCache ;
1619
1720use function Tempest \Support \arr ;
1821use function Tempest \Support \str ;
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 (
0 commit comments