Skip to content

Commit ce70db6

Browse files
author
Яценко Андрей
committed
Update: print config table
1 parent 9f8902c commit ce70db6

File tree

5 files changed

+104
-32
lines changed

5 files changed

+104
-32
lines changed

src/Commands/ClearCommand.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use Illuminate\Console\Command;
66
use LaravelOpcacheFacade\OpcacheFacade;
77

8+
/**
9+
* Class ClearCommand
10+
* @package LaravelOpcacheFacade\Commands
11+
*/
812
class ClearCommand extends Command
913
{
10-
/** @var string The console command name.*/
14+
/** @var string The console command name. */
1115
protected $signature = 'opcache:clear';
1216

13-
/** @var string The console command description.*/
17+
/** @var string The console command description. */
1418
protected $description = 'Resets the contents of the opcode cache';
1519

1620
/** @return mixed Execute the console command. */

src/Commands/ConfigCommand.php

+11-28
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,35 @@
44

55
use Illuminate\Console\Command;
66
use LaravelOpcacheFacade\OpcacheFacade;
7+
use LaravelOpcacheFacade\utils\CommandUtil;
78

9+
/**
10+
* Class ConfigCommand
11+
* @package LaravelOpcacheFacade\Commands
12+
*/
813
class ConfigCommand extends Command
914
{
10-
/** @var string The console command name.*/
15+
/** @var string The console command name. */
1116
protected $signature = 'opcache:config';
1217

13-
/** @var string The console command description.*/
18+
/** @var string The console command description. */
1419
protected $description = 'Show configuration information about the cache';
1520

1621
/** @return mixed Execute the console command. */
1722
public function handle()
1823
{
1924
$config = OpcacheFacade::getConfig();
2025
if ($config) {
26+
$header = ['key', 'value'];
27+
2128
$this->info('Version info:');
22-
$this->table(
23-
['key', 'value'],
24-
$this->prepareTable($config['version']),
25-
'box-double'
26-
);
29+
$this->table($header, CommandUtil::prepareTable($config['version']), 'box-double');
2730

2831
$this->info('Configuration info:');
29-
$this->table(
30-
['key', 'value'],
31-
$this->prepareTable($config['directives']),
32-
'box-double'
33-
);
32+
$this->table($header, CommandUtil::prepareTable($config['directives']), 'box-double');
3433
} else {
3534
$this->error('An error occurred while get config opcache');
3635
exit(2);
3736
}
3837
}
39-
40-
/**
41-
* @param $data
42-
*
43-
* @return array|array[]
44-
*/
45-
protected function prepareTable($data)
46-
{
47-
$prepareRowTable = static function ($key, $value) {
48-
return [
49-
'key' => $key,
50-
'value' => $value,
51-
];
52-
};
53-
return array_map($prepareRowTable, array_keys($data), $data);
54-
}
5538
}

src/Commands/StatusCommand.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,47 @@
44

55
use Illuminate\Console\Command;
66
use LaravelOpcacheFacade\OpcacheFacade;
7+
use LaravelOpcacheFacade\utils\CommandUtil;
78

9+
/**
10+
* Class StatusCommand
11+
* @package LaravelOpcacheFacade\Commands
12+
*/
813
class StatusCommand extends Command
914
{
10-
/** @var string The console command name.*/
15+
/** @var string The console command name. */
1116
protected $signature = 'opcache:status';
1217

13-
/** @var string The console command description.*/
18+
/** @var string The console command description. */
1419
protected $description = 'Get status information about the cache';
1520

1621
/** @return mixed Execute the console command. */
1722
public function handle()
1823
{
1924
$info = OpcacheFacade::getStatus();
25+
$base = $info;
26+
unset($base['memory_usage'], $base['interned_strings_usage'], $base['opcache_statistics']);
27+
2028
if ($info) {
29+
$header = ['key', 'value'];
30+
31+
$this->info('Base info:');
32+
$this->table($header, CommandUtil::prepareTable($base), 'box-double');
33+
34+
if (isset($info['memory_usage'])) {
35+
$this->info('Memory usage:');
36+
$this->table($header, CommandUtil::prepareTable($info['memory_usage']), 'box-double');
37+
}
38+
39+
if (isset($info['interned_strings_usage'])) {
40+
$this->info('Interned strings usage:');
41+
$this->table($header, CommandUtil::prepareTable($info['interned_strings_usage']), 'box-double');
42+
}
2143

44+
if (isset($info['opcache_statistics'])) {
45+
$this->info('OPcache statistics:');
46+
$this->table($header, CommandUtil::prepareTable($info['opcache_statistics']), 'box-double');
47+
}
2248
} else {
2349
$this->error('An error occurred while get status opcache');
2450
exit(2);

src/ServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace LaravelOpcacheFacade;
44

5+
/**
6+
* Class ServiceProvider
7+
* @package LaravelOpcacheFacade
8+
*/
59
class ServiceProvider extends \Illuminate\Support\ServiceProvider
610
{
711
/**

src/utils/CommandUtil.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
4+
namespace LaravelOpcacheFacade\utils;
5+
6+
7+
/**
8+
* Class CommandUtil
9+
* @package LaravelOpcacheFacade\utils
10+
*/
11+
class CommandUtil
12+
{
13+
/**
14+
* @param $data
15+
* @return array|array[]
16+
*/
17+
public static function prepareTable($data): array
18+
{
19+
$prepareRowTable = static function ($key, $value) {
20+
return [
21+
'key' => $key,
22+
'value' => self::prepareValue($key, $value),
23+
];
24+
};
25+
return array_map($prepareRowTable, array_keys($data), $data);
26+
}
27+
28+
/**
29+
* @param $key
30+
* @param $value
31+
* @return false|string
32+
*/
33+
public static function prepareValue($key, $value){
34+
if (strpos($key, 'memory') !== false || strpos($key, 'size') !== false) {
35+
return self::formatBytes($value);
36+
}
37+
if (strpos($key, 'time') !== false) {
38+
return date('Y-m-d H:i:s', $value);
39+
}
40+
if (is_bool($value)) {
41+
return $value ? 'true' : 'false';
42+
}
43+
return $value;
44+
}
45+
46+
/**
47+
* @param $bytes
48+
* @return string
49+
*/
50+
public static function formatBytes($bytes): string
51+
{
52+
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
53+
return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . $unit[$i];
54+
}
55+
}

0 commit comments

Comments
 (0)