ソースを参照

Make it possible to set/unset command handlers in CommandHashStrategy.

Daniele Alessandri 12 年 前
コミット
fd6a14d80b
1 ファイル変更28 行追加0 行削除
  1. 28 0
      lib/Predis/Command/Hash/CommandHashStrategy.php

+ 28 - 0
lib/Predis/Command/Hash/CommandHashStrategy.php

@@ -156,6 +156,34 @@ class CommandHashStrategy implements CommandHashStrategyInterface
         return array_keys($this->commands);
     }
 
+    /**
+     * Sets an handler for the specified command ID.
+     *
+     * The signature of the callback must have a single parameter
+     * of type Predis\Command\CommandInterface.
+     *
+     * When the callback argument is omitted or NULL, the previously
+     * associated handler for the specified command ID is removed.
+     *
+     * @param string $commandId The ID of the command to be handled.
+     * @param mixed $callback A valid callable object or NULL.
+     */
+    public function setCommandHandler($commandId, $callback = null)
+    {
+        $commandId = strtoupper($commandId);
+
+        if (!isset($callback)) {
+            unset($this->commands[$commandId]);
+            return;
+        }
+
+        if (!is_callable($callback)) {
+            throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
+        }
+
+        $this->commands[$commandId] = $callback;
+    }
+
     /**
      * Extracts the key from the first argument of a command instance.
      *