File tree Expand file tree Collapse file tree 7 files changed +179
-4
lines changed Expand file tree Collapse file tree 7 files changed +179
-4
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tempest \Cache ;
6+
7+ use Tempest \Console \Console ;
8+ use Tempest \Console \ConsoleCommand ;
9+ use Tempest \Console \HasConsole ;
10+ use Tempest \Container \Container ;
11+
12+ final readonly class CacheClearCommand
13+ {
14+ use HasConsole;
15+
16+ public function __construct (
17+ private Console $ console ,
18+ private CacheConfig $ cacheConfig ,
19+ private Container $ container ,
20+ ) {
21+ }
22+
23+ #[ConsoleCommand(name: 'cache:clear ' , aliases: ['cc ' ])]
24+ public function __invoke (bool $ all = false ): void
25+ {
26+ $ caches = $ this ->cacheConfig ->caches ;
27+
28+ if ($ all === false ) {
29+ $ caches = $ this ->ask (
30+ question: "Which caches do you want to clear? " ,
31+ options: $ this ->cacheConfig ->caches ,
32+ multiple: true ,
33+ );
34+ }
35+
36+ foreach ($ caches as $ cacheClass ) {
37+ /** @var Cache $cache */
38+ $ cache = $ this ->container ->get ($ cacheClass );
39+
40+ $ cache ->clear ();
41+
42+ $ this ->writeln ("<em> {$ cacheClass }</em> cleared successfully " );
43+ }
44+
45+ $ this ->success ('Done ' );
46+ }
47+ }
Original file line number Diff line number Diff line change 77use Psr \Cache \CacheItemPoolInterface ;
88use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
99
10- final readonly class CacheConfig
10+ final class CacheConfig
1111{
12+ /** @var class-string<\Tempest\Cache\Cache>[] */
13+ public array $ caches = [];
14+
1215 public function __construct (
1316 public CacheItemPoolInterface $ pool = new FilesystemAdapter (
1417 namespace: '' ,
@@ -17,4 +20,10 @@ public function __construct(
1720 ),
1821 ) {
1922 }
23+
24+ /** @param class-string<\Tempest\Cache\Cache> $className */
25+ public function addCache (string $ className ): void
26+ {
27+ $ this ->caches [] = $ className ;
28+ }
2029}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tempest \Cache ;
6+
7+ use Tempest \Container \Container ;
8+ use Tempest \Core \Discovery ;
9+ use Tempest \Core \HandlesDiscoveryCache ;
10+ use Tempest \Reflection \ClassReflector ;
11+
12+ final readonly class CacheDiscovery implements Discovery
13+ {
14+ use HandlesDiscoveryCache;
15+
16+ public function __construct (
17+ private CacheConfig $ cacheConfig ,
18+ ) {
19+ }
20+
21+ public function discover (ClassReflector $ class ): void
22+ {
23+ if ($ class ->implements (Cache::class)) {
24+ $ this ->cacheConfig ->addCache ($ class ->getName ());
25+ }
26+ }
27+
28+ public function createCachePayload (): string
29+ {
30+ return serialize ($ this ->cacheConfig ->caches );
31+ }
32+
33+ public function restoreCachePayload (Container $ container , string $ payload ): void
34+ {
35+ $ this ->cacheConfig ->caches = unserialize ($ payload );
36+ }
37+ }
Original file line number Diff line number Diff line change 1111final readonly class CacheInitializer implements Initializer
1212{
1313 #[Singleton]
14- public function initialize (Container $ container ): Cache
14+ public function initialize (Container $ container ): Cache | GenericCache
1515 {
1616 return new GenericCache ($ container ->get (CacheConfig::class)->pool );
1717 }
Original file line number Diff line number Diff line change @@ -34,10 +34,10 @@ public function __invoke(): void
3434
3535 $ this ->console ->writeln (implode ('' , [
3636 "<em> {$ discoveryClass }</em> " ,
37- ' cleared successful ' ,
37+ ' cleared successfully ' ,
3838 ]));
3939 }
4040
41- $ this ->console ->writeln ('Done ' );
41+ $ this ->console ->success ('Done ' );
4242 }
4343}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tests \Tempest \Fixtures \Cache ;
6+
7+ use Psr \Cache \CacheItemPoolInterface ;
8+ use Symfony \Component \Cache \Adapter \NullAdapter ;
9+ use Tempest \Cache \Cache ;
10+ use Tempest \Cache \IsCache ;
11+
12+ final class DummyCache implements Cache
13+ {
14+ use IsCache;
15+
16+ private CacheItemPoolInterface $ pool ;
17+
18+ public bool $ cleared = false ;
19+
20+ public function __construct ()
21+ {
22+ $ this ->pool = new NullAdapter ();
23+ }
24+
25+ protected function getCachePool (): CacheItemPoolInterface
26+ {
27+ return $ this ->pool ;
28+ }
29+
30+ public function clear (): void
31+ {
32+ $ this ->cleared = true ;
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Tests \Tempest \Integration \Cache ;
6+
7+ use Tempest \Cache \GenericCache ;
8+ use Tests \Tempest \Fixtures \Cache \DummyCache ;
9+ use Tests \Tempest \Integration \FrameworkIntegrationTestCase ;
10+
11+ /**
12+ * @internal
13+ */
14+ final class CacheClearCommandTest extends FrameworkIntegrationTestCase
15+ {
16+ public function test_cache_clear (): void
17+ {
18+ $ this ->console
19+ ->call ('cache:clear ' )
20+ ->assertSee (GenericCache::class)
21+ ->assertSee (DummyCache::class)
22+ ->submit ('0,1 ' )
23+ ->submit ('yes ' )
24+ ->assertSee ('Tests\Tempest\Fixtures\Cache\DummyCache cleared successfully ' )
25+ ->assertSee ('Tempest\Cache\GenericCache cleared successfully ' )
26+ ->assertSee ('Done ' );
27+ }
28+
29+ public function test_cache_clear_one_option (): void
30+ {
31+ $ this ->console
32+ ->call ('cache:clear ' )
33+ ->submit ('0 ' )
34+ ->submit ('yes ' )
35+ ->assertSee ('Tests\Tempest\Fixtures\Cache\DummyCache cleared successfully ' )
36+ ->assertNotSee ('Tempest\Cache\GenericCache cleared successfully ' )
37+ ->assertSee ('Done ' );
38+ }
39+
40+ public function test_cache_clear_all (): void
41+ {
42+ $ this ->console
43+ ->call ('cache:clear --all ' )
44+ ->assertSee ('Tests\Tempest\Fixtures\Cache\DummyCache cleared successfully ' )
45+ ->assertSee ('Tempest\Cache\GenericCache cleared successfully ' )
46+ ->assertSee ('Done ' );
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments