|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controllers\Api\V1; |
|
6
|
|
|
|
|
7
|
|
|
use Nymfonya\Component\Http\Response; |
|
8
|
|
|
use Nymfonya\Component\Container; |
|
9
|
|
|
use App\Component\Cache\Redis\Adapter; |
|
10
|
|
|
use App\Reuse\Controllers\Cacheable; |
|
11
|
|
|
|
|
12
|
|
|
final class Stat extends Cacheable |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const _SCRIPTS = 'scripts'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* instanciate |
|
19
|
|
|
* |
|
20
|
|
|
* @param Container $container |
|
21
|
|
|
*/ |
|
22
|
5 |
|
public function __construct(Container $container) |
|
23
|
|
|
{ |
|
24
|
5 |
|
parent::__construct($container); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* opcache |
|
29
|
|
|
* |
|
30
|
|
|
* @Role anonymous |
|
31
|
|
|
* @return Stat |
|
32
|
|
|
*/ |
|
33
|
1 |
|
final public function opcache(): Stat |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->response |
|
36
|
1 |
|
->setCode(Response::HTTP_SERVICE_UNAVAILABLE) |
|
37
|
1 |
|
->setContent([ |
|
38
|
1 |
|
Response::_ERROR => true, |
|
39
|
1 |
|
Response::_ERROR_MSG => 'Opcache disable' |
|
40
|
|
|
]); |
|
41
|
1 |
|
$status = opcache_get_status(); |
|
42
|
1 |
|
if (!empty($status)) { |
|
43
|
|
|
$path = dirname(dirname($this->request->getFilename())); |
|
44
|
|
|
$scripts = array_filter( |
|
45
|
|
|
$status[self::_SCRIPTS], |
|
46
|
|
|
function ($val) use ($path) { |
|
47
|
|
|
return false !== strpos($val['full_path'], $path); |
|
48
|
|
|
} |
|
49
|
|
|
); |
|
50
|
|
|
$status[self::_SCRIPTS] = array_values($scripts); |
|
51
|
|
|
$bytes = array_reduce( |
|
52
|
|
|
$status[self::_SCRIPTS], |
|
53
|
|
|
function ($stack, $val) { |
|
54
|
|
|
return $stack + (int) $val['memory_consumption']; |
|
55
|
|
|
} |
|
56
|
|
|
); |
|
57
|
|
|
$scriptCount = count($scripts); |
|
58
|
|
|
unset($scripts); |
|
59
|
|
|
$this->response |
|
60
|
|
|
->setCode(Response::HTTP_OK) |
|
61
|
|
|
->setContent( |
|
62
|
|
|
[ |
|
63
|
|
|
'error' => false, |
|
64
|
|
|
'datas' => [ |
|
65
|
|
|
'php_version' => phpversion(), |
|
66
|
|
|
'nb_files' => $scriptCount, |
|
67
|
|
|
'memory_used' => $bytes, |
|
68
|
|
|
'status' => $status |
|
69
|
|
|
] |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
unset($scriptCount, $scripts, $bytes); |
|
73
|
|
|
} |
|
74
|
1 |
|
unset($status); |
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* filecache |
|
80
|
|
|
* |
|
81
|
|
|
* @Role anonymous |
|
82
|
|
|
* @return Stat |
|
83
|
|
|
*/ |
|
84
|
1 |
|
final public function filecache(): Stat |
|
85
|
|
|
{ |
|
86
|
1 |
|
$path = realpath($this->getFileCachePath()); |
|
87
|
1 |
|
$files = glob($path . '/*'); |
|
88
|
1 |
|
$this->response |
|
89
|
1 |
|
->setCode(Response::HTTP_OK) |
|
90
|
1 |
|
->setContent( |
|
91
|
|
|
[ |
|
92
|
1 |
|
'error' => false, |
|
93
|
|
|
'datas' => [ |
|
94
|
1 |
|
'cache_path' => $path, |
|
95
|
1 |
|
'cache_hits' => count($files), |
|
|
|
|
|
|
96
|
1 |
|
'cache_files' => $files, |
|
97
|
|
|
] |
|
98
|
|
|
] |
|
99
|
|
|
); |
|
100
|
1 |
|
unset($path, $files); |
|
101
|
1 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* check redis service |
|
106
|
|
|
* |
|
107
|
|
|
* @return Test |
|
108
|
|
|
*/ |
|
109
|
2 |
|
final public function redis(): Stat |
|
110
|
|
|
{ |
|
111
|
2 |
|
$redisService = $this->getContainer()->getService(Adapter::class); |
|
112
|
2 |
|
$client = $redisService->getClient(); |
|
113
|
2 |
|
$error = $redisService->isError(); |
|
114
|
2 |
|
$ping = ''; |
|
115
|
2 |
|
$keys = []; |
|
116
|
2 |
|
if (false === $error) { |
|
117
|
1 |
|
$client->set('redis-entry-name', 'redis-entry-name-item'); |
|
118
|
1 |
|
$client->lpush('redis-list', 'item0'); |
|
119
|
1 |
|
$client->lpush('redis-list', 'item1'); |
|
120
|
1 |
|
$client->lpush('redis-list', 'item2'); |
|
121
|
1 |
|
$ping = $client->ping(); |
|
122
|
1 |
|
$keys = $client->keys('*'); |
|
123
|
|
|
} |
|
124
|
2 |
|
$resCodeError = $error |
|
125
|
1 |
|
? Response::HTTP_INTERNAL_SERVER_ERROR |
|
126
|
2 |
|
: Response::HTTP_OK; |
|
127
|
2 |
|
$this->response->setCode($resCodeError)->setContent( |
|
128
|
|
|
[ |
|
129
|
2 |
|
'error' => $error, |
|
130
|
2 |
|
'errorCode' => $redisService->getErrorCode(), |
|
131
|
2 |
|
'errorMessage' => $redisService->getErrorMessage(), |
|
132
|
|
|
'datas' => [ |
|
133
|
2 |
|
'keys' => $keys, |
|
134
|
2 |
|
'ping' => $ping, |
|
135
|
|
|
] |
|
136
|
|
|
] |
|
137
|
|
|
); |
|
138
|
2 |
|
unset($redisService, $client); |
|
139
|
2 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|