1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\console\controllers;
use Yii;
use yii\console\Controller;
use yii\caching\Cache;
use yii\helpers\Console;
use yii\console\Exception;
/**
* Allows you to flush cache.
*
* see list of available components to flush:
*
* yii cache
*
* flush particular components specified by their names:
*
* yii cache/flush first second third
*
* flush all cache components that can be found in the system
*
* yii cache/flush-all
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0
*/
class CacheController extends Controller
{
/**
* Lists the caches that can be flushed.
*/
public function actionIndex()
{
$caches = $this->findCaches();
if (!empty($caches)) {
$this->notifyCachesCanBeFlushed($caches);
} else {
$this->notifyNoCachesFound();
}
}
/**
* Flushes given cache components.
* For example,
*
* ~~~
* # flushes caches specified by their id: "first", "second", "third"
* yii cache/flush first second third
* ~~~
*
*/
public function actionFlush()
{
$cachesInput = func_get_args();
if (empty($cachesInput)) {
throw new Exception("You should specify cache components names");
}
$caches = $this->findCaches($cachesInput);
$cachesInfo = [];
$foundCaches = array_keys($caches);
$notFoundCaches = array_diff($cachesInput, array_keys($caches));
if ($notFoundCaches) {
$this->notifyNotFoundCaches($notFoundCaches);
}
if (!$foundCaches) {
$this->notifyNoCachesFound();
return static::EXIT_CODE_NORMAL;
}
if (!$this->confirmFlush($foundCaches)) {
return static::EXIT_CODE_NORMAL;
}
foreach ($caches as $name => $class) {
$cachesInfo[] = [
'name' => $name,
'class' => $class,
'is_flushed' => Yii::$app->get($name)->flush(),
];
}
$this->notifyFlushed($cachesInfo);
}
/**
* Flushes all caches registered in the system.
*/
public function actionFlushAll()
{
$caches = $this->findCaches();
$cachesInfo = [];
if (empty($caches)) {
$this->notifyNoCachesFound();
return static::EXIT_CODE_NORMAL;
}
foreach ($caches as $name => $class) {
$cachesInfo[] = [
'name' => $name,
'class' => $class,
'is_flushed' => Yii::$app->get($name)->flush(),
];
}
$this->notifyFlushed($cachesInfo);
}
/**
* Notifies user that given caches are found and can be flushed.
* @param array $caches array of cache component classes
*/
private function notifyCachesCanBeFlushed($caches)
{
$this->stdout("The following caches were found in the system:\n\n", Console::FG_YELLOW);
foreach ($caches as $name => $class) {
$this->stdout("\t* $name ($class)\n", Console::FG_GREEN);
}
$this->stdout("\n");
}
/**
* Notifies user that there was not found any cache in the system.
*/
private function notifyNoCachesFound()
{
$this->stdout("No cache components were found in the system.\n", Console::FG_RED);
}
/**
* Notifies user that given cache components were not found in the system.
* @param array $cachesNames
*/
private function notifyNotFoundCaches($cachesNames)
{
$this->stdout("The following cache components were NOT found:\n\n", Console::FG_RED);
foreach ($cachesNames as $name) {
$this->stdout("\t * $name \n", Console::FG_GREEN);
}
$this->stdout("\n");
}
/**
*
* @param array $caches
*/
private function notifyFlushed($caches)
{
$this->stdout("The following cache components were processed:\n\n", Console::FG_YELLOW);
foreach ($caches as $cache) {
$this->stdout("\t* " . $cache['name'] ." (" . $cache['class'] . ")", Console::FG_GREEN);
if (!$cache['is_flushed']) {
$this->stdout(" - not flushed\n", Console::FG_RED);
} else {
$this->stdout("\n");
}
}
$this->stdout("\n");
}
/**
* Prompts user with confirmation if caches should be flushed.
* @param array $cachesNames
* @return boolean
*/
private function confirmFlush($cachesNames)
{
$this->stdout("The following cache components will be flushed:\n\n", Console::FG_YELLOW);
foreach ($cachesNames as $name) {
$this->stdout("\t * $name \n", Console::FG_GREEN);
}
return $this->confirm("\nFlush above cache components?");
}
/**
* Returns array of caches in the system, keys are cache components names, values are class names.
* @param array $cachesNames caches to be found
* @return array
*/
private function findCaches(array $cachesNames = [])
{
$caches = [];
$components = Yii::$app->getComponents();
$findAll = ($cachesNames == []);
foreach ($components as $name => $component) {
if (!$findAll && !in_array($name, $cachesNames)) {
continue;
}
if ($component instanceof Cache) {
$caches[$name] = get_class($component);
} elseif (is_array($component) && isset($component['class']) && $this->isCacheClass($component['class'])) {
$caches[$name] = $component['class'];
} elseif (is_string($component) && $this->isCacheClass($component)) {
$caches[$name] = $component;
}
}
return $caches;
}
/**
* Checks if given class is a Cache class.
* @param string $className class name.
* @return boolean
*/
private function isCacheClass($className)
{
return is_subclass_of($className, Cache::className());
}
}