Skip to content

Commit 5a2b62d

Browse files
committed
Add support for Predis client (no extension required)
1 parent 14356f0 commit 5a2b62d

10 files changed

Lines changed: 452 additions & 174 deletions

File tree

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"ext-apcu": "Required for the APCu dashboard.",
3535
"ext-memcache": "Required for the Memcache dashboard.",
3636
"ext-memcached": "Required for the Memcached dashboard.",
37-
"ext-redis": "Required for use the Redis dashboard."
37+
"ext-redis": "Required for use the Redis dashboard.",
38+
"predis/predis": "Required for use the Redis dashboard, when Redis extension is not installed."
3839
},
3940
"minimum-stability": "dev",
4041
"prefer-stable": true,
@@ -49,7 +50,8 @@
4950
}
5051
},
5152
"scripts": {
52-
"phar": "phar-composer build twig/twig",
53+
"phar:twig": "phar-composer build twig/twig",
54+
"phar:predis": "phar-composer build predis/predis",
5355
"phpstan": "phpstan --ansi",
5456
"test": "phpunit --colors=always"
5557
}

index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
if (is_file(__DIR__.'/vendor/autoload.php')) {
1919
require_once __DIR__.'/vendor/autoload.php';
2020
} else {
21-
require_once 'phar://twig.phar/vendor/autoload.php';
21+
if (is_file(__DIR__.'/twig.phar')) {
22+
require_once 'phar://twig.phar/vendor/autoload.php';
23+
}
24+
25+
if (!extension_loaded('redis') && is_file(__DIR__.'/predis.phar')) {
26+
require_once 'phar://predis.phar/vendor/autoload.php';
27+
}
2228

2329
spl_autoload_register(static function ($class_name) {
2430
$class_name = str_replace("RobiNN\\Pca\\", '', $class_name);

predis.phar

710 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* This file is part of phpCacheAdmin.
4+
*
5+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace RobiNN\Pca\Dashboards\Redis\Compatibility;
14+
15+
use RobiNN\Pca\Dashboards\DashboardException;
16+
17+
interface CompatibilityInterface {
18+
/**
19+
* Get all data types.
20+
*
21+
* Used in form.
22+
*
23+
* @return array<string, string>
24+
*/
25+
public function getAllTypes(): array;
26+
27+
/**
28+
* Get a key type.
29+
*
30+
* @param string $key
31+
*
32+
* @return string
33+
* @throws DashboardException
34+
*/
35+
public function getType(string $key): string;
36+
37+
/**
38+
* Alias to a lRem() but with the same order of parameters.
39+
*
40+
* @param string $key
41+
* @param string $value
42+
* @param int $count
43+
*
44+
* @return int
45+
*/
46+
public function listRem(string $key, string $value, int $count): int;
47+
48+
/**
49+
* Get server info.
50+
*
51+
* @param string|null $option
52+
*
53+
* @return array<int|string, mixed>
54+
*/
55+
public function getInfo(string $option = null): array;
56+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* This file is part of phpCacheAdmin.
4+
*
5+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace RobiNN\Pca\Dashboards\Redis\Compatibility;
14+
15+
use Predis\Client;
16+
use RobiNN\Pca\Dashboards\DashboardException;
17+
18+
class Predis extends Client implements CompatibilityInterface {
19+
/**
20+
* @var array<string, string>
21+
*/
22+
private array $data_types = [
23+
'none' => 'none',
24+
'other' => 'other',
25+
'string' => 'string',
26+
'set' => 'set',
27+
'list' => 'list',
28+
'zset' => 'zset',
29+
'hash' => 'hash',
30+
'stream' => 'stream',
31+
];
32+
33+
/**
34+
* Get all data types.
35+
*
36+
* Used in form.
37+
*
38+
* @return array<string, string>
39+
*/
40+
public function getAllTypes(): array {
41+
static $types = [];
42+
43+
unset($this->data_types['none'], $this->data_types['other'], $this->data_types['stream']);
44+
45+
foreach ($this->data_types as $type) {
46+
$types[$type] = ucfirst($type);
47+
}
48+
49+
return $types;
50+
}
51+
52+
/**
53+
* Get a key type.
54+
*
55+
* @param string $key
56+
*
57+
* @return string
58+
* @throws DashboardException
59+
*/
60+
public function getType(string $key): string {
61+
$type = (string) $this->type($key);
62+
63+
if (!isset($this->data_types[$type])) {
64+
throw new DashboardException(sprintf('Unsupported data type: %s', $type));
65+
}
66+
67+
return $this->data_types[$type];
68+
}
69+
70+
/**
71+
* Alias to a lRem() but with the same order of parameters.
72+
*
73+
* @param string $key
74+
* @param string $value
75+
* @param int $count
76+
*
77+
* @return int
78+
*/
79+
public function listRem(string $key, string $value, int $count): int {
80+
return $this->lrem($key, $count, $value);
81+
}
82+
83+
/**
84+
* Get server info.
85+
*
86+
* @param string|null $option
87+
*
88+
* @return array<int|string, mixed>
89+
*/
90+
public function getInfo(string $option = null): array {
91+
static $array = [];
92+
93+
foreach (['Server', 'Clients', 'Memory', 'Persistence', 'Stats', 'Replication', 'CPU', 'Cluster', 'Keyspace'] as $option_name) {
94+
$data = $this->info()[$option_name];
95+
96+
if ($option_name === 'Keyspace') {
97+
foreach ($data as $db => $keys_data) {
98+
$keys = [];
99+
foreach ($keys_data as $key_name => $key_value) {
100+
$keys[] = $key_name.'='.$key_value;
101+
}
102+
103+
$data[$db] = implode(',', $keys);
104+
}
105+
}
106+
107+
$array[strtolower($option_name)] = $data;
108+
}
109+
110+
return $array[$option] ?? $array;
111+
}
112+
113+
/**
114+
* Get a range of messages from a given stream.
115+
*
116+
* @param string $stream
117+
* @param string $start
118+
* @param string $end
119+
*
120+
* @return array<int|string, mixed>
121+
*/
122+
public function xRange(string $stream, string $start, string $end): array {
123+
return $this->executeRaw(['XRANGE', $stream, $start, $end]);
124+
}
125+
126+
/**
127+
* Add a message to a stream.
128+
*
129+
* @param string $key
130+
* @param string $id
131+
* @param array<int, string> $messages
132+
* @param int $maxLen
133+
* @param bool $isApproximate
134+
*
135+
* @return string
136+
*/
137+
public function xAdd(string $key, string $id, array $messages, int $maxLen = 0, bool $isApproximate = false): string {
138+
return $this->executeRaw(['XADD', $key, $id, $messages, $maxLen, $isApproximate]);
139+
}
140+
141+
/**
142+
* Delete one or more messages from a stream.
143+
*
144+
* @param string $key
145+
* @param array<int, string> $ids
146+
*
147+
* @return int
148+
*/
149+
public function xDel(string $key, array $ids): int {
150+
return $this->executeRaw(['XDEL', $key, implode(' ', $ids)]);
151+
}
152+
153+
/**
154+
* Get the length of a given stream.
155+
*
156+
* @param string $stream
157+
*
158+
* @return int
159+
*/
160+
public function xLen(string $stream): int {
161+
return (int) $this->executeRaw(['XLEN', $stream]);
162+
}
163+
164+
/**
165+
* Execute any generic command.
166+
*
167+
* @param string $command
168+
* @param mixed ...$arguments
169+
*
170+
* @return mixed
171+
*/
172+
public function rawCommand(string $command, ...$arguments) {
173+
return $this->executeRaw(func_get_args());
174+
}
175+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* This file is part of phpCacheAdmin.
4+
*
5+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace RobiNN\Pca\Dashboards\Redis\Compatibility;
14+
15+
use RedisException;
16+
use RobiNN\Pca\Dashboards\DashboardException;
17+
18+
class Redis extends \Redis implements CompatibilityInterface {
19+
/**
20+
* @var array<int, string>
21+
*/
22+
private array $data_types = [
23+
\Redis::REDIS_NOT_FOUND => 'other',
24+
\Redis::REDIS_STRING => 'string',
25+
\Redis::REDIS_SET => 'set',
26+
\Redis::REDIS_LIST => 'list',
27+
\Redis::REDIS_ZSET => 'zset',
28+
\Redis::REDIS_HASH => 'hash',
29+
\Redis::REDIS_STREAM => 'stream',
30+
];
31+
32+
/**
33+
* Get all data types.
34+
*
35+
* Used in form.
36+
*
37+
* @return array<string, string>
38+
*/
39+
public function getAllTypes(): array {
40+
static $types = [];
41+
42+
unset($this->data_types[\Redis::REDIS_NOT_FOUND], $this->data_types[\Redis::REDIS_STREAM]);
43+
44+
foreach ($this->data_types as $type) {
45+
$types[$type] = ucfirst($type);
46+
}
47+
48+
return $types;
49+
}
50+
51+
/**
52+
* Get a key type.
53+
*
54+
* @param string $key
55+
*
56+
* @return string
57+
* @throws RedisException|DashboardException
58+
*/
59+
public function getType(string $key): string {
60+
$type = $this->type($key);
61+
62+
if (!isset($this->data_types[$type])) {
63+
throw new DashboardException(sprintf('Unsupported data type: %s', $type));
64+
}
65+
66+
return $this->data_types[$type];
67+
}
68+
69+
/**
70+
* Alias to a lRem() but with the same order of parameters.
71+
*
72+
* @param string $key
73+
* @param string $value
74+
* @param int $count
75+
*
76+
* @return int
77+
* @throws RedisException
78+
*/
79+
public function listRem(string $key, string $value, int $count): int {
80+
return $this->lRem($key, $value, $count);
81+
}
82+
83+
/**
84+
* Get server info.
85+
*
86+
* @param string|null $option
87+
*
88+
* @return array<int|string, mixed>
89+
* @throws RedisException
90+
*/
91+
public function getInfo(string $option = null): array {
92+
static $info = [];
93+
94+
foreach (['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'] as $option_name) {
95+
$info[strtolower($option_name)] = $this->info($option_name);
96+
}
97+
98+
return $info[$option] ?? $info;
99+
}
100+
}

0 commit comments

Comments
 (0)