diff --git a/src/Illuminate/Cache/Console/ClearCommand.php b/src/Illuminate/Cache/Console/ClearCommand.php index 48c57634231a..cc1360d3c870 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 Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; @@ -30,17 +31,26 @@ class ClearCommand extends Command */ protected $cache; + /** + * The filesystem instance. + * + * @var \Illuminate\Filesystem\Filesystem + */ + protected $files; + /** * Create a new cache clear command instance. * * @param \Illuminate\Cache\CacheManager $cache + * @param \Illuminate\Filesystem\Filesystem $files * @return void */ - public function __construct(CacheManager $cache) + public function __construct(CacheManager $cache, Filesystem $files) { parent::__construct(); $this->cache = $cache; + $this->files = $files; } /** @@ -54,6 +64,8 @@ public function handle() $this->cache()->flush(); + $this->clearRealTimeFacades(); + $this->laravel['events']->fire('cache:cleared', [$this->argument('store'), $this->tags()]); $this->info('Cache cleared successfully.'); @@ -104,4 +116,18 @@ protected function getOptions() ['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear.', null], ]; } + + /** + * Clear real-time facades stored in the framework's cache directory. + * + * @return void + */ + public function clearRealTimeFacades() + { + foreach ($this->files->files(storage_path('framework/cache')) as $file) { + if (preg_match('/facade-.*\.php$/', $file)) { + $this->files->delete($file); + } + } + } } diff --git a/src/Illuminate/Cache/FileStore.php b/src/Illuminate/Cache/FileStore.php index 3259554aeffe..d910731fa326 100755 --- a/src/Illuminate/Cache/FileStore.php +++ b/src/Illuminate/Cache/FileStore.php @@ -151,14 +151,6 @@ public function flush() } } - foreach ($this->files->files($this->directory) as $file) { - if (preg_match('/facade-.*\.php$/', $file)) { - if (! $this->files->delete($file)) { - return false; - } - } - } - return true; } diff --git a/tests/Cache/CacheFileStoreTest.php b/tests/Cache/CacheFileStoreTest.php index ffe6c3e28308..298a89d11e9f 100755 --- a/tests/Cache/CacheFileStoreTest.php +++ b/tests/Cache/CacheFileStoreTest.php @@ -138,7 +138,6 @@ public function testFlushCleansDirectory() $files = $this->mockFilesystem(); $files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->will($this->returnValue(true)); $files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->will($this->returnValue(['foo'])); - $files->expects($this->once())->method('files')->with($this->equalTo(__DIR__))->will($this->returnValue([])); $files->expects($this->once())->method('deleteDirectory')->with($this->equalTo('foo'))->will($this->returnValue(true)); $store = new FileStore($files, __DIR__); @@ -168,19 +167,6 @@ public function testFlushIgnoreNonExistingDirectory() $this->assertFalse($result, 'Flush should not clean directory'); } - public function testFlushCleansRealTimeFacades() - { - $files = $this->mockFilesystem(); - $files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->will($this->returnValue(true)); - $files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->will($this->returnValue([])); - $files->expects($this->once())->method('files')->with($this->equalTo(__DIR__))->will($this->returnValue(['/facade-XXX.php'])); - $files->expects($this->once())->method('delete')->with($this->equalTo('/facade-XXX.php'))->will($this->returnValue(true)); - - $store = new FileStore($files, __DIR__); - $result = $store->flush(); - $this->assertTrue($result, 'Flush failed'); - } - protected function mockFilesystem() { return $this->createMock('Illuminate\Filesystem\Filesystem'); diff --git a/tests/Cache/ClearCommandTest.php b/tests/Cache/ClearCommandTest.php index 7bcf5ec845ff..ba5278da6493 100644 --- a/tests/Cache/ClearCommandTest.php +++ b/tests/Cache/ClearCommandTest.php @@ -17,13 +17,16 @@ public function tearDown() public function testClearWithNoStoreArgument() { $command = new ClearCommandTestStub( - $cacheManager = m::mock('Illuminate\Cache\CacheManager') + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') ); $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); $app = new Application; + $app['path.storage'] = __DIR__; $command->setLaravel($app); + $files->shouldReceive('files')->andReturn([]); $cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository); $cacheRepository->shouldReceive('flush')->once(); @@ -34,13 +37,16 @@ public function testClearWithNoStoreArgument() public function testClearWithStoreArgument() { $command = new ClearCommandTestStub( - $cacheManager = m::mock('Illuminate\Cache\CacheManager') + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') ); $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); $app = new Application; + $app['path.storage'] = __DIR__; $command->setLaravel($app); + $files->shouldReceive('files')->andReturn([]); $cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($cacheRepository); $cacheRepository->shouldReceive('flush')->once(); @@ -55,13 +61,16 @@ public function testClearWithStoreArgument() public function testClearWithInvalidStoreArgument() { $command = new ClearCommandTestStub( - $cacheManager = m::mock('Illuminate\Cache\CacheManager') + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') ); $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); $app = new Application; + $app['path.storage'] = __DIR__; $command->setLaravel($app); + $files->shouldReceive('files')->andReturn([]); $cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException'); $cacheRepository->shouldReceive('flush')->never(); @@ -72,13 +81,16 @@ public function testClearWithInvalidStoreArgument() public function testClearWithTagsOption() { $command = new ClearCommandTestStub( - $cacheManager = m::mock('Illuminate\Cache\CacheManager') + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') ); $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); $app = new Application; + $app['path.storage'] = __DIR__; $command->setLaravel($app); + $files->shouldReceive('files')->andReturn([]); $cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository); $cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository); @@ -90,13 +102,16 @@ public function testClearWithTagsOption() public function testClearWithStoreArgumentAndTagsOption() { $command = new ClearCommandTestStub( - $cacheManager = m::mock('Illuminate\Cache\CacheManager') + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') ); $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); $app = new Application; + $app['path.storage'] = __DIR__; $command->setLaravel($app); + $files->shouldReceive('files')->andReturn([]); $cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository); $cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository); @@ -105,6 +120,27 @@ public function testClearWithStoreArgumentAndTagsOption() $this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']); } + public function testClearWillClearsRealTimeFacades() + { + $command = new ClearCommandTestStub( + $cacheManager = m::mock('Illuminate\Cache\CacheManager'), + $files = m::mock('Illuminate\Filesystem\Filesystem') + ); + + $cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository'); + + $app = new Application; + $app['path.storage'] = __DIR__; + $command->setLaravel($app); + $cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository); + $cacheRepository->shouldReceive('flush')->once(); + + $files->shouldReceive('files')->andReturn(['/facade-XXXX.php']); + $files->shouldReceive('delete')->with('/facade-XXXX.php')->once(); + + $this->runCommand($command); + } + protected function runCommand($command, $input = []) { return $command->run(new \Symfony\Component\Console\Input\ArrayInput($input), new \Symfony\Component\Console\Output\NullOutput);