Skip to content

Commit 659af6e

Browse files
committed
Merge branch 'cache-5.2' of https://github.com/KennedyTedesco/framework into KennedyTedesco-cache-5.2
2 parents b98281b + c804ba4 commit 659af6e

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/Illuminate/Cache/Console/ClearCommand.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Cache\CacheManager;
7+
use Symfony\Component\Console\Input\InputOption;
78
use Symfony\Component\Console\Input\InputArgument;
89

910
class ClearCommand extends Command
@@ -47,17 +48,26 @@ public function __construct(CacheManager $cache)
4748
*
4849
* @return void
4950
*/
50-
public function fire()
51+
public function handle()
5152
{
52-
$storeName = $this->argument('store');
53+
$storeName = $this->hasArgument('store') ? $this->argument('store') : null;
54+
$tagsNames = $this->hasOption('tags') ? $this->option('tags') : null;
5355

54-
$this->laravel['events']->fire('cache:clearing', [$storeName]);
56+
$store = $this->cache->store($storeName);
5557

56-
$this->cache->store($storeName)->flush();
58+
$this->laravel['events']->fire('cache:clearing', [$storeName, $tagsNames]);
5759

58-
$this->laravel['events']->fire('cache:cleared', [$storeName]);
60+
if (! is_null($tagsNames)) {
61+
$store->tags(explode(',', $tagsNames))->flush();
5962

60-
$this->info('Application cache cleared!');
63+
$this->info(sprintf('Application cache tags "%s" cleared!', $tagsNames));
64+
} else {
65+
$store->flush();
66+
67+
$this->info('Application cache cleared!');
68+
}
69+
70+
$this->laravel['events']->fire('cache:cleared', [$storeName, $tagsNames]);
6171
}
6272

6373
/**
@@ -71,4 +81,16 @@ protected function getArguments()
7181
['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear.'],
7282
];
7383
}
84+
85+
/**
86+
* Get the console command options.
87+
*
88+
* @return array
89+
*/
90+
protected function getOptions()
91+
{
92+
return [
93+
['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear.', null],
94+
];
95+
}
7496
}

tests/Cache/ClearCommandTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function tearDown()
1111
m::close();
1212
}
1313

14-
public function testClearWithNoStoreOption()
14+
public function testClearWithNoStoreArgument()
1515
{
1616
$command = new ClearCommandTestStub(
1717
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
@@ -28,7 +28,7 @@ public function testClearWithNoStoreOption()
2828
$this->runCommand($command);
2929
}
3030

31-
public function testClearWithStoreOption()
31+
public function testClearWithStoreArgument()
3232
{
3333
$command = new ClearCommandTestStub(
3434
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
@@ -48,7 +48,7 @@ public function testClearWithStoreOption()
4848
/**
4949
* @expectedException InvalidArgumentException
5050
*/
51-
public function testClearWithInvalidStoreOption()
51+
public function testClearWithInvalidStoreArgument()
5252
{
5353
$command = new ClearCommandTestStub(
5454
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
@@ -65,6 +65,42 @@ public function testClearWithInvalidStoreOption()
6565
$this->runCommand($command, ['store' => 'bar']);
6666
}
6767

68+
public function testClearWithTagsOption()
69+
{
70+
$command = new ClearCommandTestStub(
71+
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
72+
);
73+
74+
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
75+
76+
$app = new Application();
77+
$command->setLaravel($app);
78+
79+
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
80+
$cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository);
81+
$cacheRepository->shouldReceive('flush')->once();
82+
83+
$this->runCommand($command, ['--tags' => 'foo,bar']);
84+
}
85+
86+
public function testClearWithStoreArgumentAndTagsOption()
87+
{
88+
$command = new ClearCommandTestStub(
89+
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
90+
);
91+
92+
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
93+
94+
$app = new Application();
95+
$command->setLaravel($app);
96+
97+
$cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository);
98+
$cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository);
99+
$cacheRepository->shouldReceive('flush')->once();
100+
101+
$this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']);
102+
}
103+
68104
protected function runCommand($command, $input = [])
69105
{
70106
return $command->run(new Symfony\Component\Console\Input\ArrayInput($input), new Symfony\Component\Console\Output\NullOutput);

0 commit comments

Comments
 (0)