Completed
Pull Request — 1.x (#35)
by
unknown
18:26
created

OpcacheStatusScriptsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 65
rs 10

4 Methods

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