Completed
Push — master ( 17dfca...2f9de9 )
by Samuel
01:17
created

OpcacheInvalidateScriptsCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 12
cp 0
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Command;
13
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class OpcacheInvalidateScriptsCommand extends AbstractOpcacheCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 19
    protected function configure()
26
    {
27
        $this
28 19
            ->setName('opcache:invalidate:scripts')
29 19
            ->setDescription('Remove scripts from the opcode cache')
30 19
            ->addArgument('path', InputArgument::REQUIRED)
31 19
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force scripts invalidation')
32 19
            ->setHelp('');
33 19
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        $this->ensureExtensionLoaded('Zend OPcache');
41
        $path = $input->getArgument('path');
42
        $force = $input->getOption('force');
43
44
        $info = $this->getCacheTool()->opcache_get_status(true);
45
        $this->ensureSuccessfulOpcacheCall($info);
46
47
        $table = new Table($output);
48
        $table
49
            ->setHeaders([
50
                'Cleaned',
51
                'Filename'
52
            ])
53
            ->setRows($this->processFilelist($info['scripts'], $path, $force))
54
        ;
55
56
        $table->render();
57
58
        return 0;
59
    }
60
61
    protected function processFileList(array $cacheList, $path, $force)
62
    {
63
        $list = [];
64
65
        sort($cacheList);
66
67
        foreach ($cacheList as $item) {
68
            $filename = $this->processFilename($item['full_path']);
69
            if (preg_match('|' . $path . '|', $filename)) {
70
                $list[] = [
71
                    $this->getCacheTool()->opcache_invalidate($filename, $force),
72
                    $filename
73
                ];
74
            }
75
        }
76
77
        return $list;
78
    }
79
80
    protected function processFilename($filename)
81
    {
82
        $dir = getcwd();
83
84
        if (0 === strpos($filename, $dir)) {
85
            return "." . substr($filename, strlen($dir));
86
        }
87
88
        return $filename;
89
    }
90
}
91