Skip to content

Commit 91460ef

Browse files
authored
Merge pull request #3304 from codeigniter4/clearcache
New command: cache:clear
2 parents 3919bc3 + 8e680c7 commit 91460ef

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php namespace CodeIgniter\Commands\Cache;
2+
3+
use CodeIgniter\Cache\CacheFactory;
4+
use CodeIgniter\CLI\BaseCommand;
5+
use CodeIgniter\CLI\CLI;
6+
7+
class ClearCache extends BaseCommand
8+
{
9+
/**
10+
* Command grouping.
11+
*
12+
* @var string
13+
*/
14+
protected $group = 'Cache';
15+
16+
/**
17+
* The Command's name
18+
*
19+
* @var string
20+
*/
21+
protected $name = 'cache:clear';
22+
23+
/**
24+
* the Command's short description
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Clears the current system caches.';
29+
30+
/**
31+
* the Command's usage
32+
*
33+
* @var string
34+
*/
35+
protected $usage = 'cache:clear [driver]';
36+
37+
/**
38+
* the Command's Arguments
39+
*
40+
* @var array
41+
*/
42+
protected $arguments = [
43+
'driver' => 'The cache driver to use',
44+
];
45+
46+
/**
47+
* Creates a new migration file with the current timestamp.
48+
*
49+
* @param array $params
50+
*/
51+
public function run(array $params = [])
52+
{
53+
$config = config('Cache');
54+
55+
$handler = $params[0] ?? $config->handler;
56+
if (! array_key_exists($handler, $config->validHandlers))
57+
{
58+
CLI::error($handler . ' is not a valid cache handler.');
59+
return;
60+
}
61+
62+
$config->handler = $handler;
63+
$cache = CacheFactory::getHandler($config);
64+
65+
if (! $cache->clean())
66+
{
67+
CLI::error('Error while clearing the cache.');
68+
return;
69+
}
70+
71+
CLI::write(CLI::color('Done', 'green'));
72+
}
73+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php namespace CodeIgniter\Commands;
2+
3+
use CodeIgniter\CLI\CLI;
4+
use CodeIgniter\CLI\CommandRunner;
5+
use CodeIgniter\Config\Config;
6+
use CodeIgniter\HTTP\UserAgent;
7+
use CodeIgniter\Test\CIUnitTestCase;
8+
use CodeIgniter\Test\Filters\CITestStreamFilter;
9+
use CodeIgniter\Test\Mock\MockAppConfig;
10+
use Config\Services;
11+
12+
class ClearCacheTest extends CIUnitTestCase
13+
{
14+
protected $streamFilter;
15+
protected $result;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
CITestStreamFilter::$buffer = '';
22+
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
23+
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
24+
}
25+
26+
public function tearDown(): void
27+
{
28+
if (! $this->result)
29+
{
30+
return;
31+
}
32+
33+
stream_filter_remove($this->streamFilter);
34+
}
35+
36+
public function testClearCacheInvalidHandler()
37+
{
38+
command('cache:clear junk');
39+
$result = CITestStreamFilter::$buffer;
40+
41+
$this->assertStringContainsString('junk is not a valid cache handler.', $result);
42+
}
43+
44+
public function testClearCacheWorks()
45+
{
46+
cache()->save('foo', 'bar');
47+
48+
$this->assertEquals('bar', cache('foo'));
49+
50+
command('cache:clear');
51+
$result = CITestStreamFilter::$buffer;
52+
53+
$this->assertNull(cache('foo'));
54+
55+
$this->assertStringContainsString('Done', $result);
56+
}
57+
}

0 commit comments

Comments
 (0)