Completed
Push — master ( 17dfca...2f9de9 )
by Samuel
01:17
created

OpcacheStatusCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 7
cts 11
cp 0.6364
rs 9.552
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4.7691
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\Helper\TableSeparator;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class OpcacheStatusCommand extends AbstractOpcacheCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 19
    protected function configure()
26
    {
27
        $this
28 19
            ->setName('opcache:status')
29 19
            ->setDescription('Show summary information about the opcode cache')
30 19
            ->setHelp('');
31 19
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38 1
        $this->ensureExtensionLoaded('Zend OPcache');
39
40 1
        $info = $this->getCacheTool()->opcache_get_status(false);
41
        try {
42
43 1
            $this->ensureSuccessfulOpcacheCall($info);
44
        } catch (\RuntimeException $e) {
45
            // lets handle gracefully when file_cache_only is true
46
            if (!strstr($e->getMessage(), 'opcache.file_cache_only')) {
47
                throw $e;
48
            }
49
        }
50
51 1
        if ($info['file_cache_only'] ?? false) {
52
            $this->renderFileCacheOnlyMode($output, $info);
53
        } else {
54 1
            $this->render($output, $info);
55
        }
56
57 1
        return 0;
58
    }
59
60
    /**
61
     * @param  OutputInterface $output
62
     * @param  array $info
63
     */
64
    protected function renderFileCacheOnlyMode(OutputInterface $output, $info) {
65
        $iterator = function ($k) use (&$info) {
66
            return [$k, var_export($info[$k], true)];
67
        };
68
69
        $table = new Table($output);
70
        $table->setHeaders(['Name', 'Value']);
71
        $table->setRows(array_map($iterator, array_keys($info)));
72
        $table->render();
73
    }
74
75
    /**
76
     * @param  OutputInterface $output
77
     * @param  array $info
78
     */
79 1
    protected function render(OutputInterface $output, $info) {
80 1
        $table = new Table($output);
81 1
        $table->setHeaders(['Name', 'Value']);
82 1
        $table->setRows($this->getRows($info, $info['opcache_statistics']));
83 1
        $table->render();
84 1
    }
85
86
87
    /**
88
     * @param  array $info
89
     * @param  array $stats
90
     * @return array
91
     */
92 1
    protected function getRows($info, $stats)
93
    {
94 1
        $rows = $this->getGeneralRows($info);
95
96 1
        if (isset($info['interned_strings_usage'])) {
97 1
            $rows = array_merge($rows, $this->getStringsRows($info));
98
        }
99
100 1
        return array_merge($rows, $this->getOpcacheStatsRows($stats));
101
    }
102
103
    /**
104
     * @param  array $info
105
     * @return array
106
     */
107 1
    protected function getGeneralRows($info)
108
    {
109
        return [
110 1
            ['Enabled', $info['opcache_enabled'] ? 'Yes' : 'No'],
111 1
            ['Cache full', $info['cache_full'] ? 'Yes' : 'No'],
112 1
            ['Restart pending', $info['restart_pending'] ? 'Yes' : 'No'],
113 1
            ['Restart in progress', $info['restart_in_progress'] ? 'Yes' : 'No'],
114 1
            ['Memory used', Formatter::bytes($info['memory_usage']['used_memory'])],
115 1
            ['Memory free', Formatter::bytes($info['memory_usage']['free_memory'])],
116 1
            ['Memory wasted (%)', sprintf("%s (%s%%)", Formatter::bytes($info['memory_usage']['wasted_memory']), $info['memory_usage']['current_wasted_percentage'])],
117
        ];
118
    }
119
120
    /**
121
     * @param  array $info
122
     * @return array
123
     */
124 1
    protected function getStringsRows($info)
125
    {
126
        return [
127 1
            ['Strings buffer size', Formatter::bytes($info['interned_strings_usage']['buffer_size'])],
128 1
            ['Strings memory used', Formatter::bytes($info['interned_strings_usage']['used_memory'])],
129 1
            ['Strings memory free', Formatter::bytes($info['interned_strings_usage']['free_memory'])],
130 1
            ['Number of strings', $info['interned_strings_usage']['number_of_strings']],
131
        ];
132
    }
133
134
    /**
135
     * @param  array $stats
136
     * @return array
137
     */
138 1
    protected function getOpcacheStatsRows($stats)
139
    {
140
        return [
141 1
            new TableSeparator(),
142 1
            ['Cached scripts', $stats['num_cached_scripts']],
143 1
            ['Cached keys', $stats['num_cached_keys']],
144 1
            ['Max cached keys', $stats['max_cached_keys']],
145 1
            ['Start time', Formatter::date($stats['start_time'], 'U')],
146 1
            ['Last restart time', $stats['last_restart_time'] ? Formatter::date($stats['last_restart_time'], 'U') : 'Never'],
147 1
            ['Oom restarts', $stats['oom_restarts']],
148 1
            ['Hash restarts', $stats['hash_restarts']],
149 1
            ['Manual restarts', $stats['manual_restarts']],
150 1
            ['Hits', $stats['hits']],
151 1
            ['Misses', $stats['misses']],
152 1
            ['Blacklist misses (%)', sprintf('%s (%s%%)', $stats['blacklist_misses'], $stats['blacklist_miss_ratio'])],
153 1
            ['Opcache hit rate', $stats['opcache_hit_rate']],
154
        ];
155
    }
156
}
157