diff --git a/src/System/Integrate/Console/ClearCacheCommand.php b/src/System/Integrate/Console/ClearCacheCommand.php new file mode 100644 index 00000000..bc6a5d74 --- /dev/null +++ b/src/System/Integrate/Console/ClearCacheCommand.php @@ -0,0 +1,96 @@ +> + */ + public static array $command = [ + [ + 'pattern' => 'cache:clear', + 'fn' => [ClearCacheCommand::class, 'clear'], + ], + ]; + + /** + * @return array> + */ + public function printHelp() + { + return [ + 'commands' => [ + 'cache:clear' => 'Clear cache (default drive)', + ], + 'options' => [ + '--all' => 'Clear all registered cache driver.', + '--drivers' => 'Clear spesific driver name.', + ], + 'relation' => [ + 'cache:clear' => ['--all', '--drivers'], + ], + ]; + } + + public function clear(Application $app): int + { + if (false === $app->has('cache')) { + fail('Cache is not set yet.')->out(); + + return 1; + } + + /** @var \System\Cache\CacheManager|null */ + $cache = $app['cache']; + + /** @var string[]|null */ + $drivers = null; + + /** @var string[]|string|bool */ + $user_drivers = $this->option('drivers', false); + + if ($this->option('all', false) && false === $user_drivers) { + $drivers = array_keys( + (fn (): array => $this->{'driver'})->call($cache) // @phpstan-ignore-line + ); + } + + if ($user_drivers) { + $drivers = is_array($user_drivers) ? $user_drivers : [$user_drivers]; + } + + if (null === $drivers) { + $cache->driver()->clear(); + ok('Done default cache driver has been clear.')->out(false); + + return 0; + } + + foreach ($drivers as $driver) { + $cache->driver($driver)->clear(); + info("clear '{$driver}' driver.")->out(false); + } + + return 0; + } +} diff --git a/tests/Integrate/Commands/ClearCacheCommandTest.php b/tests/Integrate/Commands/ClearCacheCommandTest.php new file mode 100644 index 00000000..e816d036 --- /dev/null +++ b/tests/Integrate/Commands/ClearCacheCommandTest.php @@ -0,0 +1,86 @@ +app = new Application(__DIR__); + } + + protected function teardown(): void + { + $this->app->flush(); + $this->app = null; + } + + /** @test */ + public function itCantRunCommand(): void + { + $command = new ClearCacheCommand(['cli', 'clear:cache']); + + ob_start(); + $code = $command->clear($this->app); + $out =ob_get_clean(); + + $this->assertEquals(1, $code); + $this->assertStringContainsString('Cache is not set yet.', $out); + } + + /** @test */ + public function itCanClearDefaultDriver(): void + { + $this->app->set('cache', fn () => new CacheManager()); + $command = new ClearCacheCommand(['cli', 'clear:cache']); + + ob_start(); + $code = $command->clear($this->app); + $out =ob_get_clean(); + + $this->assertEquals(0, $code); + $this->assertStringContainsString('Done default cache driver has been clear.', $out); + } + + /** @test */ + public function itCanAllDriver(): void + { + $cache_manager = new CacheManager(); + $cache_manager->setDriver('array', new ArrayStorage()); + $this->app->set('cache', fn () => $cache_manager); + $command = new ClearCacheCommand(['cli', 'clear:cache', '--all'], ['all' => true]); + + ob_start(); + $code = $command->clear($this->app); + $out =ob_get_clean(); + + $this->assertEquals(0, $code); + $this->assertStringContainsString("clear 'array' driver.", $out); + } + + /** @test */ + public function itCanSpesifikDriver(): void + { + $cache_manager = new CacheManager(); + $cache_manager->setDriver('array', new ArrayStorage()); + $this->app->set('cache', fn () => $cache_manager); + $command = new ClearCacheCommand(['cli', 'clear:cache', '--drivers array'], ['drivers' => 'array']); + + ob_start(); + $code = $command->clear($this->app); + $out =ob_get_clean(); + + $this->assertEquals(0, $code); + $this->assertStringContainsString("clear 'array' driver.", $out); + } +}