Clear.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Option;
  15. use think\console\Output;
  16. class Clear extends Command
  17. {
  18. protected function configure()
  19. {
  20. // 指令配置
  21. $this
  22. ->setName('clear')
  23. ->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
  24. ->setDescription('Clear runtime file');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $path = $input->getOption('path') ?: RUNTIME_PATH;
  29. if (is_dir($path)) {
  30. $this->clearPath($path);
  31. }
  32. $output->writeln("<info>Clear Successed</info>");
  33. }
  34. protected function clearPath($path)
  35. {
  36. $path = realpath($path) . DS;
  37. $files = scandir($path);
  38. if ($files) {
  39. foreach ($files as $file) {
  40. if ('.' != $file && '..' != $file && is_dir($path . $file)) {
  41. $this->clearPath($path . $file);
  42. } elseif ('.gitignore' != $file && is_file($path . $file)) {
  43. unlink($path . $file);
  44. }
  45. }
  46. }
  47. }
  48. }