diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php
index 9da3974a8..223693214 100644
--- a/system/src/Grav/Common/Cache.php
+++ b/system/src/Grav/Common/Cache.php
@@ -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[] = 'Cleared: ' . $path . '*';
+ }
+ }
+
+ $output[] = '';
+
+ if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
+ touch($user_config);
+
+ $output[] = 'Touched: ' . $user_config;
+ $output[] = '';
+ }
+
+ return $output;
+ }
}
diff --git a/system/src/Grav/Console/Cli/ClearCacheCommand.php b/system/src/Grav/Console/Cli/ClearCacheCommand.php
index 148d24634..de48ca0d0 100644
--- a/system/src/Grav/Console/Cli/ClearCacheCommand.php
+++ b/system/src/Grav/Console/Cli/ClearCacheCommand.php
@@ -1,6 +1,8 @@
writeln('Clearing cache');
$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('Cleared: ' . $path . '*');
- }
+ foreach (Cache::clearCache($remove) as $result) {
+ $output->writeln($result);
}
-
- if (file_exists($user_config)) {
- touch($user_config);
- $output->writeln('');
- $output->writeln('Touched: ' . $user_config);
- $output->writeln('');
- }
-
- if (!$anything) {
- $output->writeln('Nothing to clear...');
- $output->writeln('');
- }
-
}
}