CacheController.php 7.81 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
 * see list of available components to flush:
20
 *
21
 *     yii cache
22
 *
23 24 25 26 27 28 29
 * 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
30
 *
31 32 33 34
 * Note that the command uses cache components defined in your console application configuration file. If components
 * configured are different from web application, web application cache won't be cleared. In order to fix it please
 * duplicate web application cache components in console config. You can use any component names.
 *
35
 * @author Alexander Makarov <sam@rmcreative.ru>
36
 * @author Mark Jebri <mark.github@yandex.ru>
37 38 39 40
 * @since 2.0
 */
class CacheController extends Controller
{
41 42 43 44 45
    /**
     * Lists the caches that can be flushed.
     */
    public function actionIndex()
    {
46 47
        $caches = $this->findCaches();

48
        if (!empty($caches)) {
49
            $this->notifyCachesCanBeFlushed($caches);
50
        } else {
51
            $this->notifyNoCachesFound();
52 53
        }
    }
54

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

83 84 85
        if (!$foundCaches) {
            $this->notifyNoCachesFound();
            return static::EXIT_CODE_NORMAL;
86
        }
87

88 89 90 91 92 93 94 95 96 97 98 99 100
        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);
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

    /**
     * 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);
    }

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
    /**
     * Clears DB schema cache for a given connection component.
     *
     * ~~~
     * # clears cache schema specified by component id: "db"
     * yii cache/flush-schema db
     * ~~~
     *
     * @param string $db id connection component
     * @return int exit code
     * @throws Exception
     * @throws \yii\base\InvalidConfigException
     *
     * @since 2.0.1
     */
    public function actionFlushSchema($db = 'db')
    {
        $connection = Yii::$app->get($db, false);
        if ($connection === null) {
            $this->stdout("Unknown component \"$db\".\n", Console::FG_RED);
            return self::EXIT_CODE_ERROR;
        }

        if (!$connection instanceof \yii\db\Connection) {
            $this->stdout("\"$db\" component doesn't inherit \\yii\\db\\Connection.\n", Console::FG_RED);
            return self::EXIT_CODE_ERROR;
        } else if (!$this->confirm("Flush cache schema for \"$db\" connection?")) {
            return static::EXIT_CODE_NORMAL;
        }

        try {
            $schema = $connection->getSchema();
            $schema->refresh();
            $this->stdout("Schema cache for component \"$db\", was flushed.\n\n", Console::FG_GREEN);
        } catch (\Exception $e) {
            $this->stdout($e->getMessage() . "\n\n", Console::FG_RED);
        }
    }

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    /**
     * 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);
259
            } elseif (is_array($component) && isset($component['class']) && $this->isCacheClass($component['class'])) {
260
                $caches[$name] = $component['class'];
261
            } elseif (is_string($component) && $this->isCacheClass($component)) {
262 263 264 265 266 267 268
                $caches[$name] = $component;
            }
        }

        return $caches;
    }

269 270 271 272 273 274 275 276 277 278
    /**
     * 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());
    }

279
}