Completed
Push — 1.x ( 2f995a...fa8c59 )
by Samuel
18:04
created

OpcacheInvalidateScriptsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
nc 1
cc 1
eloc 6
nop 0
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 CacheTool\Util\Formatter;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class OpcacheInvalidateScriptsCommand extends AbstractCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('opcache:invalidate:scripts')
28
            ->setDescription('Remove scripts from the opcode cache')
29
            ->addArgument('path', InputArgument::REQUIRED)
30
            ->setHelp('');
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $this->ensureExtensionLoaded('Zend OPcache');
39
        $path = $input->getArgument('path');
40
41
        $info = $this->getCacheTool()->opcache_get_status(true);
42
43
        if ($info === false) {
44
            throw new \RuntimeException('opcache_get_status(): No Opcache status info available.  Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?');
45
        }
46
47
        $table = $this->getHelper('table');
48
        $table
49
            ->setHeaders(array(
50
                'Cleaned',
51
                'Filename'
52
            ))
53
            ->setRows($this->processFilelist($info['scripts'], $path))
54
        ;
55
56
        $table->render($output);
57
    }
58
59
    protected function processFileList(array $cacheList, $path)
60
    {
61
        $list = array();
62
63
        sort($cacheList);
64
65
        foreach ($cacheList as $item) {
66
            $filename = $this->processFilename($item['full_path']);
67
            if (preg_match('|' . $path . '|', $filename)) {
68
                $list[] = array(
69
                    $this->getCacheTool()->opcache_invalidate($filename),
70
                    $filename
71
                );
72
            }
73
        }
74
75
        return $list;
76
    }
77
78
    protected function processFilename($filename)
79
    {
80
        $dir = getcwd();
81
82
        if (0 === strpos($filename, $dir)) {
83
            return "." . substr($filename, strlen($dir));
84
        }
85
86
        return $filename;
87
    }
88
}
89