CacheController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console\controllers;
  8. use Yii;
  9. use yii\caching\ApcCache;
  10. use yii\console\Controller;
  11. use yii\caching\Cache;
  12. use yii\helpers\Console;
  13. use yii\console\Exception;
  14. /**
  15. * Allows you to flush cache.
  16. *
  17. * see list of available components to flush:
  18. *
  19. * yii cache
  20. *
  21. * flush particular components specified by their names:
  22. *
  23. * yii cache/flush first second third
  24. *
  25. * flush all cache components that can be found in the system
  26. *
  27. * yii cache/flush-all
  28. *
  29. * Note that the command uses cache components defined in your console application configuration file. If components
  30. * configured are different from web application, web application cache won't be cleared. In order to fix it please
  31. * duplicate web application cache components in console config. You can use any component names.
  32. *
  33. * APC is not shared between PHP processes so flushing cache from command line has no effect on web.
  34. * Flushing web cache could be either done by:
  35. *
  36. * - Putting a php file under web root and calling it via HTTP
  37. * - Using [Cachetool](http://gordalina.github.io/cachetool/)
  38. *
  39. * @author Alexander Makarov <sam@rmcreative.ru>
  40. * @author Mark Jebri <mark.github@yandex.ru>
  41. * @since 2.0
  42. */
  43. class CacheController extends Controller
  44. {
  45. /**
  46. * Lists the caches that can be flushed.
  47. */
  48. public function actionIndex()
  49. {
  50. $caches = $this->findCaches();
  51. if (!empty($caches)) {
  52. $this->notifyCachesCanBeFlushed($caches);
  53. } else {
  54. $this->notifyNoCachesFound();
  55. }
  56. }
  57. /**
  58. * Flushes given cache components.
  59. * For example,
  60. *
  61. * ```
  62. * # flushes caches specified by their id: "first", "second", "third"
  63. * yii cache/flush first second third
  64. * ```
  65. *
  66. */
  67. public function actionFlush()
  68. {
  69. $cachesInput = func_get_args();
  70. if (empty($cachesInput)) {
  71. throw new Exception('You should specify cache components names');
  72. }
  73. $caches = $this->findCaches($cachesInput);
  74. $cachesInfo = [];
  75. $foundCaches = array_keys($caches);
  76. $notFoundCaches = array_diff($cachesInput, array_keys($caches));
  77. if ($notFoundCaches) {
  78. $this->notifyNotFoundCaches($notFoundCaches);
  79. }
  80. if (!$foundCaches) {
  81. $this->notifyNoCachesFound();
  82. return static::EXIT_CODE_NORMAL;
  83. }
  84. if (!$this->confirmFlush($foundCaches)) {
  85. return static::EXIT_CODE_NORMAL;
  86. }
  87. foreach ($caches as $name => $class) {
  88. $cachesInfo[] = [
  89. 'name' => $name,
  90. 'class' => $class,
  91. 'is_flushed' => $this->canBeFlushed($class) ? Yii::$app->get($name)->flush() : false,
  92. ];
  93. }
  94. $this->notifyFlushed($cachesInfo);
  95. }
  96. /**
  97. * Flushes all caches registered in the system.
  98. */
  99. public function actionFlushAll()
  100. {
  101. $caches = $this->findCaches();
  102. $cachesInfo = [];
  103. if (empty($caches)) {
  104. $this->notifyNoCachesFound();
  105. return static::EXIT_CODE_NORMAL;
  106. }
  107. foreach ($caches as $name => $class) {
  108. $cachesInfo[] = [
  109. 'name' => $name,
  110. 'class' => $class,
  111. 'is_flushed' => $this->canBeFlushed($class) ? Yii::$app->get($name)->flush() : false,
  112. ];
  113. }
  114. $this->notifyFlushed($cachesInfo);
  115. }
  116. /**
  117. * Clears DB schema cache for a given connection component.
  118. *
  119. * ```
  120. * # clears cache schema specified by component id: "db"
  121. * yii cache/flush-schema db
  122. * ```
  123. *
  124. * @param string $db id connection component
  125. * @return int exit code
  126. * @throws Exception
  127. * @throws \yii\base\InvalidConfigException
  128. *
  129. * @since 2.0.1
  130. */
  131. public function actionFlushSchema($db = 'db')
  132. {
  133. $connection = Yii::$app->get($db, false);
  134. if ($connection === null) {
  135. $this->stdout("Unknown component \"$db\".\n", Console::FG_RED);
  136. return self::EXIT_CODE_ERROR;
  137. }
  138. if (!$connection instanceof \yii\db\Connection) {
  139. $this->stdout("\"$db\" component doesn't inherit \\yii\\db\\Connection.\n", Console::FG_RED);
  140. return self::EXIT_CODE_ERROR;
  141. } elseif (!$this->confirm("Flush cache schema for \"$db\" connection?")) {
  142. return static::EXIT_CODE_NORMAL;
  143. }
  144. try {
  145. $schema = $connection->getSchema();
  146. $schema->refresh();
  147. $this->stdout("Schema cache for component \"$db\", was flushed.\n\n", Console::FG_GREEN);
  148. } catch (\Exception $e) {
  149. $this->stdout($e->getMessage() . "\n\n", Console::FG_RED);
  150. }
  151. }
  152. /**
  153. * Notifies user that given caches are found and can be flushed.
  154. * @param array $caches array of cache component classes
  155. */
  156. private function notifyCachesCanBeFlushed($caches)
  157. {
  158. $this->stdout("The following caches were found in the system:\n\n", Console::FG_YELLOW);
  159. foreach ($caches as $name => $class) {
  160. if ($this->canBeFlushed($class)) {
  161. $this->stdout("\t* $name ($class)\n", Console::FG_GREEN);
  162. } else {
  163. $this->stdout("\t* $name ($class) - can not be flushed via console\n", Console::FG_YELLOW);
  164. }
  165. }
  166. $this->stdout("\n");
  167. }
  168. /**
  169. * Notifies user that there was not found any cache in the system.
  170. */
  171. private function notifyNoCachesFound()
  172. {
  173. $this->stdout("No cache components were found in the system.\n", Console::FG_RED);
  174. }
  175. /**
  176. * Notifies user that given cache components were not found in the system.
  177. * @param array $cachesNames
  178. */
  179. private function notifyNotFoundCaches($cachesNames)
  180. {
  181. $this->stdout("The following cache components were NOT found:\n\n", Console::FG_RED);
  182. foreach ($cachesNames as $name) {
  183. $this->stdout("\t* $name \n", Console::FG_GREEN);
  184. }
  185. $this->stdout("\n");
  186. }
  187. /**
  188. *
  189. * @param array $caches
  190. */
  191. private function notifyFlushed($caches)
  192. {
  193. $this->stdout("The following cache components were processed:\n\n", Console::FG_YELLOW);
  194. foreach ($caches as $cache) {
  195. $this->stdout("\t* " . $cache['name'] .' (' . $cache['class'] . ')', Console::FG_GREEN);
  196. if (!$cache['is_flushed']) {
  197. $this->stdout(" - not flushed\n", Console::FG_RED);
  198. } else {
  199. $this->stdout("\n");
  200. }
  201. }
  202. $this->stdout("\n");
  203. }
  204. /**
  205. * Prompts user with confirmation if caches should be flushed.
  206. * @param array $cachesNames
  207. * @return bool
  208. */
  209. private function confirmFlush($cachesNames)
  210. {
  211. $this->stdout("The following cache components will be flushed:\n\n", Console::FG_YELLOW);
  212. foreach ($cachesNames as $name) {
  213. $this->stdout("\t* $name \n", Console::FG_GREEN);
  214. }
  215. return $this->confirm("\nFlush above cache components?");
  216. }
  217. /**
  218. * Returns array of caches in the system, keys are cache components names, values are class names.
  219. * @param array $cachesNames caches to be found
  220. * @return array
  221. */
  222. private function findCaches(array $cachesNames = [])
  223. {
  224. $caches = [];
  225. $components = Yii::$app->getComponents();
  226. $findAll = ($cachesNames === []);
  227. foreach ($components as $name => $component) {
  228. if (!$findAll && !in_array($name, $cachesNames, true)) {
  229. continue;
  230. }
  231. if ($component instanceof Cache) {
  232. $caches[$name] = get_class($component);
  233. } elseif (is_array($component) && isset($component['class']) && $this->isCacheClass($component['class'])) {
  234. $caches[$name] = $component['class'];
  235. } elseif (is_string($component) && $this->isCacheClass($component)) {
  236. $caches[$name] = $component;
  237. }
  238. }
  239. return $caches;
  240. }
  241. /**
  242. * Checks if given class is a Cache class.
  243. * @param string $className class name.
  244. * @return bool
  245. */
  246. private function isCacheClass($className)
  247. {
  248. return is_subclass_of($className, Cache::className());
  249. }
  250. /**
  251. * Checks if cache of a certain class can be flushed
  252. * @param string $className class name.
  253. * @return bool
  254. */
  255. private function canBeFlushed($className)
  256. {
  257. return !is_a($className, ApcCache::className(), true) || php_sapi_name() !== "cli";
  258. }
  259. }