Completed
Push — master ( 24c3a4...52b53c )
by Samuel
22:24 queued 20:47
created

OpcacheInvalidateScriptsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 20.51%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 0
loc 72
ccs 8
cts 39
cp 0.2051
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processFileList() 0 18 3
A configure() 0 9 1
A execute() 0 23 2
A processFilename() 0 10 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 AbstractCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 5
    protected function configure()
26
    {
27 5
        $this
28 5
            ->setName('opcache:invalidate:scripts')
29 5
            ->setDescription('Remove scripts from the opcode cache')
30 5
            ->addArgument('path', InputArgument::REQUIRED)
31 5
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force scripts invalidation')
32 5
            ->setHelp('');
33 5
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
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
46
        if ($info === false) {
47
            throw new \RuntimeException('opcache_get_status(): No Opcache status info available.  Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?');
48
        }
49
50
        $table = new Table($output);
51
        $table
52
            ->setHeaders(array(
53
                'Cleaned',
54
                'Filename'
55
            ))
56
            ->setRows($this->processFilelist($info['scripts'], $path, $force))
57
        ;
58
59
        $table->render();
60
    }
61
62
    protected function processFileList(array $cacheList, $path, $force)
63
    {
64
        $list = array();
65
66
        sort($cacheList);
67
68
        foreach ($cacheList as $item) {
69
            $filename = $this->processFilename($item['full_path']);
70
            if (preg_match('|' . $path . '|', $filename)) {
71
                $list[] = array(
72
                    $this->getCacheTool()->opcache_invalidate($filename, $force),
73
                    $filename
74
                );
75
            }
76
        }
77
78
        return $list;
79
    }
80
81
    protected function processFilename($filename)
82
    {
83
        $dir = getcwd();
84
85
        if (0 === strpos($filename, $dir)) {
86
            return "." . substr($filename, strlen($dir));
87
        }
88
89
        return $filename;
90
    }
91
}
92