Completed
Push — master ( 094775...65d192 )
by Samuel
15:48 queued 14:44
created

OpcacheStatusScriptsCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 24
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.536
c 0
b 0
f 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\Helper\Table;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class OpcacheStatusScriptsCommand extends AbstractCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 19
    protected function configure()
25
    {
26
        $this
27 19
            ->setName('opcache:status:scripts')
28 19
            ->setDescription('Show scripts in the opcode cache')
29 19
            ->setHelp('');
30 19
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37 1
        $this->ensureExtensionLoaded('Zend OPcache');
38
39 1
        $info = $this->getCacheTool()->opcache_get_status(true);
40
41 1
        if ($info === false) {
42
            throw new \RuntimeException('opcache_get_status(): No Opcache status info available.  Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?');
43
        }
44
45 1
        $table = new Table($output);
46
        $table
47 1
            ->setHeaders([
48 1
                'Hits',
49
                'Memory',
50
                'Filename'
51
            ])
52 1
            ->setRows($this->processFilelist($info['scripts']))
53
        ;
54
55 1
        $table->render();
56
57 1
        return 0;
58
    }
59
60 1
    protected function processFileList(array $cacheList)
61
    {
62 1
        $list = [];
63
64 1
        foreach ($cacheList as $item) {
65
            $list[] = [
66
                number_format($item['hits']),
67
                Formatter::bytes($item['memory_consumption']),
68
                $this->processFilename($item['full_path']),
69
            ];
70
        }
71
72 1
        return $list;
73
    }
74
75
    protected function processFilename($filename)
76
    {
77
        $dir = getcwd();
78
79
        if (0 === strpos($filename, $dir)) {
80
            return "." . substr($filename, strlen($dir));
81
        }
82
83
        return $filename;
84
    }
85
}
86