ClearCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Illuminate\Cache\Console;
  3. use Illuminate\Cache\CacheManager;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Symfony\Component\Console\Attribute\AsCommand;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputOption;
  9. #[AsCommand(name: 'cache:clear')]
  10. class ClearCommand extends Command
  11. {
  12. /**
  13. * The console command name.
  14. *
  15. * @var string
  16. */
  17. protected $name = 'cache:clear';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Flush the application cache';
  24. /**
  25. * The cache manager instance.
  26. *
  27. * @var \Illuminate\Cache\CacheManager
  28. */
  29. protected $cache;
  30. /**
  31. * The filesystem instance.
  32. *
  33. * @var \Illuminate\Filesystem\Filesystem
  34. */
  35. protected $files;
  36. /**
  37. * Create a new cache clear command instance.
  38. *
  39. * @param \Illuminate\Cache\CacheManager $cache
  40. * @param \Illuminate\Filesystem\Filesystem $files
  41. * @return void
  42. */
  43. public function __construct(CacheManager $cache, Filesystem $files)
  44. {
  45. parent::__construct();
  46. $this->cache = $cache;
  47. $this->files = $files;
  48. }
  49. /**
  50. * Execute the console command.
  51. *
  52. * @return void
  53. */
  54. public function handle()
  55. {
  56. $this->laravel['events']->dispatch(
  57. 'cache:clearing', [$this->argument('store'), $this->tags()]
  58. );
  59. $successful = $this->cache()->flush();
  60. $this->flushFacades();
  61. if (! $successful) {
  62. return $this->components->error('Failed to clear cache. Make sure you have the appropriate permissions.');
  63. }
  64. $this->laravel['events']->dispatch(
  65. 'cache:cleared', [$this->argument('store'), $this->tags()]
  66. );
  67. $this->components->info('Application cache cleared successfully.');
  68. }
  69. /**
  70. * Flush the real-time facades stored in the cache directory.
  71. *
  72. * @return void
  73. */
  74. public function flushFacades()
  75. {
  76. if (! $this->files->exists($storagePath = storage_path('framework/cache'))) {
  77. return;
  78. }
  79. foreach ($this->files->files($storagePath) as $file) {
  80. if (preg_match('/facade-.*\.php$/', $file)) {
  81. $this->files->delete($file);
  82. }
  83. }
  84. }
  85. /**
  86. * Get the cache instance for the command.
  87. *
  88. * @return \Illuminate\Cache\Repository
  89. */
  90. protected function cache()
  91. {
  92. $cache = $this->cache->store($this->argument('store'));
  93. return empty($this->tags()) ? $cache : $cache->tags($this->tags());
  94. }
  95. /**
  96. * Get the tags passed to the command.
  97. *
  98. * @return array
  99. */
  100. protected function tags()
  101. {
  102. return array_filter(explode(',', $this->option('tags') ?? ''));
  103. }
  104. /**
  105. * Get the console command arguments.
  106. *
  107. * @return array
  108. */
  109. protected function getArguments()
  110. {
  111. return [
  112. ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear'],
  113. ];
  114. }
  115. /**
  116. * Get the console command options.
  117. *
  118. * @return array
  119. */
  120. protected function getOptions()
  121. {
  122. return [
  123. ['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear', null],
  124. ];
  125. }
  126. }