Ver código fonte

Fix DispatcherLoop error with client prefix keys

DispatcherLoop works properly if a client have configured a
prefix for the keys or not
Eloi Poch 12 anos atrás
pai
commit
6cd124cc59
1 arquivos alterados com 19 adições e 3 exclusões
  1. 19 3
      lib/Predis/PubSub/DispatcherLoop.php

+ 19 - 3
lib/Predis/PubSub/DispatcherLoop.php

@@ -96,8 +96,10 @@ class DispatcherLoop
      */
     public function attachCallback($channel, $callback)
     {
+        $callbackName = $this->getPrefixKeys() . $channel;
+
         $this->validateCallback($callback);
-        $this->callbacks[$channel] = $callback;
+        $this->callbacks[$callbackName] = $callback;
         $this->pubSubContext->subscribe($channel);
     }
 
@@ -108,8 +110,10 @@ class DispatcherLoop
      */
     public function detachCallback($channel)
     {
-        if (isset($this->callbacks[$channel])) {
-            unset($this->callbacks[$channel]);
+        $callbackName = $this->getPrefixKeys() . $channel;
+
+        if (isset($this->callbacks[$callbackName])) {
+            unset($this->callbacks[$callbackName]);
             $this->pubSubContext->unsubscribe($channel);
         }
     }
@@ -148,4 +152,16 @@ class DispatcherLoop
     {
         $this->pubSubContext->closeContext();
     }
+
+    /**
+     * Return the prefix of the keys
+     *
+     * @return string
+     */
+    protected function getPrefixKeys()
+    {
+        $prefix = $this->client->getOptions()->prefix;
+
+        return $prefix ? $prefix->getPrefix() : '';
+    }
 }