Skip to content

Commit 7f58924

Browse files
committed
Small fixes, added addPath() tpl method for custom dashboards
1 parent 83da477 commit 7f58924

8 files changed

Lines changed: 62 additions & 31 deletions

File tree

index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646

4747
foreach ($admin->getDashboards() as $d_key => $d_dashboard) {
4848
$d_info = $d_dashboard->getDashboardInfo();
49-
$nav[$d_key] = $d_info['title'];
49+
$nav[$d_key] = [
50+
'title' => $d_info['title'],
51+
'icon' => $d_info['icon'] ?? $d_key,
52+
];
5053
}
5154

5255
$current = $admin->currentDashboard();

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private function moreInfo(array $servers): string {
125125
* @return array<int, array<string, string|int>>
126126
*/
127127
private function getAllKeys($memcached): array {
128-
$keys = [];
128+
static $keys = [];
129129

130130
foreach ($memcached->getKeys() as $key_data) {
131131
$keys[] = [

src/Dashboards/Redis/RedisTrait.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private function getDatabases(Redis $redis): array {
209209
* @return array<int, array<string, string|int>>
210210
*/
211211
private function getAllKeys(Redis $redis): array {
212-
$keys = [];
212+
static $keys = [];
213213
$filter = Http::get('s');
214214
$filter = !empty($filter) ? $filter : '*';
215215

@@ -229,26 +229,6 @@ private function getAllKeys(Redis $redis): array {
229229
return $keys;
230230
}
231231

232-
/**
233-
* Get a key type.
234-
*
235-
* @param int $type
236-
*
237-
* @return string
238-
*/
239-
private function getType(int $type): string {
240-
$data_types = [
241-
Redis::REDIS_STRING => 'string',
242-
Redis::REDIS_SET => 'set',
243-
Redis::REDIS_LIST => 'list',
244-
Redis::REDIS_ZSET => 'zset',
245-
Redis::REDIS_HASH => 'hash',
246-
Redis::REDIS_NOT_FOUND => 'other',
247-
];
248-
249-
return $data_types[$type];
250-
}
251-
252232
/**
253233
* Main dashboard content.
254234
*

src/Dashboards/Redis/TypesTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
use RobiNN\Pca\Http;
1818

1919
trait TypesTrait {
20+
/**
21+
* Get a key type.
22+
*
23+
* @param int $type
24+
*
25+
* @return string
26+
*/
27+
private function getType(int $type): string {
28+
$data_types = [
29+
Redis::REDIS_STRING => 'string',
30+
Redis::REDIS_SET => 'set',
31+
Redis::REDIS_LIST => 'list',
32+
Redis::REDIS_ZSET => 'zset',
33+
Redis::REDIS_HASH => 'hash',
34+
Redis::REDIS_NOT_FOUND => 'other',
35+
];
36+
37+
return $data_types[$type];
38+
}
39+
2040
/**
2141
* Get key's value. (For edit form)
2242
*

src/Helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ public static function returnJson(array $data): string {
170170
/**
171171
* Get svg icon from file.
172172
*
173-
* @param string $icon
173+
* @param string $icon Icon name from `assets/icons/` or custom path.
174174
* @param ?int $size
175175
* @param ?string $class
176176
*
177177
* @return ?string
178178
*/
179179
public static function svg(string $icon, ?int $size = 16, ?string $class = null): ?string {
180-
$file = __DIR__.'/../assets/icons/'.$icon.'.svg';
180+
$file = is_file($icon) ? $icon : __DIR__.'/../assets/icons/'.$icon.'.svg';
181181

182182
if (is_file($file)) {
183183
$content = trim(file_get_contents($file));

src/Template.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Exception;
1616
use Twig\Environment;
17+
use Twig\Error\LoaderError;
1718
use Twig\Extension\DebugExtension;
1819
use Twig\Loader\FilesystemLoader;
1920
use Twig\TwigFilter;
@@ -25,6 +26,11 @@ class Template {
2526
*/
2627
private array $globals = [];
2728

29+
/**
30+
* @var array<string, string>
31+
*/
32+
private array $paths = [];
33+
2834
/**
2935
* Add global template variable.
3036
*
@@ -37,6 +43,18 @@ public function addGlobal(string $name, $value): void {
3743
$this->globals[$name] = $value;
3844
}
3945

46+
/**
47+
* Add a path with namespace.
48+
*
49+
* @param string $namespace
50+
* @param string $path
51+
*
52+
* @return void
53+
*/
54+
public function addPath(string $namespace, string $path): void {
55+
$this->paths[$namespace] = $path;
56+
}
57+
4058
/**
4159
* Render template.
4260
*
@@ -53,6 +71,14 @@ public function render(string $tpl, array $data = []): string {
5371
'debug' => Config::get('twigdebug'),
5472
]);
5573

74+
foreach ($this->paths as $namespace => $path) {
75+
try {
76+
$loader->addPath(realpath($path), $namespace);
77+
} catch (LoaderError $e) {
78+
echo $e->getMessage();
79+
}
80+
}
81+
5682
if (Config::get('twigdebug')) {
5783
$twig->addExtension(new DebugExtension());
5884
}

templates/layout.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<nav class="bg-{{ color }}-500 fixed top-0 left-0 bottom-0 block w-64 overflow-hidden overflow-y-auto py-4 pt-6 px-6 shadow-xl shadow-gray-500">
1313
<div class="mx-auto min-h-full w-full px-0">
1414
<a class="hidden pb-2 text-white md:block" href="index.php">{{ svg('logo', null) }}</a>
15-
{% for link, title in nav %}
15+
{% for link, item in nav %}
1616
<a class="flex items-center py-2 {{ current == link ? ' font-bold' : '' }} text-white hover:text-gray-200" href="?type={{ link }}">
17-
{{ svg(link, 16, 'mr-1' ~ (current == link ? '' : ' opacity-75')) }} {{ title }}
17+
{{ svg(item.icon, 16, 'mr-1' ~ (current == link ? '' : ' opacity-75')) }} {{~ item.title ~}}
1818
</a>
1919
{% endfor %}
2020
<footer class="my-4 text-center text-white">

templates/partials/info_table.twig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
<td colspan="2" class="px-4 py-2 text-xs font-bold uppercase whitespace-nowrap">{{ name|replace({'_': ' '}) }}</td>
3232
</tr>
3333
{% for sub_name, sub_value in value %}
34-
<tr class="[&:last-child>*]:border-b-0">
35-
<td class="border-b border-gray-100 px-4 py-1 text-sm font-semibold whitespace-nowrap">{{ sub_name }}</td>
36-
<td class="border-b border-gray-100 px-4 py-1 text-sm">{{ sub_value|raw }}</td>
37-
</tr>
34+
{% if sub_value is not iterable %}
35+
<tr class="[&:last-child>*]:border-b-0">
36+
<td class="border-b border-gray-100 px-4 py-1 text-sm font-semibold whitespace-nowrap">{{ sub_name }}</td>
37+
<td class="border-b border-gray-100 px-4 py-1 text-sm">{{ sub_value|raw }}</td>
38+
</tr>
39+
{% endif %}
3840
{% endfor %}
3941
{% else %}
4042
<tr class="[&:last-child>*]:border-b-0">

0 commit comments

Comments
 (0)