Skip to content

Commit 07b543f

Browse files
committed
Fix memory issues
1 parent af241ab commit 07b543f

10 files changed

Lines changed: 92 additions & 46 deletions

File tree

src/Config.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
use JsonException;
1212

1313
class Config {
14+
/**
15+
* @var array<string, mixed>|null
16+
*/
17+
private static ?array $config = null;
18+
19+
/**
20+
* This is intended for use in tests.
21+
*/
22+
public static function reset(): void {
23+
self::$config = null;
24+
}
25+
1426
/**
1527
* @template Default
1628
*
@@ -19,6 +31,10 @@ class Config {
1931
* @return mixed|Default
2032
*/
2133
public static function get(string $key, $default = null) {
34+
if (self::$config !== null) {
35+
return self::$config[$key] ?? $default;
36+
}
37+
2238
if (is_file(__DIR__.'/../config.php')) {
2339
$config = (array) require __DIR__.'/../config.php';
2440
} elseif (is_file(__DIR__.'/../config.dist.php')) {
@@ -33,7 +49,9 @@ public static function get(string $key, $default = null) {
3349
$key = 'encoding';
3450
}
3551

36-
return $config[$key] ?? $default;
52+
self::$config = $config;
53+
54+
return self::$config[$key] ?? $default;
3755
}
3856

3957
/**

src/Dashboards/Redis/RedisTrait.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public function getAllKeys(): array {
421421
$keys_array = $this->redis->keys($filter);
422422
}
423423

424-
return array_map(static fn ($key): array => ['key' => $key], $keys_array);
424+
return $keys_array;
425425
}
426426

427427
public function isCommandSupported(string $command): bool {
@@ -438,14 +438,27 @@ public function isCommandSupported(string $command): bool {
438438
/**
439439
* @param array<int|string, mixed> $keys
440440
*
441+
* @return array<int|string, mixed>
442+
*
443+
* @throws Exception
444+
*/
445+
private function pipeline(array $keys): array {
446+
$keys = array_map(static fn ($key): array => ['key' => $key], $keys);
447+
$keys_array = array_column($keys, 'key');
448+
449+
return $this->redis->pipelineKeys($keys_array);
450+
}
451+
452+
/**
453+
* @param array<int|string, mixed> $keys_array
454+
*
441455
* @return array<int, array<string, string|int>>
442456
*
443457
* @throws Exception
444458
*/
445-
public function keysTableView(array $keys): array {
459+
public function keysTableView(array $keys_array): array {
460+
$pipeline = $this->pipeline($keys_array);
446461
$formatted_keys = [];
447-
$keys_array = array_column($keys, 'key');
448-
$pipeline = $this->redis->pipelineKeys($keys_array);
449462

450463
foreach ($keys_array as $key) {
451464
$formatted_keys[] = [
@@ -464,15 +477,14 @@ public function keysTableView(array $keys): array {
464477
}
465478

466479
/**
467-
* @param array<int|string, mixed> $keys
480+
* @param array<int|string, mixed> $keys_array
468481
*
469482
* @return array<int, array<string, string|int>>
470483
*
471484
* @throws Exception
472485
*/
473-
public function keysTreeView(array $keys): array {
474-
$keys_array = array_column($keys, 'key');
475-
$pipeline = $this->redis->pipelineKeys($keys_array);
486+
public function keysTreeView(array $keys_array): array {
487+
$pipeline = $this->pipeline($keys_array);
476488
$separator = $this->servers[$this->current_server]['separator'] ?? ':';
477489
$this->template->addGlobal('separator', $separator);
478490

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function autoload(string $path): void {
5454
});
5555
}
5656

57-
if (!extension_loaded('xdebug')) {
57+
if (!extension_loaded('xdebug') && php_sapi_name() !== 'cli') {
5858
set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline): bool {
5959
if ((error_reporting() & $errno) === 0) {
6060
return false;

templates/components/badge.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<span class="inline-block py-1 px-3 rounded-full {{ bg }} text-xs {{ txt ? txt : 'text-white dark:text-gray-100' }}{{ class|space }}">{{ text|raw }}</span>
1+
{% import 'components/macros.twig' as components %}
2+
{{ components.badge(text, class, bg, txt) }}

templates/components/checkbox.twig

Lines changed: 0 additions & 5 deletions
This file was deleted.

templates/components/macros.twig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{% macro checkbox(class, disabled, id) %}
2+
<input type="checkbox"{{ (id ? ' id="' ~ id ~ '" name="' ~ id ~ '"' : '')|raw }}
3+
class="checkbox text-primary-500 -mt-1.5 h-4 w-4 select-none appearance-none rounded border border-gray-300 bg-white align-middle
4+
checked:border-primary-200 checked:bg-primary-500 focus:outline-hidden focus:ring-3 focus:ring-primary-200 focus:border-primary-300
5+
dark:bg-gray-900 dark:border-gray-600 dark:checked:bg-primary-500 dark:checked:border-primary-800 dark:focus:ring-primary-400 dark:focus:border-primary-500{{ class|space }}"
6+
aria-label=""{{ (disabled is defined and disabled) ? ' disabled' : '' }}>
7+
{% endmacro %}
8+
9+
{% macro badge(text, class, bg, txt) %}
10+
<span class="inline-block py-1 px-3 rounded-full {{ bg }} text-xs {{ txt ? txt : 'text-white dark:text-gray-100' }}{{ class|space }}">{{ text|raw }}</span>
11+
{% endmacro %}
12+
13+
{% macro key_type(type) %}
14+
{% if type == 'string' %}
15+
{% set type_color = 'bg-green-500' %}
16+
{% elseif type == 'set' %}
17+
{% set type_color = 'bg-sky-500' %}
18+
{% elseif type == 'list' %}
19+
{% set type_color = 'bg-orange-500' %}
20+
{% elseif type == 'zset' %}
21+
{% set type_color = 'bg-red-500' %}
22+
{% elseif type == 'hash' %}
23+
{% set type_color = 'bg-purple-500' %}
24+
{% elseif type == 'stream' %}
25+
{% set type_color = 'bg-slate-500' %}
26+
{% else %}
27+
{% set type_color = 'bg-blue-500' %}
28+
{% endif %}
29+
30+
{{ _self.badge(type, 'uppercase font-bold', type_color, null) }}
31+
{% endmacro %}

templates/partials/key_type_badge.twig

Lines changed: 0 additions & 19 deletions
This file was deleted.

templates/partials/table_view.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
{% import 'components/macros.twig' as components %}
12
{% set show_actions = hide_actions is not defined or hide_actions == false %}
23
<div class="overflow-hidden overflow-x-auto">
34
<table class="w-full table-auto text-left dark:text-gray-300">
45
<thead>
56
<tr class="text-primary-900 border-t border-b border-gray-200 dark:border-gray-700 dark:text-primary-400">
67
{% if show_actions %}
7-
<th class="pl-6 px-3 py-2 w-4" title="Check all">{{ include('components/checkbox.twig', {disabled: keys|length == 0, class: 'check-all'}) }}</th>
8+
<th class="pl-6 px-3 py-2 w-4" title="Check all">{{ components.checkbox('check-all', keys|length == 0) }}</th>
89
{% endif %}
910
{% for item in head_items %}
1011
{% set is_active = sortcol == item.sort %}
@@ -28,7 +29,7 @@
2829
{% for key in keys %}
2930
<tr class="hover:bg-gray-50 dark:hover:bg-white/5" data-key="{{ key.base64 ? key.key|base64 : key.key }}">
3031
{% if show_actions %}
31-
<td class="pl-6 px-3 py-2 border-b border-gray-200 dark:border-gray-700">{{ include('components/checkbox.twig', {class: 'check-key'}) }}</td>
32+
<td class="pl-6 px-3 py-2 border-b border-gray-200 dark:border-gray-700">{{ components.checkbox('check-key') }}</td>
3233
{% endif %}
3334
{% for item_key, item in key.info %}
3435
{% set is_title = item_key == 'title' or (item_key == 'link_title' and view_key) %}
@@ -38,8 +39,7 @@
3839
{% set link = view_key|replace({'__key__': key.key}) %}
3940
<a class="text-primary-500 hover:text-primary-700 font-semibold dark:text-primary-400 dark:hover:text-primary-300" href="{{ link }}">{{ item }}</a>
4041
{% elseif item_key == 'type' %}
41-
{% import 'partials/key_type_badge.twig' as key_badge %}
42-
{{- key_badge.key_type(item) -}}
42+
{{- components.key_type(item) -}}
4343
{% elseif item_key starts with 'number_' %}
4444
{{- item|number -}}
4545
{% elseif item_key starts with 'time_' %}

templates/partials/tree_view.twig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
{% import 'components/macros.twig' as components %}
12
{% set show_actions = hide_actions is not defined or hide_actions == false %}
2-
{% macro render_tree(items, level=0, show_actions, view_key) %}
3+
4+
{% macro render_tree(items, level=0, show_actions, view_key, components) %}
35
{% for key, item in items %}
46
{% if item.type == 'folder' %}
57
<div class="border-t border-gray-200 dark:border-gray-700" style="--level: {{ level }}">
@@ -11,15 +13,15 @@
1113
<span class="items-count text-gray-500 dark:text-gray-400 text-xs">({{ item.count }})</span>
1214
</div>
1315
<div class="tree-children hidden" data-path="{{ item.path }}">
14-
{{ _self.render_tree(item.children, level + 1, show_actions, view_key) }}
16+
{{ _self.render_tree(item.children, level + 1, show_actions, view_key, components) }}
1517
</div>
1618
</div>
1719
{% else %}
1820
<div class="keywrapper border-t border-gray-200 dark:border-gray-700" style="--level: {{ level }}">
1921
<div data-key="{{ item.base64 ? item.key|base64 : item.key }}" class="flex justify-between px-6 py-1 hover:bg-gray-50 dark:hover:bg-white/5" style="padding-left: calc({{ level }} * 20px + 32px)">
2022
<div class="flex items-center gap-2">
2123
{% if show_actions %}
22-
{{ include('components/checkbox.twig', {class: 'check-key mt-0'}) }}
24+
{{ components.checkbox('check-key mt-0') }}
2325
{% endif %}
2426

2527
<span class="text-primary-500 hover:text-primary-700 font-semibold dark:text-primary-400 dark:hover:text-primary-300">
@@ -33,8 +35,7 @@
3335
<div class="flex gap-4">
3436
{% for item_key, kitem in item.info %}
3537
{% if item_key == 'type' %}
36-
{% import 'partials/key_type_badge.twig' as key_badge %}
37-
{{- key_badge.key_type(kitem) -}}
38+
{{- components.key_type(kitem) -}}
3839
{% elseif item_key starts with 'number_' %}
3940
{{- kitem|number -}}
4041
{% elseif item_key starts with 'time_' %}
@@ -62,12 +63,12 @@
6263

6364
<div class="treeview border-b border-t border-gray-200 dark:border-gray-700">
6465
<div class="flex gap-2 px-6 py-2">
65-
<div title="Check all">{{ include('components/checkbox.twig', {disabled: keys|length == 0, class: 'check-all'}) }}</div>
66+
<div title="Check all">{{ components.checkbox('check-all', keys|length == 0) }}</div>
6667
<button class="expand-toggle text-xs px-2 py-1 rounded text-gray-900 bg-gray-100 hover:bg-gray-300 dark:text-gray-100 dark:bg-gray-900 dark:hover:bg-gray-700">
6768
Expand all
6869
</button>
6970
</div>
7071
<div class="tree-content">
71-
{{ _self.render_tree(keys, 0, show_actions, view_key) }}
72+
{{ _self.render_tree(keys, 0, show_actions, view_key, components) }}
7273
</div>
7374
</div>

tests/ConfigTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
use RobiNN\Pca\Config;
1414

1515
final class ConfigTest extends TestCase {
16+
protected function tearDown(): void {
17+
parent::tearDown();
18+
Config::reset();
19+
}
20+
1621
public function testGetter(): void {
1722
$this->assertTrue(Config::get('true', true));
1823
$this->assertSame([], Config::get('array', []));
@@ -48,6 +53,8 @@ public function testEnvOverride(): void {
4853
// default in config
4954
$this->assertSame('d. m. Y H:i:s', Config::get('time-format', ''));
5055

56+
Config::reset();
57+
5158
putenv('PCA_TIME-FORMAT=d. m. Y');
5259

5360
$this->assertSame('d. m. Y', Config::get('time-format', ''));

0 commit comments

Comments
 (0)