Skip to content

Commit 47a39fc

Browse files
committed
Update
1 parent cb77966 commit 47a39fc

10 files changed

Lines changed: 46 additions & 39 deletions

File tree

src/Dashboards/Memcached/Compatibility/KeysTrait.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ public function command(string $command): array {
4141

4242
while (!feof($fp)) {
4343
$buffer .= fgets($fp, 1024);
44+
45+
$ends = ['END', 'DELETED', 'NOT_FOUND', 'OK', 'EXISTS', 'ERROR', 'RESET', 'STORED', 'NOT_STORED', 'VERSION'];
46+
47+
foreach ($ends as $end) {
48+
if (preg_match('/^'.$end.'/imu', $buffer)) {
49+
break 2;
50+
}
51+
}
52+
4453
$lines = explode("\n", $buffer);
4554
$buffer = array_pop($lines);
4655

4756
foreach ($lines as $line) {
4857
$line = trim($line);
4958

50-
if ($line === 'END' || $line === 'ERROR' || $line === '') {
51-
break 2;
52-
}
53-
5459
$data[] = $line;
5560
}
5661
}
@@ -71,17 +76,19 @@ private function keyData(string $line): array {
7176
static $data = [];
7277

7378
foreach (explode(' ', $line) as $part) {
74-
[$key, $val] = explode('=', $part);
75-
76-
if ($key === 'exp') {
77-
if ($val !== '-1') {
78-
$val = (int) $val - time();
79-
} else {
80-
$val = (int) $val;
79+
if ($part !== '') {
80+
[$key, $val] = explode('=', $part);
81+
82+
if ($key === 'exp') {
83+
if ($val !== '-1') {
84+
$val = (int) $val - time();
85+
} else {
86+
$val = (int) $val;
87+
}
8188
}
82-
}
8389

84-
$data[$key] = $val;
90+
$data[$key] = $val;
91+
}
8592
}
8693

8794
return $data;
@@ -106,10 +113,8 @@ public function getKeys(): array {
106113

107114
$all_keys = $this->command('lru_crawler metadump all');
108115

109-
if ($all_keys !== null) {
110-
foreach ($all_keys as $line) {
111-
$keys[] = $this->keyData($line);
112-
}
116+
foreach ($all_keys as $line) {
117+
$keys[] = $this->keyData($line);
113118
}
114119

115120
return $keys;

src/Dashboards/Memcached/Compatibility/PHPMem.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ private function send(string $command): string {
8080
while (!feof($fp)) {
8181
$buffer .= fgets($fp, 256);
8282

83-
foreach (['END', 'DELETED', 'NOT_FOUND', 'OK', 'EXISTS', 'ERROR', 'RESET', 'STORED', 'NOT_STORED', 'VERSION'] as $end) {
83+
$ends = ['END', 'DELETED', 'NOT_FOUND', 'OK', 'EXISTS', 'ERROR', 'RESET', 'STORED', 'NOT_STORED', 'VERSION'];
84+
85+
foreach ($ends as $end) {
8486
if (preg_match('/^'.$end.'/imu', $buffer)) {
8587
break 2;
8688
}
@@ -109,7 +111,8 @@ public function set(string $key, $value, int $expiration = 0): bool {
109111
$value = serialize($value);
110112
}
111113

112-
$raw = $this->send('set'.' '.$key.' '.'0 '.$expiration.' '.strlen((string) $value)."\r\n".$value."\r\n");
114+
// set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n
115+
$raw = $this->send('set'.' '.$key.' '.'0 '.$expiration.' '.strlen((string) $value)."\r\n".$value);
113116

114117
if (Helpers::str_starts_with($raw, 'STORED')) {
115118
return true;

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ private function getAllKeys($memcached): array {
140140

141141
foreach ($memcached->getKeys() as $key_data) {
142142
$keys[] = [
143-
'key' => $key_data['key'],
144-
'ttl' => $key_data['exp'],
143+
'key' => $key_data['key'] ?? $key_data,
144+
'ttl' => $key_data['exp'] ?? null,
145145
'type' => 'string', // In Memcached everything is stored as a string. Calling gettype() will slow down page loading.
146146
];
147147
}

src/Dashboards/OPCache/OPCacheDashboard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ public function info(): array {
9090
'title' => 'Status',
9191
'moreinfo' => true,
9292
'data' => [
93-
'JIT' => Helpers::enabledDisabledBadge($this->template, isset($status['jit']) && $status['jit']['enabled']),
93+
'JIT' => Helpers::stateBadge($this->template, isset($status['jit']) && $status['jit']['enabled']),
9494
'Start time' => Format::time($stats['start_time']),
9595
'Last restart' => Format::time($stats['last_restart_time']),
96-
'Cache full' => Helpers::enabledDisabledBadge($this->template, $status['cache_full'] === false, null, ['No', 'Yes']),
96+
'Cache full' => Helpers::stateBadge($this->template, $status['cache_full'] === false, null, ['No', 'Yes']),
9797
],
9898
],
9999
[

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ public function listRem(string $key, string $value, int $count): int {
9090
public function getInfo(string $option = null): array {
9191
static $array = [];
9292

93-
foreach (['Server', 'Clients', 'Memory', 'Persistence', 'Stats', 'Replication', 'CPU', 'Cluster', 'Keyspace'] as $option_name) {
93+
$options = ['Server', 'Clients', 'Memory', 'Persistence', 'Stats', 'Replication', 'CPU', 'Cluster', 'Keyspace'];
94+
95+
foreach ($options as $option_name) {
9496
$data = $this->info()[$option_name];
9597

9698
if ($option_name === 'Keyspace') {

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function listRem(string $key, string $value, int $count): int {
9191
public function getInfo(string $option = null): array {
9292
static $info = [];
9393

94-
foreach (['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'] as $option_name) {
94+
$options = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'];
95+
96+
foreach ($options as $option_name) {
9597
$info[strtolower($option_name)] = $this->info($option_name);
9698
}
9799

src/Dashboards/Server/ServerDashboard.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ public function ajax(): string {
6262
* @return array<string, mixed>
6363
*/
6464
public function info(): array {
65-
$xdebug = Helpers::enabledDisabledBadge(
66-
$this->template,
67-
extension_loaded('xdebug'),
68-
' - v'.phpversion('xdebug')
69-
);
65+
$xdebug = Helpers::stateBadge($this->template, extension_loaded('xdebug'), ' - v'.phpversion('xdebug'));
7066

7167
return [
7268
'panels' => [

src/Helpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ public static function alert(Template $template, string $message, ?string $color
7474
}
7575

7676
/**
77-
* Show enabled/disabled badge.
77+
* Show state badge.
7878
*
7979
* @param Template $template
8080
* @param bool $enabled
81-
* @param ?string $text
81+
* @param string|null $text
8282
* @param ?array<int, string> $badge_text
8383
*
8484
* @return string
8585
*/
86-
public static function enabledDisabledBadge(Template $template, bool $enabled = true, ?string $text = null, ?array $badge_text = null): string {
86+
public static function stateBadge(Template $template, bool $enabled, string $text = null, ?array $badge_text = null): string {
8787
$badge_text ??= ['Enabled', 'Disabled'];
8888

8989
return $template->render('components/badge', [

src/Paginator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class Paginator {
3939
public function __construct(Template $template, array $items, array $queries = [['pp'], ['p' => ''],]) {
4040
$this->template = $template;
4141
$this->url = $queries;
42-
4342
$this->total = count($items);
4443
$this->page = Http::get('p', 'int', 1);
4544
$this->per_page = Http::get('pp', 'int', 25);
@@ -96,13 +95,12 @@ private function getPages(): array {
9695
* @return string
9796
*/
9897
public function render(): string {
99-
$on_page = count($this->paginated);
100-
98+
$on_page = count($this->paginated) > 0 ? 1 : 0;
10199
$select = [25, 50, 100, 200, 300, 400];
102100

103101
return $this->template->render('components/paginator', [
104-
'first_on_page' => Format::number((int) array_key_first($this->paginated) + ($on_page > 0 ? 1 : 0)),
105-
'last_on_page' => Format::number((int) array_key_last($this->paginated) + ($on_page > 0 ? 1 : 0)),
102+
'first_on_page' => Format::number((int) array_key_first($this->paginated) + $on_page),
103+
'last_on_page' => Format::number((int) array_key_last($this->paginated) + $on_page),
106104
'total' => Format::number($this->total),
107105
'current_page' => $this->page,
108106
'per_page' => $this->per_page,

src/Template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public function render(string $tpl, array $data = []): string {
8585

8686
$twig->addFunction(new TwigFunction('svg', [Helpers::class, 'svg'], ['is_safe' => ['html']]));
8787

88-
$twig->addFilter(new TwigFilter('space', static fn (?string $value): string => $value !== '' ? ' '.$value : '', ['is_safe' => ['html']]));
88+
$space_filter = static fn (?string $value): string => $value !== '' ? ' '.$value : '';
89+
$twig->addFilter(new TwigFilter('space', $space_filter, ['is_safe' => ['html']]));
8990

9091
foreach ($this->globals as $name => $value) {
9192
$twig->addGlobal($name, $value);

0 commit comments

Comments
 (0)