diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff2d15..8fe7130 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [2.2.0] - 2022-12-02 + +### Added + +- New arguments for clear cache command to clear all directories or specified + ## [2.1.1] - 2022-03-23 ### Fixed diff --git a/src/Command/Berlioz/CacheClearCommand.php b/src/Command/Berlioz/CacheClearCommand.php index 8886691..91a9707 100644 --- a/src/Command/Berlioz/CacheClearCommand.php +++ b/src/Command/Berlioz/CacheClearCommand.php @@ -15,13 +15,23 @@ namespace Berlioz\Cli\Core\Command\Berlioz; use Berlioz\Cli\Core\Command\AbstractCommand; +use Berlioz\Cli\Core\Command\Argument; use Berlioz\Cli\Core\Console\Environment; +use Berlioz\Cli\Core\Exception\InvalidArgumentException; +use League\Flysystem\FilesystemException; +use League\Flysystem\StorageAttributes; /** * Class CacheClearCommand. */ +#[Argument(name: 'all', longPrefix: 'all', description: 'All caches directories', noValue: true, castTo: 'bool')] +#[Argument(name: 'directory', description: 'Directories name', castTo: 'string')] class CacheClearCommand extends AbstractCommand { + public function __construct() + { + } + /** * @inheritDoc */ @@ -33,22 +43,62 @@ public static function getDescription(): ?string /** * Clear cache. * + * @param bool $directories + * * @return bool + * @throws FilesystemException */ - public function clearCache(): bool + public function clearCache(bool|array $directories = false): bool { - return $this->getApp()->getCore()->getCache()->clear(); + $result = $this->getApp()->getCore()->getCache()->clear(); + + if (false === $directories) { + return $result; + } + + $contents = $this->getApp()->getCore()->getFilesystem()->listContents('cache://'); + + /** @var StorageAttributes $item */ + foreach ($contents as $item) { + $basename = basename($item->path()); + + // Ignore hidden items + if (true === $directories || in_array($basename, $directories)) { + if (str_starts_with($basename, '.')) { + continue; + } + } + + if (true === is_array($directories) && false === in_array($basename, $directories)) { + continue; + } + + // Directory + if ($item->isDir()) { + $this->getApp()->getCore()->getFilesystem()->deleteDirectory($item->path()); + continue; + } + + $this->getApp()->getCore()->getFilesystem()->delete($item->path()); + } + + return $result; } /** * @inheritDoc + * @throws FilesystemException + * @throws InvalidArgumentException */ public function run(Environment $env): int { $env->console()->inline('Cache clear... '); $env->console()->spinner(); - if (true === $this->clearCache()) { + if (true === $this->clearCache( + $env->getArgumentMultiple('directory') ?: + $env->getArgument('all') + )) { $env->console()->green('done!'); return 0; } diff --git a/tests/Command/Berlioz/CacheClearCommandTest.php b/tests/Command/Berlioz/CacheClearCommandTest.php index f8aca87..d9ff072 100644 --- a/tests/Command/Berlioz/CacheClearCommandTest.php +++ b/tests/Command/Berlioz/CacheClearCommandTest.php @@ -13,6 +13,7 @@ namespace Berlioz\Cli\Core\Tests\Command\Berlioz; use Berlioz\Cli\Core\App\CliApp; +use Berlioz\Cli\Core\Command\Argument; use Berlioz\Cli\Core\Command\Berlioz\CacheClearCommand; use Berlioz\Cli\Core\Command\CommandDeclaration; use Berlioz\Cli\Core\Console\Console; @@ -37,12 +38,60 @@ public function testClearCache() { $app = new CliApp(new Core(new FakeDefaultDirectories(), cache: false)); $app->getCore()->getCache()->set('foo', 'bar'); + $app->getCore()->getFilesystem()->write('cache://foo', 'foo content'); $command = new CacheClearCommand(); $command->setApp($app); $this->assertEquals('bar', $app->getCore()->getCache()->get('foo')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://foo')); + $this->assertTrue($command->clearCache()); + + $this->assertNull($app->getCore()->getCache()->get('foo')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://foo')); + } + + public function testClearCache_all() + { + $app = new CliApp(new Core(new FakeDefaultDirectories(), cache: false)); + $app->getCore()->getCache()->set('foo', 'bar'); + $app->getCore()->getFilesystem()->write('cache://foo', 'foo content'); + $app->getCore()->getFilesystem()->write('cache://bar/bar', 'bar content'); + $command = new CacheClearCommand(); + $command->setApp($app); + + $this->assertEquals('bar', $app->getCore()->getCache()->get('foo')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://foo')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://bar/bar')); + + $this->assertTrue($command->clearCache(true)); + $this->assertNull($app->getCore()->getCache()->get('foo')); + $this->assertFalse($app->getCore()->getFilesystem()->fileExists('cache://foo')); + $this->assertFalse($app->getCore()->getFilesystem()->fileExists('cache://bar/bar')); + } + + public function testClearCache_list() + { + $app = new CliApp(new Core(new FakeDefaultDirectories(), cache: false)); + $app->getCore()->getCache()->set('foo', 'bar'); + $app->getCore()->getFilesystem()->write('cache://bar/bar', 'bar content'); + $app->getCore()->getFilesystem()->write('cache://baz/baz', 'baz content'); + $app->getCore()->getFilesystem()->write('cache://qux/qux', 'bux content'); + $command = new CacheClearCommand(); + $command->setApp($app); + + $this->assertEquals('bar', $app->getCore()->getCache()->get('foo')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://bar/bar')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://baz/baz')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://qux/qux')); + + $this->assertTrue($command->clearCache(['bar', 'qux'])); + + $this->assertNull($app->getCore()->getCache()->get('foo')); + $this->assertFalse($app->getCore()->getFilesystem()->fileExists('cache://bar/bar')); + $this->assertTrue($app->getCore()->getFilesystem()->fileExists('cache://baz/baz')); + $this->assertFalse($app->getCore()->getFilesystem()->fileExists('cache://qux/qux')); } public function testRun() @@ -53,6 +102,24 @@ public function testRun() $command->setApp($app); $console = new Console(); $console->output->defaultTo('buffer'); + $console->getArgumentsManager()->add( + 'all', + [ + 'name' => 'all', + 'longPrefix' => 'all', + 'description' => 'All caches directories', + 'noValue' => true, + 'castTo' => 'bool' + ] + ); + $console->getArgumentsManager()->add( + 'directory', + [ + 'name' => 'directory', + 'description' => 'Directories name', + 'castTo' => 'string' + ] + ); $this->assertEquals('bar', $app->getCore()->getCache()->get('foo')); $this->assertSame( @@ -60,7 +127,20 @@ public function testRun() $command->run( new Environment( $console, - new CommandDeclaration('berlioz:cache-clear', CacheClearCommand::class) + new CommandDeclaration( + 'berlioz:cache-clear', + CacheClearCommand::class, + [ + new Argument( + name: 'all', + longPrefix: 'all', + description: 'All caches directories', + noValue: true, + castTo: 'bool' + ), + new Argument(name: 'directory', description: 'Directories name', castTo: 'string') + ], + ) ) ) );