diff --git a/src/Illuminate/Cache/Console/ClearCommand.php b/src/Illuminate/Cache/Console/ClearCommand.php index 02ea032fdf40..836869fcd514 100755 --- a/src/Illuminate/Cache/Console/ClearCommand.php +++ b/src/Illuminate/Cache/Console/ClearCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Cache\CacheManager; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; class ClearCommand extends Command @@ -47,17 +48,26 @@ public function __construct(CacheManager $cache) * * @return void */ - public function fire() + public function handle() { - $storeName = $this->argument('store'); + $storeName = $this->hasArgument('store') ? $this->argument('store') : null; + $tagsNames = $this->hasOption('tags') ? $this->option('tags') : null; - $this->laravel['events']->fire('cache:clearing', [$storeName]); + $store = $this->cache->store($storeName); - $this->cache->store($storeName)->flush(); + $this->laravel['events']->fire('cache:clearing', [$storeName, $tagsNames]); - $this->laravel['events']->fire('cache:cleared', [$storeName]); + if (! is_null($tagsNames)) { + $store->tags(explode(',', $tagsNames))->flush(); - $this->info('Application cache cleared!'); + $this->info(sprintf('Application cache tags "%s" cleared!', $tagsNames)); + } else { + $store->flush(); + + $this->info('Application cache cleared!'); + } + + $this->laravel['events']->fire('cache:cleared', [$storeName, $tagsNames]); } /** @@ -71,4 +81,16 @@ protected function getArguments() ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear.'], ]; } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear.', null], + ]; + } } diff --git a/tests/Cache/ClearCommandTest.php b/tests/Cache/ClearCommandTest.php index 29b074447aa6..004ce94ff83b 100644 --- a/tests/Cache/ClearCommandTest.php +++ b/tests/Cache/ClearCommandTest.php @@ -11,7 +11,7 @@ public function tearDown() m::close(); } - public function testClearWithNoStoreOption() + public function testClearWithNoStoreArgument() { $command = new ClearCommandTestStub( $cacheManager = m::mock('Illuminate\Cache\CacheManager') @@ -28,7 +28,7 @@ public function testClearWithNoStoreOption() $this->runCommand($command); } - public function testClearWithStoreOption() + public function testClearWithStoreArgument() { $command = new ClearCommandTestStub( $cacheManager = m::mock('Illuminate\Cache\CacheManager') @@ -48,7 +48,7 @@ public function testClearWithStoreOption() /** * @expectedException InvalidArgumentException */ - public function testClearWithInvalidStoreOption() + public function testClearWithInvalidStoreArgument() { $command = new ClearCommandTestStub( $cacheManager = m::mock('Illuminate\Cache\CacheManager') @@ -65,6 +65,42 @@ public function testClearWithInvalidStoreOption() $this->runCommand($command, ['store' => 'bar']); } + public function testClearWithTagsOption() + { + $command = new ClearCommandTestStub( + $cacheManager = m::mock('Illuminate\Cache\CacheManager') + ); + + $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); + + $app = new Application(); + $command->setLaravel($app); + + $cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository); + $cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository); + $cacheRepository->shouldReceive('flush')->once(); + + $this->runCommand($command, ['--tags' => 'foo,bar']); + } + + public function testClearWithStoreArgumentAndTagsOption() + { + $command = new ClearCommandTestStub( + $cacheManager = m::mock('Illuminate\Cache\CacheManager') + ); + + $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); + + $app = new Application(); + $command->setLaravel($app); + + $cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository); + $cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository); + $cacheRepository->shouldReceive('flush')->once(); + + $this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']); + } + protected function runCommand($command, $input = []) { return $command->run(new Symfony\Component\Console\Input\ArrayInput($input), new Symfony\Component\Console\Output\NullOutput);