|
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 <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace think\console\command; |
|
12
|
|
|
|
|
13
|
|
|
use think\console\Command; |
|
14
|
|
|
use think\console\Input; |
|
15
|
|
|
use think\console\input\Option; |
|
16
|
|
|
use think\console\Output; |
|
17
|
|
|
|
|
18
|
|
|
class Clear extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
protected function configure() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
// 指令配置 |
|
23
|
|
|
$this |
|
24
|
|
|
->setName('clear') |
|
25
|
|
|
->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null) |
|
26
|
|
|
->addOption('cache', 'c', Option::VALUE_NONE, 'clear cache file') |
|
27
|
|
|
->addOption('log', 'l', Option::VALUE_NONE, 'clear log file') |
|
28
|
|
|
->addOption('dir', 'r', Option::VALUE_NONE, 'clear empty dir') |
|
29
|
|
|
->addOption('route', 'u', Option::VALUE_NONE, 'clear route cache') |
|
30
|
|
|
->setDescription('Clear runtime file'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function execute(Input $input, Output $output) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
if ($input->getOption('route')) { |
|
36
|
|
|
$this->app->cache->clear('route_cache'); |
|
|
|
|
|
|
37
|
|
|
} else { |
|
38
|
|
|
$runtimePath = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR; |
|
39
|
|
|
|
|
40
|
|
|
if ($input->getOption('cache')) { |
|
41
|
|
|
$path = $runtimePath . 'cache'; |
|
42
|
|
|
} elseif ($input->getOption('log')) { |
|
43
|
|
|
$path = $runtimePath . 'log'; |
|
44
|
|
|
} else { |
|
45
|
|
|
$path = $input->getOption('path') ?: $runtimePath; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$rmdir = $input->getOption('dir') ? true : false; |
|
49
|
|
|
$this->clear(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, $rmdir); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$output->writeln("<info>Clear Successed</info>"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function clear(string $path, bool $rmdir): void |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$files = is_dir($path) ? scandir($path) : []; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($files as $file) { |
|
60
|
|
|
if ('.' != $file && '..' != $file && is_dir($path . $file)) { |
|
61
|
|
|
array_map('unlink', glob($path . $file . DIRECTORY_SEPARATOR . '*.*')); |
|
|
|
|
|
|
62
|
|
|
if ($rmdir) { |
|
63
|
|
|
rmdir($path . $file); |
|
64
|
|
|
} |
|
65
|
|
|
} elseif ('.gitignore' != $file && is_file($path . $file)) { |
|
66
|
|
|
unlink($path . $file); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|