From 51d0b58cd3225faf56fdaf5eb66267e6adad3829 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 18 Mar 2012 10:28:12 +0100 Subject: [PATCH] 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. --- lib/Predis/BasicClientInterface.php | 31 ++++++++++ lib/Predis/Client.php | 27 ++------ lib/Predis/ClientInterface.php | 68 +++++++++++++++++++++ lib/Predis/ExecutableContextInterface.php | 29 +++++++++ lib/Predis/Monitor/MonitorContext.php | 12 ++-- lib/Predis/Pipeline/PipelineContext.php | 22 ++++--- lib/Predis/PubSub/DispatcherLoop.php | 6 +- lib/Predis/PubSub/PubSubContext.php | 10 +-- lib/Predis/Transaction/MultiExecContext.php | 18 +++--- 9 files changed, 169 insertions(+), 54 deletions(-) create mode 100644 lib/Predis/BasicClientInterface.php create mode 100644 lib/Predis/ClientInterface.php create mode 100644 lib/Predis/ExecutableContextInterface.php diff --git a/lib/Predis/BasicClientInterface.php b/lib/Predis/BasicClientInterface.php new file mode 100644 index 00000000..a25f4102 --- /dev/null +++ b/lib/Predis/BasicClientInterface.php @@ -0,0 +1,31 @@ + + * + * 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 + */ +interface BasicClientInterface +{ + /** + * Executes the specified Redis command. + * + * @param CommandInterface $command A Redis command. + * @return mixed + */ + public function executeCommand(CommandInterface $command); +} diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index 28f8a07b..1337627e 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -27,7 +27,7 @@ use Predis\Transaction\MultiExecContext; * * @author Daniele Alessandri */ -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) { diff --git a/lib/Predis/ClientInterface.php b/lib/Predis/ClientInterface.php new file mode 100644 index 00000000..0e6a20b9 --- /dev/null +++ b/lib/Predis/ClientInterface.php @@ -0,0 +1,68 @@ + + * + * 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 + */ +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()); +} diff --git a/lib/Predis/ExecutableContextInterface.php b/lib/Predis/ExecutableContextInterface.php new file mode 100644 index 00000000..9b0017ae --- /dev/null +++ b/lib/Predis/ExecutableContextInterface.php @@ -0,0 +1,29 @@ + + * + * 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 + */ +interface ExecutableContextInterface +{ + /** + * Starts the execution of the context. + * + * @param mixed $callable Optional callback for execution. + * @return array + */ + public function execute($callable = null); +} diff --git a/lib/Predis/Monitor/MonitorContext.php b/lib/Predis/Monitor/MonitorContext.php index 797c6826..438214ca 100644 --- a/lib/Predis/Monitor/MonitorContext.php +++ b/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() { diff --git a/lib/Predis/Pipeline/PipelineContext.php b/lib/Predis/Pipeline/PipelineContext.php index 128b8ddb..5a784e91 100644 --- a/lib/Predis/Pipeline/PipelineContext.php +++ b/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 */ -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() { diff --git a/lib/Predis/PubSub/DispatcherLoop.php b/lib/Predis/PubSub/DispatcherLoop.php index 7bdd10cf..3f6796d0 100644 --- a/lib/Predis/PubSub/DispatcherLoop.php +++ b/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; diff --git a/lib/Predis/PubSub/PubSubContext.php b/lib/Predis/PubSub/PubSubContext.php index 03c95432..45f01423 100644 --- a/lib/Predis/PubSub/PubSubContext.php +++ b/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'); diff --git a/lib/Predis/Transaction/MultiExecContext.php b/lib/Predis/Transaction/MultiExecContext.php index b57cd723..0e5924e1 100644 --- a/lib/Predis/Transaction/MultiExecContext.php +++ b/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 */ -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)