1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell;
16:
17: use Cake\Cache\Cache;
18: use Cake\Cache\Engine\ApcuEngine;
19: use Cake\Cache\Engine\WincacheEngine;
20: use Cake\Console\Shell;
21: use InvalidArgumentException;
22:
23: 24: 25: 26: 27: 28: 29:
30: class CacheShell extends Shell
31: {
32:
33: 34: 35: 36: 37:
38: public function getOptionParser()
39: {
40: $parser = parent::getOptionParser();
41: $parser->addSubcommand('list_prefixes', [
42: 'help' => 'Show a list of all defined cache prefixes.',
43: ]);
44: $parser->addSubcommand('clear_all', [
45: 'help' => 'Clear all caches.',
46: ]);
47: $parser->addSubcommand('clear', [
48: 'help' => 'Clear the cache for a specified prefix.',
49: 'parser' => [
50: 'description' => [
51: 'Clear the cache for a particular prefix.',
52: 'For example, `cake cache clear _cake_model_` will clear the model cache',
53: 'Use `cake cache list_prefixes` to list available prefixes'
54: ],
55: 'arguments' => [
56: 'prefix' => [
57: 'help' => 'The cache prefix to be cleared.',
58: 'required' => true
59: ]
60: ]
61: ]
62: ]);
63:
64: return $parser;
65: }
66:
67: 68: 69: 70: 71: 72: 73:
74: public function clear($prefix = null)
75: {
76: try {
77: $engine = Cache::engine($prefix);
78: Cache::clear(false, $prefix);
79: if ($engine instanceof ApcuEngine) {
80: $this->warn("ApcuEngine detected: Cleared $prefix CLI cache successfully " .
81: "but $prefix web cache must be cleared separately.");
82: } elseif ($engine instanceof WincacheEngine) {
83: $this->warn("WincacheEngine detected: Cleared $prefix CLI cache successfully " .
84: "but $prefix web cache must be cleared separately.");
85: } else {
86: $this->out("<success>Cleared $prefix cache</success>");
87: }
88: } catch (InvalidArgumentException $e) {
89: $this->abort($e->getMessage());
90: }
91: }
92:
93: 94: 95: 96: 97:
98: public function clearAll()
99: {
100: $prefixes = Cache::configured();
101: foreach ($prefixes as $prefix) {
102: $this->clear($prefix);
103: }
104: }
105:
106: 107: 108: 109: 110:
111: public function listPrefixes()
112: {
113: $prefixes = Cache::configured();
114: foreach ($prefixes as $prefix) {
115: $this->out($prefix);
116: }
117: }
118: }
119: