Clear.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\command;
  12. use think\Cache;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\input\Argument;
  16. use think\console\input\Option;
  17. use think\console\Output;
  18. class Clear extends Command
  19. {
  20. protected function configure()
  21. {
  22. // 指令配置
  23. $this
  24. ->setName('clear')
  25. ->addArgument('type', Argument::OPTIONAL, 'type to clear', null)
  26. ->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
  27. ->setDescription('Clear runtime file');
  28. }
  29. protected function execute(Input $input, Output $output)
  30. {
  31. $path = $input->getOption('path') ?: RUNTIME_PATH;
  32. $type = $input->getArgument('type');
  33. if ($type == 'route') {
  34. Cache::clear('route_check');
  35. } else {
  36. if (is_dir($path)) {
  37. $this->clearPath($path);
  38. }
  39. }
  40. $output->writeln("<info>Clear Successed</info>");
  41. }
  42. protected function clearPath($path)
  43. {
  44. $path = realpath($path) . DS;
  45. $files = scandir($path);
  46. if ($files) {
  47. foreach ($files as $file) {
  48. if ('.' != $file && '..' != $file && is_dir($path . $file)) {
  49. $this->clearPath($path . $file);
  50. } elseif ('.gitignore' != $file && is_file($path . $file)) {
  51. unlink($path . $file);
  52. }
  53. }
  54. }
  55. }
  56. }