Completed
Push — master ( ce0f45...969a46 )
by yuuki
01:36
created

ClearCacheCommand::fire()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 12
c 1
b 1
f 0
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2017 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Console;
19
20
use Illuminate\Console\Command;
21
use Illuminate\Filesystem\Filesystem;
22
use Illuminate\Contracts\Config\Repository as ConfigRepository;
23
24
/**
25
 * Class ClearCacheCommand
26
 */
27
class ClearCacheCommand extends Command
28
{
29
    /** @var string */
30
    protected $name = 'ytake:aspect-clear-cache';
31
32
    /** @var string */
33
    protected $description = 'Flush the application aspect code cache';
34
35
    /** @var ConfigRepository */
36
    protected $config;
37
38
    /** @var Filesystem */
39
    protected $filesystem;
40
41
    /**
42
     * @param ConfigRepository $config
43
     * @param Filesystem       $filesystem
44
     */
45
    public function __construct(ConfigRepository $config, Filesystem $filesystem)
46
    {
47
        parent::__construct();
48
        $this->config = $config;
49
        $this->filesystem = $filesystem;
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public function handle()
56
    {
57
        $configure = $this->config->get('ytake-laravel-aop.aspect');
58
        $driverConfig = $configure['drivers'][$configure['default']];
59
        if (isset($driverConfig['cache_dir'])) {
60
            $this->removeFiles($driverConfig['cache_dir']);
61
        }
62
        if (isset($driverConfig['compile_dir'])) {
63
            $this->removeFiles($driverConfig['compile_dir']);
64
        }
65
        $this->info('aspect code cache clear!');
66
    }
67
68
    /**
69
     * @param string $dir
70
     */
71
    protected function removeFiles($dir)
72
    {
73
        $files = $this->filesystem->glob($dir . '/*');
74
        foreach ($files as $file) {
75
            $this->filesystem->delete($file);
76
        }
77
    }
78
}
79