Переглянути джерело

Add new interfaces used to abstract the main client class and contexts.

Now developers can pass a client object or an executable context (see
Predis\Transaction\MultiExecContext or Predis\Pipeline\PipelineContext)
interchangeably as parameters to their methods using the new interface
Predis\BasicCliantInterface.

These new interfaces will also allow us to easily create new client
classes aside from the standard Predis\Client one.
Daniele Alessandri 13 роки тому
батько
коміт
51d0b58cd3

+ 31 - 0
lib/Predis/BasicClientInterface.php

@@ -0,0 +1,31 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis;
+
+use Predis\Command\CommandInterface;
+
+/**
+ * Defines the interface of a basic client object or abstraction that
+ * can send commands to Redis.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+interface BasicClientInterface
+{
+    /**
+     * Executes the specified Redis command.
+     *
+     * @param CommandInterface $command A Redis command.
+     * @return mixed
+     */
+    public function executeCommand(CommandInterface $command);
+}

+ 6 - 21
lib/Predis/Client.php

@@ -27,7 +27,7 @@ use Predis\Transaction\MultiExecContext;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-class Client
+class Client implements ClientInterface
 {
     const VERSION = '0.8.0-dev';
 
@@ -102,9 +102,7 @@ class Client
     }
 
     /**
-     * Returns the server profile used by the client.
-     *
-     * @return ServerProfileInterface
+     * {@inheritdoc}
      */
     public function getProfile()
     {
@@ -112,9 +110,7 @@ class Client
     }
 
     /**
-     * Returns the client options specified upon initialization.
-     *
-     * @return ClientOptions
+     * {@inheritdoc}
      */
     public function getOptions()
     {
@@ -185,11 +181,7 @@ class Client
     }
 
     /**
-     * Returns the underlying connection instance or, when connected to a cluster,
-     * one of the connection instances identified by its alias.
-     *
-     * @param string $id The alias of a connection when connected to a cluster.
-     * @return ConnectionInterface
+     * {@inheritdoc}
      */
     public function getConnection($id = null)
     {
@@ -218,11 +210,7 @@ class Client
     }
 
     /**
-     * Creates a new instance of the specified Redis command.
-     *
-     * @param string $method The name of a Redis command.
-     * @param array $arguments The arguments for the command.
-     * @return CommandInterface
+     * {@inheritdoc}
      */
     public function createCommand($method, $arguments = array())
     {
@@ -230,10 +218,7 @@ class Client
     }
 
     /**
-     * Executes the specified Redis command.
-     *
-     * @param CommandInterface $command A Redis command.
-     * @return mixed
+     * {@inheritdoc}
      */
     public function executeCommand(CommandInterface $command)
     {

+ 68 - 0
lib/Predis/ClientInterface.php

@@ -0,0 +1,68 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis;
+
+use Predis\Option\ClientOptionsInterface;
+use Predis\Connection\ConnectionInterface;
+use Predis\Profile\ServerProfileInterface;
+
+/**
+ * Interface defining the most important parts needed to create an
+ * high-level Redis client object that can interact with other
+ * building blocks of Predis.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+interface ClientInterface extends BasicClientInterface
+{
+    /**
+     * Returns the server profile used by the client.
+     *
+     * @return ServerProfileInterface
+     */
+    public function getProfile();
+
+    /**
+     * Returns the client options specified upon initialization.
+     *
+     * @return ClientOptionsInterface
+     */
+    public function getOptions();
+
+    /**
+     * Opens the connection to the server.
+     */
+    public function connect();
+
+    /**
+     * Disconnects from the server.
+     */
+    public function disconnect();
+
+    /**
+     * Returns the underlying connection instance or, when connected to a cluster,
+     * one of the connection instances identified by its alias.
+     *
+     * @param string $id The alias of a connection when connected to a cluster.
+     * @return ConnectionInterface
+     */
+    public function getConnection($id = null);
+
+    /**
+     * Creates a new instance of the specified Redis command.
+     *
+     * @param string $method The name of a Redis command.
+     * @param array $arguments The arguments for the command.
+     * @return CommandInterface
+     */
+    public function createCommand($method, $arguments = array());
+}

+ 29 - 0
lib/Predis/ExecutableContextInterface.php

@@ -0,0 +1,29 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis;
+
+/**
+ * Defines the interface of a basic client object or abstraction that
+ * can send commands to Redis.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+interface ExecutableContextInterface
+{
+    /**
+     * Starts the execution of the context.
+     *
+     * @param mixed $callable Optional callback for execution.
+     * @return array
+     */
+    public function execute($callable = null);
+}

+ 5 - 7
lib/Predis/Monitor/MonitorContext.php

@@ -11,7 +11,7 @@
 
 namespace Predis\Monitor;
 
-use Predis\Client;
+use Predis\ClientInterface;
 use Predis\Helpers;
 use Predis\NotSupportedException;
 
@@ -27,9 +27,9 @@ class MonitorContext implements \Iterator
     private $position;
 
     /**
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      */
-    public function __construct(Client $client)
+    public function __construct(ClientInterface $client)
     {
         $this->checkCapabilities($client);
         $this->client = $client;
@@ -48,9 +48,9 @@ class MonitorContext implements \Iterator
      * Checks if the passed client instance satisfies the required conditions
      * needed to initialize a monitor context.
      *
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      */
-    private function checkCapabilities(Client $client)
+    private function checkCapabilities(ClientInterface $client)
     {
         if (Helpers::isCluster($client->getConnection())) {
             throw new NotSupportedException('Cannot initialize a monitor context over a cluster of connections');
@@ -63,8 +63,6 @@ class MonitorContext implements \Iterator
 
     /**
      * Initializes the context and sends the MONITOR command to the server.
-     *
-     * @param Client Client instance used by the context.
      */
     protected function openContext()
     {

+ 12 - 10
lib/Predis/Pipeline/PipelineContext.php

@@ -11,11 +11,13 @@
 
 namespace Predis\Pipeline;
 
-use Predis\Client;
-use Predis\Helpers;
-use Predis\ClientException;
+use Predis\ClientInterface;
+use Predis\BasicClientInterface;
+use Predis\ExecutableContextInterface;
 use Predis\Command\CommandInterface;
 use Predis\Connection\ReplicationConnectionInterface;
+use Predis\Helpers;
+use Predis\ClientException;
 
 /**
  * Abstraction of a pipeline context where write and read operations
@@ -23,7 +25,7 @@ use Predis\Connection\ReplicationConnectionInterface;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-class PipelineContext
+class PipelineContext implements BasicClientInterface, ExecutableContextInterface
 {
     private $client;
     private $executor;
@@ -33,10 +35,10 @@ class PipelineContext
     private $running  = false;
 
     /**
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      * @param array Options for the context initialization.
      */
-    public function __construct(Client $client, Array $options = null)
+    public function __construct(ClientInterface $client, Array $options = null)
     {
         $this->client = $client;
         $this->executor = $this->createExecutor($client, $options ?: array());
@@ -46,11 +48,11 @@ class PipelineContext
      * Returns a pipeline executor depending on the kind of the underlying
      * connection and the passed options.
      *
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      * @param array Options for the context initialization.
      * @return PipelineExecutorInterface
      */
-    protected function createExecutor(Client $client, Array $options)
+    protected function createExecutor(ClientInterface $client, Array $options)
     {
         if (!$options) {
             return new StandardExecutor();
@@ -154,7 +156,7 @@ class PipelineContext
     /**
      * Handles the actual execution of the whole pipeline.
      *
-     * @param mixed $callable Callback for execution.
+     * @param mixed $callable Optional callback for execution.
      * @return array
      */
     public function execute($callable = null)
@@ -188,7 +190,7 @@ class PipelineContext
     /**
      * Returns the underlying client instance used by the pipeline object.
      *
-     * @return Client
+     * @return ClientInterface
      */
     public function getClient()
     {

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

@@ -11,7 +11,7 @@
 
 namespace Predis\PubSub;
 
-use Predis\Client;
+use Predis\ClientInterface;
 
 /**
  * Method-dispatcher loop built around the client-side abstraction of a Redis
@@ -28,9 +28,9 @@ class DispatcherLoop
     private $subscriptionCallback;
 
     /**
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      */
-    public function __construct(Client $client)
+    public function __construct(ClientInterface $client)
     {
         $this->callbacks = array();
         $this->client = $client;

+ 5 - 5
lib/Predis/PubSub/PubSubContext.php

@@ -11,7 +11,7 @@
 
 namespace Predis\PubSub;
 
-use Predis\Client;
+use Predis\ClientInterface;
 use Predis\Helpers;
 use Predis\ClientException;
 use Predis\NotSupportedException;
@@ -39,10 +39,10 @@ class PubSubContext implements \Iterator
     private $options;
 
     /**
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      * @param array Options for the context initialization.
      */
-    public function __construct(Client $client, Array $options = null)
+    public function __construct(ClientInterface $client, Array $options = null)
     {
         $this->checkCapabilities($client);
         $this->options = $options ?: array();
@@ -65,9 +65,9 @@ class PubSubContext implements \Iterator
      * Checks if the passed client instance satisfies the required conditions
      * needed to initialize a Publish / Subscribe context.
      *
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      */
-    private function checkCapabilities(Client $client)
+    private function checkCapabilities(ClientInterface $client)
     {
         if (Helpers::isCluster($client->getConnection())) {
             throw new NotSupportedException('Cannot initialize a PUB/SUB context over a cluster of connections');

+ 10 - 8
lib/Predis/Transaction/MultiExecContext.php

@@ -11,12 +11,14 @@
 
 namespace Predis\Transaction;
 
-use Predis\Client;
+use Predis\ClientInterface;
+use Predis\BasicClientInterface;
+use Predis\ExecutableContextInterface;
+use Predis\Command\CommandInterface;
 use Predis\Helpers;
 use Predis\ResponseQueued;
 use Predis\ClientException;
 use Predis\ServerException;
-use Predis\Command\CommandInterface;
 use Predis\NotSupportedException;
 use Predis\CommunicationException;
 use Predis\Protocol\ProtocolException;
@@ -26,7 +28,7 @@ use Predis\Protocol\ProtocolException;
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
-class MultiExecContext
+class MultiExecContext implements BasicClientInterface, ExecutableContextInterface
 {
     const STATE_RESET       = 0x00000;
     const STATE_INITIALIZED = 0x00001;
@@ -43,10 +45,10 @@ class MultiExecContext
     protected $commands;
 
     /**
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      * @param array Options for the context initialization.
      */
-    public function __construct(Client $client, Array $options = null)
+    public function __construct(ClientInterface $client, Array $options = null)
     {
         $this->checkCapabilities($client);
         $this->options = $options ?: array();
@@ -109,9 +111,9 @@ class MultiExecContext
      * Checks if the passed client instance satisfies the required conditions
      * needed to initialize a transaction context.
      *
-     * @param Client Client instance used by the context.
+     * @param ClientInterface Client instance used by the context.
      */
-    private function checkCapabilities(Client $client)
+    private function checkCapabilities(ClientInterface $client)
     {
         if (Helpers::isCluster($client->getConnection())) {
             throw new NotSupportedException('Cannot initialize a MULTI/EXEC context over a cluster of connections');
@@ -326,7 +328,7 @@ class MultiExecContext
     /**
      * Handles the actual execution of the whole transaction.
      *
-     * @param mixed $callable Callback for execution.
+     * @param mixed $callable Optional callback for execution.
      * @return array
      */
     public function execute($callable = null)