-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3588 from nystudio107/develop
Add clear-caches console command
- Loading branch information
Showing
3 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\console\actions; | ||
|
||
use Craft; | ||
use craft\helpers\FileHelper; | ||
use yii\base\Action; | ||
use yii\base\InvalidArgumentException; | ||
use yii\console\Controller; | ||
use yii\console\ExitCode; | ||
use yii\helpers\Console; | ||
|
||
/** | ||
* @inheritdoc | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.0.37 | ||
*/ | ||
class ClearCacheAction extends Action | ||
{ | ||
// Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var string|callable | ||
*/ | ||
public $action; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $label; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $params; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
* @return int | ||
*/ | ||
public function run(): int | ||
{ | ||
$label = Console::ansiFormat(Craft::t('app', 'Clearing cache:'), [Console::FG_GREEN]); | ||
$name = Console::ansiFormat($this->label, [Console::FG_YELLOW]); | ||
Console::output("{$label} {$name}"); | ||
|
||
if (is_string($this->action)) { | ||
try { | ||
FileHelper::clearDirectory($this->action); | ||
} catch (InvalidArgumentException $e) { | ||
// the directory doesn't exist | ||
} catch (\Throwable $e) { | ||
$error = "Could not clear the directory {$this->label}: " . $e->getMessage(); | ||
Console::error(Console::ansiFormat($error, [Console::FG_RED])); | ||
Craft::warning($error, __METHOD__); | ||
} | ||
} else if (isset($this->params)) { | ||
try { | ||
call_user_func_array($this->action, $this->params); | ||
} catch (\Throwable $e) { | ||
$error = "Error clearing cache {$this->label}: " . $e->getMessage(); | ||
Console::error(Console::ansiFormat($error, [Console::FG_RED])); | ||
Craft::warning($error, __METHOD__); | ||
} | ||
} else { | ||
try { | ||
$action = $this->action; | ||
$action(); | ||
} catch (\Throwable $e) { | ||
$error = "Error clearing cache {$this->label}: " . $e->getMessage(); | ||
Console::error(Console::ansiFormat($error, [Console::FG_RED])); | ||
Craft::warning($error, __METHOD__); | ||
} | ||
} | ||
|
||
return ExitCode::OK; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\console\controllers; | ||
|
||
use craft\console\actions\ClearCacheAction; | ||
use craft\helpers\Console; | ||
use craft\helpers\FileHelper; | ||
use craft\utilities\ClearCaches; | ||
use yii\base\InvalidRouteException; | ||
use yii\console\Controller; | ||
use yii\console\Exception; | ||
use yii\console\ExitCode; | ||
|
||
/** | ||
* Clear caches via the CLI | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.0.37 | ||
*/ | ||
class ClearCachesController extends Controller | ||
{ | ||
// Properties | ||
// ========================================================================= | ||
|
||
public $allowAnonymous = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $_actions = []; | ||
|
||
/** | ||
* @var \Reflection | ||
*/ | ||
private $_dummyReflection; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
// Set up the actions array | ||
$cacheOptions = ClearCaches::cacheOptions(); | ||
foreach ($cacheOptions as $cacheOption) { | ||
$this->_actions[$cacheOption['key']] = [ | ||
'class' => ClearCacheAction::class, | ||
'action' => $cacheOption['action'], | ||
'label' => $cacheOption['label'], | ||
'params' => $cacheOption['params'] ?? null, | ||
'controller' => $this, | ||
]; | ||
} | ||
// Set up a reflection for this class to handle closures | ||
$this->_dummyReflection = new \ReflectionMethod($this, 'dummyMethod'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function actions() | ||
{ | ||
return $this->_actions; | ||
} | ||
|
||
/** | ||
* Lists the caches that can be cleared. | ||
* | ||
* @return int | ||
*/ | ||
public function actionIndex(): int | ||
{ | ||
$this->stdout("The following caches can be cleared:\n\n", Console::FG_YELLOW); | ||
|
||
$lengths = []; | ||
foreach ($this->_actions as $action) { | ||
$lengths[] = strlen($action['label']); | ||
} | ||
$maxLength = max($lengths); | ||
|
||
foreach ($this->_actions as $id => $action) { | ||
$this->stdout('- '); | ||
$this->stdout(str_pad($id, $maxLength, ' '), Console::FG_YELLOW); | ||
$this->stdout(' ' . $action['label'] . PHP_EOL); | ||
} | ||
|
||
$this->stdout(PHP_EOL); | ||
return ExitCode::OK; | ||
} | ||
|
||
/** | ||
* Clear all caches | ||
* | ||
* @return int | ||
* @throws InvalidRouteException | ||
* @throws Exception | ||
*/ | ||
public function actionAll(): int | ||
{ | ||
foreach ($this->_actions as $id => $action) { | ||
$this->runAction($id); | ||
} | ||
return ExitCode::OK; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getActionHelpSummary($action) | ||
{ | ||
$help = parent::getActionHelpSummary($action); | ||
if (empty($help) && array_key_exists($action->id, $this->_actions)) { | ||
$help = $this->_actions[$action->id]['label']; | ||
} | ||
|
||
return $help; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getActionHelp($action) | ||
{ | ||
$help = parent::getActionHelp($action); | ||
if (empty($help) && array_key_exists($action->id, $this->_actions)) { | ||
$help = $this->_actions[$action->id]['label']; | ||
} | ||
|
||
return $help; | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function getActionMethodReflection($action) | ||
{ | ||
if (array_key_exists($action->id, $this->_actions)) { | ||
if (is_string($this->_actions[$action->id]['action'])) { | ||
return new \ReflectionMethod(FileHelper::class, 'clearDirectory'); | ||
} else { | ||
if (is_array($this->_actions[$action->id]['action'])) { | ||
return new \ReflectionMethod( | ||
$this->_actions[$action->id]['action'][0], | ||
$this->_actions[$action->id]['action'][1] | ||
); | ||
} else { | ||
return $this->_dummyReflection; | ||
} | ||
} | ||
} | ||
|
||
return parent::getActionMethodReflection($action); | ||
} | ||
|
||
protected function dummyMethod() | ||
{ | ||
} | ||
} |