ClearCommand.php 3.1KB

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