From dfd343dff64a8c43d8dab274073e41ddb9269bec Mon Sep 17 00:00:00 2001 From: Djamil Legato Date: Tue, 12 Aug 2014 16:43:59 -0700 Subject: [PATCH] Added new clear-cache command --- bin/grav | 3 +- system/src/Grav/Console/ClearCacheCommand.php | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 system/src/Grav/Console/ClearCacheCommand.php diff --git a/bin/grav b/bin/grav index ba8851bca..c33e0ba35 100755 --- a/bin/grav +++ b/bin/grav @@ -18,6 +18,7 @@ $app->addCommands(array( new Grav\Console\InstallCommand(), new Grav\Console\SetupCommand(), new Grav\Console\CleanCommand(), - new Grav\Console\NewCommand(), + new Grav\Console\ClearCacheCommand(), + new Grav\Console\NewProjectCommand(), )); $app->run(); diff --git a/system/src/Grav/Console/ClearCacheCommand.php b/system/src/Grav/Console/ClearCacheCommand.php new file mode 100644 index 000000000..226be5ad0 --- /dev/null +++ b/system/src/Grav/Console/ClearCacheCommand.php @@ -0,0 +1,82 @@ +setName("clear-cache") + ->setDescription("Clears Grav cache") + ->setHelp('The clear-cache deletes all cache files'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + + + // Create a red output option + $output->getFormatter()->setStyle('red', new OutputFormatterStyle('red')); + $output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan')); + $output->getFormatter()->setStyle('green', new OutputFormatterStyle('green')); + $output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta')); + + $this->cleanPaths($output); + + + } + + // loops over the array of paths and deletes the files/folders + private function cleanPaths($output) + { + $output->writeln(''); + $output->writeln('Clearing cache'); + $output->writeln(''); + + $anything = false; + + foreach($this->paths_to_remove as $path) { + $files = glob(ROOT_DIR . rtrim($path, '/') . '/*'); + + foreach ($files as $file) { + if (is_file($file) && @unlink($file)) $anything = true; + elseif (is_dir($file) && @$this->rrmdir($file)) $anything = true; + } + + if ($anything) $output->writeln('Cleared: ' . $path . '*'); + } + + if (!$anything) { + $output->writeln('Nothing to clear...'); + $output->writeln(''); + } + + } + + // Recursively Delete folder - DANGEROUS! USE WITH CARE!!!! + private function rrmdir($dir) { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object); + } + } + reset($objects); + rmdir($dir); + return true; + } + } +} +