Skip to content

Commit

Permalink
New arguments for clear cache command to clear all directories or spe…
Browse files Browse the repository at this point in the history
…cified
  • Loading branch information
ElGigi committed Dec 2, 2022
1 parent 36f2380 commit aff6dcc
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 53 additions & 3 deletions src/Command/Berlioz/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand Down
82 changes: 81 additions & 1 deletion tests/Command/Berlioz/CacheClearCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -53,14 +102,45 @@ 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(
0,
$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')
],
)
)
)
);
Expand Down

0 comments on commit aff6dcc

Please sign in to comment.