You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					68 lines
				
				1.6 KiB
			
		
		
			
		
	
	
					68 lines
				
				1.6 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											13 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\console\controllers;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\console\Controller;
 | ||
|  | use yii\console\Exception;
 | ||
|  | use yii\caching\Cache;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * This command allows you to flush cache.
 | ||
|  |  *
 | ||
|  |  * @author Alexander Makarov <sam@rmcreative.ru>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class CacheController extends Controller
 | ||
|  | {
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Lists the caches that can be flushed.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function actionIndex()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$caches = array();
 | ||
|  | 		$components = Yii::$app->getComponents();
 | ||
|  | 		foreach ($components as $name => $component) {
 | ||
|  | 			if ($component instanceof Cache) {
 | ||
|  | 				$caches[$name] = get_class($component);
 | ||
|  | 			} elseif (is_array($component) && isset($component['class']) && strpos($component['class'], 'Cache') !== false) {
 | ||
|  | 				$caches[$name] = $component['class'];
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		if (!empty($caches)) {
 | ||
|  | 			echo "The following caches can be flushed:\n\n";
 | ||
|  | 			foreach ($caches as $name => $class) {
 | ||
|  | 				echo " * $name: $class\n";
 | ||
|  | 			}
 | ||
|  | 		} else {
 | ||
|  | 			echo "No cache is used.\n";
 | ||
|  | 		}
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Flushes cache.
 | ||
|  | 	 * @param string $component Name of the cache application component to use.
 | ||
|  | 	 *
 | ||
|  | 	 * @throws \yii\console\Exception
 | ||
|  | 	 */
 | ||
|  | 	public function actionFlush($component = 'cache')
 | ||
|  | 	{
 | ||
|  | 		/** @var $cache Cache */
 | ||
| 
											13 years ago
										 | 		$cache = Yii::$app->getComponent($component);
 | ||
| 
											13 years ago
										 | 		if (!$cache || !$cache instanceof Cache) {
 | ||
| 
											13 years ago
										 | 			throw new Exception('Application component "'.$component.'" is not defined or not a cache.');
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if (!$cache->flush()) {
 | ||
| 
											13 years ago
										 | 			throw new Exception('Unable to flush cache.');
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		echo "\nDone.\n";
 | ||
|  | 	}
 | ||
|  | }
 |