mirror of
https://github.com/getgrav/grav.git
synced 2026-02-22 22:51:13 +01:00
moved clear-cache functionality into Cache object
This commit is contained in:
@@ -3,6 +3,7 @@ namespace Grav\Common;
|
||||
|
||||
use \Doctrine\Common\Cache\Cache as DoctrineCache;
|
||||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
|
||||
/**
|
||||
* The GravCache object is used throughout Grav to store and retrieve cached data.
|
||||
@@ -37,6 +38,33 @@ class Cache extends Getters
|
||||
|
||||
protected $cache_dir;
|
||||
|
||||
protected static $standard_remove = [
|
||||
'cache/twig/',
|
||||
'cache/doctrine/',
|
||||
'cache/compiled/',
|
||||
'cache/validated-',
|
||||
'images/',
|
||||
'assets/',
|
||||
];
|
||||
|
||||
protected static $all_remove = [
|
||||
'cache/',
|
||||
'images/',
|
||||
'assets/'
|
||||
];
|
||||
|
||||
protected static $assets_remove = [
|
||||
'assets/'
|
||||
];
|
||||
|
||||
protected static $images_remove = [
|
||||
'images/'
|
||||
];
|
||||
|
||||
protected static $cache_remove = [
|
||||
'cache/'
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -169,4 +197,70 @@ class Cache extends Getters
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to clear all Grav caches
|
||||
*
|
||||
* @param string $remove standard|all|assets-only|images-only|cache-only
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function clearCache($remove = 'standard')
|
||||
{
|
||||
|
||||
$output = [];
|
||||
$user_config = USER_DIR . 'config/system.yaml';
|
||||
|
||||
switch($remove) {
|
||||
case 'all':
|
||||
$remove_paths = self::$all_remove;
|
||||
break;
|
||||
case 'assets-only':
|
||||
$remove_paths = self::$assets_remove;
|
||||
break;
|
||||
case 'images-only':
|
||||
$remove_paths = self::$images_remove;
|
||||
break;
|
||||
case 'cache-only':
|
||||
$remove_paths = self::$cache_remove;
|
||||
break;
|
||||
default:
|
||||
$remove_paths = self::$standard_remove;
|
||||
}
|
||||
|
||||
|
||||
foreach ($remove_paths as $path) {
|
||||
|
||||
$anything = false;
|
||||
$files = glob(ROOT_DIR . $path . '*');
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file)) {
|
||||
if (@unlink($file)) {
|
||||
$anything = true;
|
||||
}
|
||||
} elseif (is_dir($file)) {
|
||||
if (@Folder::delete($file)) {
|
||||
$anything = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($anything) {
|
||||
$output[] = '<red>Cleared: </red>' . $path . '*';
|
||||
}
|
||||
}
|
||||
|
||||
$output[] = '';
|
||||
|
||||
if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
|
||||
touch($user_config);
|
||||
|
||||
$output[] = '<red>Touched: </red>' . $user_config;
|
||||
$output[] = '';
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Grav\Console\Cli;
|
||||
|
||||
use Grav\Console\ConsoleTrait;
|
||||
use Grav\Common\Cache;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
@@ -17,33 +19,6 @@ use Symfony\Component\Yaml\Yaml;
|
||||
class ClearCacheCommand extends Command
|
||||
{
|
||||
|
||||
protected $standard_remove = [
|
||||
'cache/twig/',
|
||||
'cache/doctrine/',
|
||||
'cache/compiled/',
|
||||
'cache/validated-',
|
||||
'images/',
|
||||
'assets/',
|
||||
];
|
||||
|
||||
protected $all_remove = [
|
||||
'cache/',
|
||||
'images/',
|
||||
'assets/'
|
||||
];
|
||||
|
||||
protected $assets_remove = [
|
||||
'assets/'
|
||||
];
|
||||
|
||||
protected $images_remove = [
|
||||
'images/'
|
||||
];
|
||||
|
||||
protected $cache_remove = [
|
||||
'cache/'
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -87,55 +62,21 @@ class ClearCacheCommand extends Command
|
||||
$output->writeln('<magenta>Clearing cache</magenta>');
|
||||
$output->writeln('');
|
||||
|
||||
$user_config = USER_DIR . 'config/system.yaml';
|
||||
|
||||
$anything = false;
|
||||
|
||||
if ($input->getOption('all')) {
|
||||
$remove_paths = $this->all_remove;
|
||||
$remove = 'all';
|
||||
} elseif ($input->getOption('assets-only')) {
|
||||
$remove_paths = $this->assets_remove;
|
||||
$remove = 'assets-only';
|
||||
} elseif ($input->getOption('images-only')) {
|
||||
$remove_paths = $this->images_remove;
|
||||
$remove = 'images-only';
|
||||
} elseif ($input->getOption('cache-only')) {
|
||||
$remove_paths = $this->cache_remove;
|
||||
$remove = 'cache-only';
|
||||
} else {
|
||||
$remove_paths = $this->standard_remove;
|
||||
$remove = 'standard';
|
||||
}
|
||||
|
||||
foreach ($remove_paths as $path) {
|
||||
|
||||
$files = glob(ROOT_DIR . $path . '*');
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file)) {
|
||||
if (@unlink($file)) {
|
||||
$anything = true;
|
||||
}
|
||||
} elseif (is_dir($file)) {
|
||||
if (@Folder::delete($file)) {
|
||||
$anything = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($anything) {
|
||||
$output->writeln('<red>Cleared: </red>' . $path . '*');
|
||||
}
|
||||
foreach (Cache::clearCache($remove) as $result) {
|
||||
$output->writeln($result);
|
||||
}
|
||||
|
||||
if (file_exists($user_config)) {
|
||||
touch($user_config);
|
||||
$output->writeln('');
|
||||
$output->writeln('<red>Touched: </red>' . $user_config);
|
||||
$output->writeln('');
|
||||
}
|
||||
|
||||
if (!$anything) {
|
||||
$output->writeln('<green>Nothing to clear...</green>');
|
||||
$output->writeln('');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user