Completed
Push — 1.x ( 8c31f4...5f4811 )
by Samuel
7s
created

OpcacheStatusCommand::getGeneralRows()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 8.8571
nc 16
cc 5
eloc 9
nop 1
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\TableSeparator;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class OpcacheStatusCommand extends AbstractCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('opcache:status')
28
            ->setDescription('Show summary information about the opcode cache')
29
            ->setHelp('');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $this->ensureExtensionLoaded('Zend OPcache');
38
39
        $info = $this->getCacheTool()->opcache_get_status(false);
40
41
        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
        $table = $this->getHelper('table');
46
        $table->setHeaders(array('Name', 'Value'));
47
        $table->setRows($this->getRows($info, $info['opcache_statistics']));
48
        $table->render($output);
49
    }
50
51
    /**
52
     * @param  array $info
53
     * @param  array $stats
54
     * @return array
55
     */
56
    protected function getRows($info, $stats)
57
    {
58
        $rows = $this->getGeneralRows($info);
59
60
        if (isset($info['interned_strings_usage'])) {
61
            $rows = array_merge($rows, $this->getStringsRows($info));
62
        }
63
64
        return array_merge($rows, $this->getOpcacheStatsRows($stats));
65
    }
66
67
    /**
68
     * @param  array $info
69
     * @return array
70
     */
71
    protected function getGeneralRows($info)
72
    {
73
        return array(
74
            array('Enabled', $info['opcache_enabled'] ? 'Yes' : 'No'),
75
            array('Cache full', $info['cache_full'] ? 'Yes' : 'No'),
76
            array('Restart pending', $info['restart_pending'] ? 'Yes' : 'No'),
77
            array('Restart in progress', $info['restart_in_progress'] ? 'Yes' : 'No'),
78
            array('Memory used', Formatter::bytes($info['memory_usage']['used_memory'])),
79
            array('Memory free', Formatter::bytes($info['memory_usage']['free_memory'])),
80
            array('Memory wasted (%)', sprintf("%s (%s%%)", Formatter::bytes($info['memory_usage']['wasted_memory']), $info['memory_usage']['current_wasted_percentage'])),
81
        );
82
    }
83
84
    /**
85
     * @param  array $info
86
     * @return array
87
     */
88
    protected function getStringsRows($info)
89
    {
90
        return array(
91
            array('Strings buffer size', Formatter::bytes($info['interned_strings_usage']['buffer_size'])),
92
            array('Strings memory used', Formatter::bytes($info['interned_strings_usage']['used_memory'])),
93
            array('Strings memory free', Formatter::bytes($info['interned_strings_usage']['free_memory'])),
94
            array('Number of strings', $info['interned_strings_usage']['number_of_strings']),
95
        );
96
    }
97
98
    /**
99
     * @param  array $stats
100
     * @return array
101
     */
102
    protected function getOpcacheStatsRows($stats)
103
    {
104
        return array(
105
            new TableSeparator(),
106
            array('Cached scripts', $stats['num_cached_scripts']),
107
            array('Cached keys', $stats['num_cached_keys']),
108
            array('Max cached keys', $stats['max_cached_keys']),
109
            array('Start time', Formatter::date($stats['start_time'], 'U')),
110
            array('Last restart time', $stats['last_restart_time'] ? Formatter::date($stats['last_restart_time'], 'U') : 'Never'),
111
            array('Oom restarts', $stats['oom_restarts']),
112
            array('Hash restarts', $stats['hash_restarts']),
113
            array('Manual restarts', $stats['manual_restarts']),
114
            array('Hits', $stats['hits']),
115
            array('Misses', $stats['misses']),
116
            array('Blacklist misses (%)', sprintf('%s (%s%%)', $stats['blacklist_misses'], $stats['blacklist_miss_ratio'])),
117
            array('Opcache hit rate', $stats['opcache_hit_rate']),
118
        );
119
    }
120
}
121