Completed
Pull Request — 6.0 (#2285)
by
unknown
11:12
created

Clear::cacheHasExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
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->setName('clear')
24
            ->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
25
            ->addOption('cache', 'c', Option::VALUE_NONE, 'clear cache file')
26
            ->addOption('log', 'l', Option::VALUE_NONE, 'clear log file')
27
            ->addOption('dir', 'r', Option::VALUE_NONE, 'clear empty dir')
28
            ->addOption('expire', 'e', Option::VALUE_NONE, 'clear cache file if cache has expired')
29
            ->setDescription('Clear runtime file');
30
    }
31
32
    protected function execute(Input $input, Output $output)
33
    {
34
        $runtimePath = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR;
35
36
        if ($input->getOption('cache')) {
37
            $path = $runtimePath . 'cache';
38
        } elseif ($input->getOption('log')) {
39
            $path = $runtimePath . 'log';
40
        } else {
41
            $path = $input->getOption('path') ?: $runtimePath;
42
        }
43
44
        $rmdir = $input->getOption('dir') ? true : false;
45
        // --expire 仅当 --cache 时生效
46
        $cache_expire = $input->getOption('expire') && $input->getOption('cache') ? true : false;
47
        $this->clear(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, $rmdir, $cache_expire);
48
49
        $output->writeln("<info>Clear Successed</info>");
50
    }
51
52
    protected function clear(string $path, bool $rmdir, bool $cache_expire): void
53
    {
54
        $files = is_dir($path) ? scandir($path) : [];
55
56
        foreach ($files as $file) {
57
            if ('.' != $file && '..' != $file && is_dir($path . $file)) {
58
                $this->clear($path . $file . DIRECTORY_SEPARATOR, $rmdir, $cache_expire);
59
                if ($rmdir) {
60
                    @rmdir($path . $file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

60
                    /** @scrutinizer ignore-unhandled */ @rmdir($path . $file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
                }
62
            } elseif ('.gitignore' != $file && is_file($path . $file)) {
63
                if ($cache_expire) {
64
                    if ($this->cacheHasExpired($path . $file)) {
65
                        unlink($path . $file);
66
                    }
67
                } else {
68
                    unlink($path . $file);
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * 缓存文件是否已过期
76
     * @param $filename string 文件路径
77
     * @return bool
78
     */
79
    protected function cacheHasExpired($filename) {
80
        $content = file_get_contents($filename);
81
        $expire = (int) substr($content, 8, 12);
82
        return 0 != $expire && time() - $expire > filemtime($filename);
83
    }
84
85
}
86