|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright MacFJA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 14 | + * Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace MacFJA\RediSearch\Redis\Client; |
| 23 | + |
| 24 | +use function function_exists; |
| 25 | +use function React\Async\await; |
| 26 | + |
| 27 | +use Clue\React\Redis\Client; |
| 28 | +use MacFJA\RediSearch\Redis\Command; |
| 29 | +use React\Promise\Promise; |
| 30 | +use React\Promise\PromiseInterface; |
| 31 | +use RuntimeException; |
| 32 | + |
| 33 | +class ReactRedisClient extends AbstractClient |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var Client |
| 37 | + */ |
| 38 | + private $redis; |
| 39 | + |
| 40 | + /** |
| 41 | + * @codeCoverageIgnore |
| 42 | + * |
| 43 | + * @param mixed $redis |
| 44 | + */ |
| 45 | + private function __construct($redis) |
| 46 | + { |
| 47 | + if (!static::supports($redis)) { |
| 48 | + throw new RuntimeException($this->getMissingMessage('Clue Redis React and/or React\\Async', false, [ |
| 49 | + Client::class => [], |
| 50 | + PromiseInterface::class => [], |
| 51 | + ], ['\React\Async\await'])); |
| 52 | + } |
| 53 | + if ($redis instanceof Promise) { |
| 54 | + $realRedis = await($redis); |
| 55 | + if (!$realRedis instanceof Client) { |
| 56 | + throw new RuntimeException('The provided $redis parameter is not a valid Redis client'); |
| 57 | + } |
| 58 | + $this->redis = $realRedis; |
| 59 | + |
| 60 | + return; |
| 61 | + } |
| 62 | + $this->redis = $redis; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @codeCoverageIgnore |
| 67 | + */ |
| 68 | + public function pipeline(Command ...$commands): array |
| 69 | + { |
| 70 | + false === static::$disableNotice |
| 71 | + && trigger_error('Warning, Clue\\React\\Redis\\Client don\'t use a real Redis Pipeline', E_USER_NOTICE); |
| 72 | + |
| 73 | + return array_map(function (Command $command) { |
| 74 | + return $this->execute($command); |
| 75 | + }, $commands); |
| 76 | + } |
| 77 | + |
| 78 | + public static function make($redis): \MacFJA\RediSearch\Redis\Client |
| 79 | + { |
| 80 | + return new self($redis); |
| 81 | + } |
| 82 | + |
| 83 | + public function executeRaw(...$args) |
| 84 | + { |
| 85 | + $command = array_shift($args); |
| 86 | + |
| 87 | + return await($this->redis->__call((string) $command, array_map('strval', $args))); |
| 88 | + } |
| 89 | + |
| 90 | + public static function supports($redis): bool |
| 91 | + { |
| 92 | + return ($redis instanceof Client || $redis instanceof PromiseInterface) && function_exists('\React\Async\await'); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @codeCoverageIgnore |
| 97 | + */ |
| 98 | + protected function doPipeline(Command ...$commands): array |
| 99 | + { |
| 100 | + return []; |
| 101 | + } |
| 102 | +} |
0 commit comments