Completed
Push — github-actions ( 673aea...a81297 )
by Samuel
01:18
created

OpcacheStatusCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 4
dl 0
loc 102
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 15 2
A getRows() 0 10 2
A getGeneralRows() 0 12 5
A getStringsRows() 0 9 1
A getOpcacheStatsRows() 0 18 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\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 AbstractCommand
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)
37
    {
38 1
        $this->ensureExtensionLoaded('Zend OPcache');
39
40 1
        $info = $this->getCacheTool()->opcache_get_status(false);
41
42 1
        if ($info === false) {
43
            throw new \RuntimeException('opcache_get_status(): No Opcache status info available.  Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?');
44
        }
45
46 1
        $table = new Table($output);
47 1
        $table->setHeaders(['Name', 'Value']);
48 1
        $table->setRows($this->getRows($info, $info['opcache_statistics']));
49 1
        $table->render();
50 1
    }
51
52
    /**
53
     * @param  array $info
54
     * @param  array $stats
55
     * @return array
56
     */
57 1
    protected function getRows($info, $stats)
58
    {
59 1
        $rows = $this->getGeneralRows($info);
60
61 1
        if (isset($info['interned_strings_usage'])) {
62 1
            $rows = array_merge($rows, $this->getStringsRows($info));
63
        }
64
65 1
        return array_merge($rows, $this->getOpcacheStatsRows($stats));
66
    }
67
68
    /**
69
     * @param  array $info
70
     * @return array
71
     */
72 1
    protected function getGeneralRows($info)
73
    {
74
        return [
75 1
            ['Enabled', $info['opcache_enabled'] ? 'Yes' : 'No'],
76 1
            ['Cache full', $info['cache_full'] ? 'Yes' : 'No'],
77 1
            ['Restart pending', $info['restart_pending'] ? 'Yes' : 'No'],
78 1
            ['Restart in progress', $info['restart_in_progress'] ? 'Yes' : 'No'],
79 1
            ['Memory used', Formatter::bytes($info['memory_usage']['used_memory'])],
80 1
            ['Memory free', Formatter::bytes($info['memory_usage']['free_memory'])],
81 1
            ['Memory wasted (%)', sprintf("%s (%s%%)", Formatter::bytes($info['memory_usage']['wasted_memory']), $info['memory_usage']['current_wasted_percentage'])],
82
        ];
83
    }
84
85
    /**
86
     * @param  array $info
87
     * @return array
88
     */
89 1
    protected function getStringsRows($info)
90
    {
91
        return [
92 1
            ['Strings buffer size', Formatter::bytes($info['interned_strings_usage']['buffer_size'])],
93 1
            ['Strings memory used', Formatter::bytes($info['interned_strings_usage']['used_memory'])],
94 1
            ['Strings memory free', Formatter::bytes($info['interned_strings_usage']['free_memory'])],
95 1
            ['Number of strings', $info['interned_strings_usage']['number_of_strings']],
96
        ];
97
    }
98
99
    /**
100
     * @param  array $stats
101
     * @return array
102
     */
103 1
    protected function getOpcacheStatsRows($stats)
104
    {
105
        return [
106 1
            new TableSeparator(),
107 1
            ['Cached scripts', $stats['num_cached_scripts']],
108 1
            ['Cached keys', $stats['num_cached_keys']],
109 1
            ['Max cached keys', $stats['max_cached_keys']],
110 1
            ['Start time', Formatter::date($stats['start_time'], 'U')],
111 1
            ['Last restart time', $stats['last_restart_time'] ? Formatter::date($stats['last_restart_time'], 'U') : 'Never'],
112 1
            ['Oom restarts', $stats['oom_restarts']],
113 1
            ['Hash restarts', $stats['hash_restarts']],
114 1
            ['Manual restarts', $stats['manual_restarts']],
115 1
            ['Hits', $stats['hits']],
116 1
            ['Misses', $stats['misses']],
117 1
            ['Blacklist misses (%)', sprintf('%s (%s%%)', $stats['blacklist_misses'], $stats['blacklist_miss_ratio'])],
118 1
            ['Opcache hit rate', $stats['opcache_hit_rate']],
119
        ];
120
    }
121
}
122