ClearCacheCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @package Grav\Console\Cli
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Cli;
  9. use Grav\Common\Cache;
  10. use Grav\Console\ConsoleCommand;
  11. use Symfony\Component\Console\Input\InputOption;
  12. class ClearCacheCommand extends ConsoleCommand
  13. {
  14. protected function configure()
  15. {
  16. $this
  17. ->setName('cache')
  18. ->setAliases(['clearcache', 'cache-clear'])
  19. ->setDescription('Clears Grav cache')
  20. ->addOption('invalidate', null, InputOption::VALUE_NONE, 'Invalidate cache, but do not remove any files')
  21. ->addOption('purge', null, InputOption::VALUE_NONE, 'If set purge old caches')
  22. ->addOption('all', null, InputOption::VALUE_NONE, 'If set will remove all including compiled, twig, doctrine caches')
  23. ->addOption('assets-only', null, InputOption::VALUE_NONE, 'If set will remove only assets/*')
  24. ->addOption('images-only', null, InputOption::VALUE_NONE, 'If set will remove only images/*')
  25. ->addOption('cache-only', null, InputOption::VALUE_NONE, 'If set will remove only cache/*')
  26. ->addOption('tmp-only', null, InputOption::VALUE_NONE, 'If set will remove only tmp/*')
  27. ->setHelp('The <info>cache</info> command allows you to interact with Grav cache');
  28. }
  29. protected function serve()
  30. {
  31. $this->cleanPaths();
  32. }
  33. /**
  34. * loops over the array of paths and deletes the files/folders
  35. */
  36. private function cleanPaths()
  37. {
  38. $this->output->writeln('');
  39. if ($this->input->getOption('purge')) {
  40. $this->output->writeln('<magenta>Purging old cache</magenta>');
  41. $this->output->writeln('');
  42. $msg = Cache::purgeJob();
  43. $this->output->writeln($msg);
  44. } else {
  45. $this->output->writeln('<magenta>Clearing cache</magenta>');
  46. $this->output->writeln('');
  47. if ($this->input->getOption('all')) {
  48. $remove = 'all';
  49. } elseif ($this->input->getOption('assets-only')) {
  50. $remove = 'assets-only';
  51. } elseif ($this->input->getOption('images-only')) {
  52. $remove = 'images-only';
  53. } elseif ($this->input->getOption('cache-only')) {
  54. $remove = 'cache-only';
  55. } elseif ($this->input->getOption('tmp-only')) {
  56. $remove = 'tmp-only';
  57. } elseif ($this->input->getOption('invalidate')) {
  58. $remove = 'invalidate';
  59. } else {
  60. $remove = 'standard';
  61. }
  62. foreach (Cache::clearCache($remove) as $result) {
  63. $this->output->writeln($result);
  64. }
  65. }
  66. }
  67. }