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: public function getOptionParser()
38: {
39: $parser = parent::getOptionParser();
40: $parser->addSubcommand('list_prefixes', [
41: 'help' => 'Show a list of all defined cache prefixes.',
42: ]);
43: $parser->addSubcommand('clear_all', [
44: 'help' => 'Clear all caches.',
45: ]);
46: $parser->addSubcommand('clear', [
47: 'help' => 'Clear the cache for a specified prefix.',
48: 'parser' => [
49: 'description' => [
50: 'Clear the cache for a particular prefix.',
51: 'For example, `cake cache clear _cake_model_` will clear the model cache',
52: 'Use `cake cache list_prefixes` to list available prefixes'
53: ],
54: 'arguments' => [
55: 'prefix' => [
56: 'help' => 'The cache prefix to be cleared.',
57: 'required' => true
58: ]
59: ]
60: ]
61: ]);
62:
63: return $parser;
64: }
65:
66: 67: 68: 69: 70: 71: 72:
73: public function clear($prefix = null)
74: {
75: try {
76: $engine = Cache::engine($prefix);
77: Cache::clear(false, $prefix);
78: if ($engine instanceof ApcuEngine) {
79: $this->warn("ApcuEngine detected: Cleared $prefix CLI cache successfully " .
80: "but $prefix web cache must be cleared separately.");
81: } elseif ($engine instanceof WincacheEngine) {
82: $this->warn("WincacheEngine detected: Cleared $prefix CLI cache successfully " .
83: "but $prefix web cache must be cleared separately.");
84: } else {
85: $this->out("<success>Cleared $prefix cache</success>");
86: }
87: } catch (InvalidArgumentException $e) {
88: $this->abort($e->getMessage());
89: }
90: }
91:
92: 93: 94: 95: 96:
97: public function clearAll()
98: {
99: $prefixes = Cache::configured();
100: foreach ($prefixes as $prefix) {
101: $this->clear($prefix);
102: }
103: }
104:
105: 106: 107: 108: 109:
110: public function listPrefixes()
111: {
112: $prefixes = Cache::configured();
113: foreach ($prefixes as $prefix) {
114: $this->out($prefix);
115: }
116: }
117: }
118: