|
| 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 | +} |
0 commit comments