CacheController.php 6.19 KB
Newer Older
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console\controllers;

Qiang Xue committed
10
use Yii;
11 12
use yii\console\Controller;
use yii\caching\Cache;
13 14
use yii\helpers\Console;
use yii\console\Exception;
15 16

/**
17
 * Allows you to flush cache.
18
 *
19 20 21
 * ~~~
 * #see list of available components to flush
 * yii cache
22
 *
23 24
 * #flush particular components specified by their names
 * yii cache/flush first second third
25
 *
26 27 28
 * #flush all cache components that can be found in the system
 * yii cache/flush-all
 * ~~~
29
 *
30
 * @author Alexander Makarov <sam@rmcreative.ru>
31
 * @author Mark Jebri <mark.github@yandex.ru>
32 33 34 35
 * @since 2.0
 */
class CacheController extends Controller
{
36 37 38 39 40
    /**
     * Lists the caches that can be flushed.
     */
    public function actionIndex()
    {
41 42
        $caches = $this->findCaches();

43
        if (!empty($caches)) {
44
            $this->notifyCachesCanBeFlushed($caches);
45
        } else {
46
            $this->notifyNoCachesFound();
47 48
        }
    }
49

50
    /**
51 52
     * Flushes given cache components.
     * For example,
53
     *
54 55 56 57 58
     * ~~~
     * # flushes caches specified by their id: "first", "second", "third"
     * yii cache/flush first second third
     * ~~~
     * 
59
     */
60
    public function actionFlush()
61
    {
62 63 64 65 66 67 68 69 70 71 72 73 74 75
        $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);
76
        }
77

78 79 80
        if (!$foundCaches) {
            $this->notifyNoCachesFound();
            return static::EXIT_CODE_NORMAL;
81
        }
82

83 84 85 86 87 88 89 90 91 92 93 94 95
        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);
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

    /**
     * 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);
215
            } elseif (is_array($component) && isset($component['class']) && $this->isCacheClass($component['class'])) {
216
                $caches[$name] = $component['class'];
217
            } elseif (is_string($component) && $this->isCacheClass($component)) {
218 219 220 221 222 223 224
                $caches[$name] = $component;
            }
        }

        return $caches;
    }

225 226 227 228 229 230 231 232 233 234
    /**
     * 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());
    }

235
}