1 | <?php |
||
45 | class CacheController extends Controller |
||
46 | { |
||
47 | /** |
||
48 | * Lists the caches that can be flushed. |
||
49 | */ |
||
50 | public function actionIndex() |
||
60 | |||
61 | /** |
||
62 | * Flushes given cache components. |
||
63 | * For example, |
||
64 | * |
||
65 | * ``` |
||
66 | * # flushes caches specified by their id: "first", "second", "third" |
||
67 | * yii cache/flush first second third |
||
68 | * ``` |
||
69 | * |
||
70 | */ |
||
71 | public function actionFlush() |
||
72 | { |
||
73 | $cachesInput = func_get_args(); |
||
74 | |||
75 | if (empty($cachesInput)) { |
||
76 | throw new Exception('You should specify cache components names'); |
||
77 | } |
||
78 | |||
79 | $caches = $this->findCaches($cachesInput); |
||
80 | $cachesInfo = []; |
||
81 | |||
82 | $foundCaches = array_keys($caches); |
||
83 | $notFoundCaches = array_diff($cachesInput, array_keys($caches)); |
||
84 | |||
85 | if ($notFoundCaches) { |
||
|
|||
86 | $this->notifyNotFoundCaches($notFoundCaches); |
||
87 | } |
||
88 | |||
89 | if (!$foundCaches) { |
||
90 | $this->notifyNoCachesFound(); |
||
91 | return static::EXIT_CODE_NORMAL; |
||
92 | } |
||
93 | |||
94 | if (!$this->confirmFlush($foundCaches)) { |
||
95 | return static::EXIT_CODE_NORMAL; |
||
96 | } |
||
97 | |||
98 | foreach ($caches as $name => $class) { |
||
99 | $cachesInfo[] = [ |
||
100 | 'name' => $name, |
||
101 | 'class' => $class, |
||
102 | 'is_flushed' => Yii::$app->get($name)->flush(), |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | $this->notifyFlushed($cachesInfo); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Flushes all caches registered in the system. |
||
111 | */ |
||
112 | public function actionFlushAll() |
||
113 | { |
||
114 | $caches = $this->findCaches(); |
||
115 | $cachesInfo = []; |
||
116 | |||
117 | if (empty($caches)) { |
||
118 | $this->notifyNoCachesFound(); |
||
119 | return static::EXIT_CODE_NORMAL; |
||
120 | } |
||
121 | |||
122 | foreach ($caches as $name => $class) { |
||
123 | $cachesInfo[] = [ |
||
124 | 'name' => $name, |
||
125 | 'class' => $class, |
||
126 | 'is_flushed' => Yii::$app->get($name)->flush(), |
||
127 | ]; |
||
128 | } |
||
129 | |||
130 | $this->notifyFlushed($cachesInfo); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Clears DB schema cache for a given connection component. |
||
135 | * |
||
136 | * ``` |
||
137 | * # clears cache schema specified by component id: "db" |
||
138 | * yii cache/flush-schema db |
||
139 | * ``` |
||
140 | * |
||
141 | * @param string $db id connection component |
||
142 | * @return int exit code |
||
143 | * @throws Exception |
||
144 | * @throws \yii\base\InvalidConfigException |
||
145 | * |
||
146 | * @since 2.0.1 |
||
147 | */ |
||
148 | public function actionFlushSchema($db = 'db') |
||
149 | { |
||
150 | $connection = Yii::$app->get($db, false); |
||
151 | if ($connection === null) { |
||
152 | $this->stdout("Unknown component \"$db\".\n", Console::FG_RED); |
||
153 | return self::EXIT_CODE_ERROR; |
||
154 | } |
||
155 | |||
156 | if (!$connection instanceof \yii\db\Connection) { |
||
157 | $this->stdout("\"$db\" component doesn't inherit \\yii\\db\\Connection.\n", Console::FG_RED); |
||
158 | return self::EXIT_CODE_ERROR; |
||
159 | } elseif (!$this->confirm("Flush cache schema for \"$db\" connection?")) { |
||
160 | return static::EXIT_CODE_NORMAL; |
||
161 | } |
||
162 | |||
163 | try { |
||
164 | $schema = $connection->getSchema(); |
||
165 | $schema->refresh(); |
||
166 | $this->stdout("Schema cache for component \"$db\", was flushed.\n\n", Console::FG_GREEN); |
||
167 | } catch (\Exception $e) { |
||
168 | $this->stdout($e->getMessage() . "\n\n", Console::FG_RED); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Notifies user that given caches are found and can be flushed. |
||
174 | * @param array $caches array of cache component classes |
||
175 | */ |
||
176 | private function notifyCachesCanBeFlushed($caches) |
||
186 | |||
187 | /** |
||
188 | * Notifies user that there was not found any cache in the system. |
||
189 | */ |
||
190 | private function notifyNoCachesFound() |
||
194 | |||
195 | /** |
||
196 | * Notifies user that given cache components were not found in the system. |
||
197 | * @param array $cachesNames |
||
198 | */ |
||
199 | private function notifyNotFoundCaches($cachesNames) |
||
209 | |||
210 | /** |
||
211 | * |
||
212 | * @param array $caches |
||
213 | */ |
||
214 | private function notifyFlushed($caches) |
||
230 | |||
231 | /** |
||
232 | * Prompts user with confirmation if caches should be flushed. |
||
233 | * @param array $cachesNames |
||
234 | * @return bool |
||
235 | */ |
||
236 | private function confirmFlush($cachesNames) |
||
246 | |||
247 | /** |
||
248 | * Returns array of caches in the system, keys are cache components names, values are class names. |
||
249 | * @param array $cachesNames caches to be found |
||
250 | * @return array |
||
251 | */ |
||
252 | private function findCaches(array $cachesNames = []) |
||
274 | |||
275 | /** |
||
276 | * Checks if given class is a Cache class. |
||
277 | * @param string $className class name. |
||
278 | * @return bool |
||
279 | */ |
||
280 | private function isCacheClass($className) |
||
284 | } |
||
285 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.