2626use MacFJA \RediSearch \Redis \Command ;
2727use Predis \ClientContextInterface ;
2828use Predis \ClientInterface ;
29+ use Predis \Command \CommandInterface ;
2930use Predis \Command \RawCommand ;
31+ use Predis \Command \RawFactory ;
3032use RuntimeException ;
3133
3234class PredisClient extends AbstractClient
3335{
3436 /** @var ClientInterface */
3537 private $ redis ;
3638
39+ /** @var null|RawFactory */
40+ private static $ rawFactory ;
41+
3742 /**
3843 * @codeCoverageIgnore
3944 */
@@ -51,7 +56,7 @@ private function __construct(ClientInterface $redis)
5156
5257 public function execute (Command $ command )
5358 {
54- $ rawResponse = $ this ->redis ->executeCommand (new RawCommand (array_merge ([$ command ->getId ()], $ command ->getArguments ())));
59+ $ rawResponse = $ this ->redis ->executeCommand (self :: createRawCommand (array_merge ([$ command ->getId ()], $ command ->getArguments ())));
5560
5661 return $ command ->parseResponse ($ rawResponse );
5762 }
@@ -82,7 +87,7 @@ public static function make($redis): Client
8287 */
8388 public function executeRaw (...$ args )
8489 {
85- return $ this ->redis ->executeCommand (new RawCommand ($ args ));
90+ return $ this ->redis ->executeCommand (self :: createRawCommand ($ args ));
8691 }
8792
8893 protected function doPipeline (Command ...$ commands ): array
@@ -91,8 +96,27 @@ protected function doPipeline(Command ...$commands): array
9196
9297 return $ this ->redis ->pipeline (static function (ClientContextInterface $ pipeline ) use ($ commands ): void {
9398 foreach ($ commands as $ command ) {
94- $ pipeline ->executeCommand (new RawCommand (array_merge ([$ command ->getId ()], $ command ->getArguments ())));
99+ $ pipeline ->executeCommand (self :: createRawCommand (array_merge ([$ command ->getId ()], $ command ->getArguments ())));
95100 }
96101 });
97102 }
103+
104+ /**
105+ * @param array<float|int|string> $args
106+ * @codeCoverageIgnore
107+ */
108+ private static function createRawCommand (array $ args ): CommandInterface
109+ {
110+ if (class_exists (RawFactory::class)) {
111+ if (null === self ::$ rawFactory ) {
112+ self ::$ rawFactory = new RawFactory ();
113+ }
114+ $ commandID = array_shift ($ args );
115+
116+ return self ::$ rawFactory ->create ((string ) $ commandID , $ args );
117+ }
118+
119+ // @phpstan-ignore-next-line
120+ return new RawCommand ($ args );
121+ }
98122}
0 commit comments