Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clear-caches console command #3588

Merged
merged 14 commits into from
Dec 28, 2018
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Added
- Added the `clear-caches` console command. ([#3588](https://github.com/craftcms/cms/pull/3588))

### Changed
- Pressing the <kbd>Return</kbd> key (or <kbd>Ctrl</kbd>/<kbd>Command</kbd> + <kbd>Return</kbd>) when a textual cell is focused in an editable table will now change the focus to the same cell in the next row (after creating a new row if necessary.) ([#3576](https://github.com/craftcms/cms/issues/3576))

Expand Down
87 changes: 87 additions & 0 deletions src/console/actions/ClearCacheAction.php
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. <support@pixelandtonic.com>
* @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;
}
}
170 changes: 170 additions & 0 deletions src/console/controllers/ClearCachesController.php
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. <support@pixelandtonic.com>
* @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()
{
}
}