Skip to content

Commit 440bca2

Browse files
committed
Move facade clearing logic to ClearCommand instead of FileStore
1 parent 651b2d5 commit 440bca2

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

src/Illuminate/Cache/Console/ClearCommand.php

Lines changed: 27 additions & 1 deletion
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 Illuminate\Filesystem\Filesystem;
78
use Symfony\Component\Console\Input\InputOption;
89
use Symfony\Component\Console\Input\InputArgument;
910

@@ -30,17 +31,26 @@ class ClearCommand extends Command
3031
*/
3132
protected $cache;
3233

34+
/**
35+
* The filesystem instance.
36+
*
37+
* @var \Illuminate\Filesystem\Filesystem
38+
*/
39+
protected $files;
40+
3341
/**
3442
* Create a new cache clear command instance.
3543
*
3644
* @param \Illuminate\Cache\CacheManager $cache
45+
* @param \Illuminate\Filesystem\Filesystem $files
3746
* @return void
3847
*/
39-
public function __construct(CacheManager $cache)
48+
public function __construct(CacheManager $cache, Filesystem $files)
4049
{
4150
parent::__construct();
4251

4352
$this->cache = $cache;
53+
$this->files = $files;
4454
}
4555

4656
/**
@@ -54,6 +64,8 @@ public function handle()
5464

5565
$this->cache()->flush();
5666

67+
$this->clearRealTimeFacades();
68+
5769
$this->laravel['events']->fire('cache:cleared', [$this->argument('store'), $this->tags()]);
5870

5971
$this->info('Cache cleared successfully.');
@@ -104,4 +116,18 @@ protected function getOptions()
104116
['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear.', null],
105117
];
106118
}
119+
120+
/**
121+
* Clear real-time facades stored in the framework's cache directory.
122+
*
123+
* @return void
124+
*/
125+
public function clearRealTimeFacades()
126+
{
127+
foreach ($this->files->files(storage_path('framework/cache')) as $file) {
128+
if (preg_match('/facade-.*\.php$/', $file)) {
129+
$this->files->delete($file);
130+
}
131+
}
132+
}
107133
}

src/Illuminate/Cache/FileStore.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ public function flush()
151151
}
152152
}
153153

154-
foreach ($this->files->files($this->directory) as $file) {
155-
if (preg_match('/facade-.*\.php$/', $file)) {
156-
if (! $this->files->delete($file)) {
157-
return false;
158-
}
159-
}
160-
}
161-
162154
return true;
163155
}
164156

tests/Cache/CacheFileStoreTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public function testFlushCleansDirectory()
138138
$files = $this->mockFilesystem();
139139
$files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->will($this->returnValue(true));
140140
$files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->will($this->returnValue(['foo']));
141-
$files->expects($this->once())->method('files')->with($this->equalTo(__DIR__))->will($this->returnValue([]));
142141
$files->expects($this->once())->method('deleteDirectory')->with($this->equalTo('foo'))->will($this->returnValue(true));
143142

144143
$store = new FileStore($files, __DIR__);
@@ -168,19 +167,6 @@ public function testFlushIgnoreNonExistingDirectory()
168167
$this->assertFalse($result, 'Flush should not clean directory');
169168
}
170169

171-
public function testFlushCleansRealTimeFacades()
172-
{
173-
$files = $this->mockFilesystem();
174-
$files->expects($this->once())->method('isDirectory')->with($this->equalTo(__DIR__))->will($this->returnValue(true));
175-
$files->expects($this->once())->method('directories')->with($this->equalTo(__DIR__))->will($this->returnValue([]));
176-
$files->expects($this->once())->method('files')->with($this->equalTo(__DIR__))->will($this->returnValue(['/facade-XXX.php']));
177-
$files->expects($this->once())->method('delete')->with($this->equalTo('/facade-XXX.php'))->will($this->returnValue(true));
178-
179-
$store = new FileStore($files, __DIR__);
180-
$result = $store->flush();
181-
$this->assertTrue($result, 'Flush failed');
182-
}
183-
184170
protected function mockFilesystem()
185171
{
186172
return $this->createMock('Illuminate\Filesystem\Filesystem');

tests/Cache/ClearCommandTest.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ public function tearDown()
1717
public function testClearWithNoStoreArgument()
1818
{
1919
$command = new ClearCommandTestStub(
20-
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
20+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
21+
$files = m::mock('Illuminate\Filesystem\Filesystem')
2122
);
2223

2324
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
2425

2526
$app = new Application;
27+
$app['path.storage'] = __DIR__;
2628
$command->setLaravel($app);
29+
$files->shouldReceive('files')->andReturn([]);
2730

2831
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
2932
$cacheRepository->shouldReceive('flush')->once();
@@ -34,13 +37,16 @@ public function testClearWithNoStoreArgument()
3437
public function testClearWithStoreArgument()
3538
{
3639
$command = new ClearCommandTestStub(
37-
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
40+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
41+
$files = m::mock('Illuminate\Filesystem\Filesystem')
3842
);
3943

4044
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
4145

4246
$app = new Application;
47+
$app['path.storage'] = __DIR__;
4348
$command->setLaravel($app);
49+
$files->shouldReceive('files')->andReturn([]);
4450

4551
$cacheManager->shouldReceive('store')->once()->with('foo')->andReturn($cacheRepository);
4652
$cacheRepository->shouldReceive('flush')->once();
@@ -55,13 +61,16 @@ public function testClearWithStoreArgument()
5561
public function testClearWithInvalidStoreArgument()
5662
{
5763
$command = new ClearCommandTestStub(
58-
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
64+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
65+
$files = m::mock('Illuminate\Filesystem\Filesystem')
5966
);
6067

6168
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
6269

6370
$app = new Application;
71+
$app['path.storage'] = __DIR__;
6472
$command->setLaravel($app);
73+
$files->shouldReceive('files')->andReturn([]);
6574

6675
$cacheManager->shouldReceive('store')->once()->with('bar')->andThrow('InvalidArgumentException');
6776
$cacheRepository->shouldReceive('flush')->never();
@@ -72,13 +81,16 @@ public function testClearWithInvalidStoreArgument()
7281
public function testClearWithTagsOption()
7382
{
7483
$command = new ClearCommandTestStub(
75-
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
84+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
85+
$files = m::mock('Illuminate\Filesystem\Filesystem')
7686
);
7787

7888
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
7989

8090
$app = new Application;
91+
$app['path.storage'] = __DIR__;
8192
$command->setLaravel($app);
93+
$files->shouldReceive('files')->andReturn([]);
8294

8395
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
8496
$cacheRepository->shouldReceive('tags')->once()->with(['foo', 'bar'])->andReturn($cacheRepository);
@@ -90,13 +102,16 @@ public function testClearWithTagsOption()
90102
public function testClearWithStoreArgumentAndTagsOption()
91103
{
92104
$command = new ClearCommandTestStub(
93-
$cacheManager = m::mock('Illuminate\Cache\CacheManager')
105+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
106+
$files = m::mock('Illuminate\Filesystem\Filesystem')
94107
);
95108

96109
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
97110

98111
$app = new Application;
112+
$app['path.storage'] = __DIR__;
99113
$command->setLaravel($app);
114+
$files->shouldReceive('files')->andReturn([]);
100115

101116
$cacheManager->shouldReceive('store')->once()->with('redis')->andReturn($cacheRepository);
102117
$cacheRepository->shouldReceive('tags')->once()->with(['foo'])->andReturn($cacheRepository);
@@ -105,6 +120,27 @@ public function testClearWithStoreArgumentAndTagsOption()
105120
$this->runCommand($command, ['store' => 'redis', '--tags' => 'foo']);
106121
}
107122

123+
public function testClearWillClearsRealTimeFacades()
124+
{
125+
$command = new ClearCommandTestStub(
126+
$cacheManager = m::mock('Illuminate\Cache\CacheManager'),
127+
$files = m::mock('Illuminate\Filesystem\Filesystem')
128+
);
129+
130+
$cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
131+
132+
$app = new Application;
133+
$app['path.storage'] = __DIR__;
134+
$command->setLaravel($app);
135+
$cacheManager->shouldReceive('store')->once()->with(null)->andReturn($cacheRepository);
136+
$cacheRepository->shouldReceive('flush')->once();
137+
138+
$files->shouldReceive('files')->andReturn(['/facade-XXXX.php']);
139+
$files->shouldReceive('delete')->with('/facade-XXXX.php')->once();
140+
141+
$this->runCommand($command);
142+
}
143+
108144
protected function runCommand($command, $input = [])
109145
{
110146
return $command->run(new \Symfony\Component\Console\Input\ArrayInput($input), new \Symfony\Component\Console\Output\NullOutput);

0 commit comments

Comments
 (0)