From 3c6389f4dd6256caa9b13b0a808b5b64e7af375e Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Mon, 11 Nov 2013 16:18:00 +0100 Subject: [PATCH] Rework how connection parameters are handled. These changes do not affect the actual functionalities of the client, but make the code more explicit and less error-prone. --- lib/Predis/Client.php | 40 +++++++++++++------ lib/Predis/Connection/ConnectionFactory.php | 2 +- .../Connection/ConnectionParameters.php | 6 +-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index e2dcb15c..60c2986d 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -19,6 +19,7 @@ use Predis\Configuration\OptionsInterface; use Predis\Connection\AggregatedConnectionInterface; use Predis\Connection\ConnectionInterface; use Predis\Connection\ConnectionFactoryInterface; +use Predis\Connection\ConnectionParametersInterface; use Predis\Monitor\MonitorContext; use Predis\Pipeline\PipelineContext; use Predis\Profile\ServerProfile; @@ -48,8 +49,8 @@ class Client implements ClientInterface */ public function __construct($parameters = null, $options = null) { - $this->options = $this->createOptions($options); - $this->connection = $this->createConnection($parameters); + $this->options = $this->createOptions($options ?: array()); + $this->connection = $this->createConnection($parameters ?: array()); $this->profile = $this->options->profile; } @@ -63,10 +64,6 @@ class Client implements ClientInterface */ protected function createOptions($options) { - if (!isset($options)) { - return new Options(); - } - if (is_array($options)) { return new Options($options); } @@ -83,6 +80,14 @@ class Client implements ClientInterface * (string, array) or returns the passed argument if it is an instance of a * class implementing Predis\Connection\ConnectionInterface. * + * Accepted types for connection parameters are: + * + * - Instance of Predis\Connection\ConnectionInterface. + * - Instance of Predis\Connection\ConnectionParametersInterface. + * - Array + * - String + * - Callable + * * @param mixed $parameters Connection parameters or connection instance. * @return ConnectionInterface */ @@ -92,12 +97,23 @@ class Client implements ClientInterface return $parameters; } - if (is_array($parameters) && isset($parameters[0])) { - $options = $this->options; - $replication = isset($options->replication) && $options->replication; - $connection = $options->{$replication ? 'replication' : 'cluster'}; + if ($parameters instanceof ConnectionParametersInterface || is_string($parameters)) { + return $this->options->connections->create($parameters); + } - return $options->connections->createAggregated($connection, $parameters); + if (is_array($parameters)) { + $options = $this->options; + + if (isset($parameters[0])) { + $replication = isset($options->replication) && $options->replication; + $connection = $options->{$replication ? 'replication' : 'cluster'}; + + $options->connections->createAggregated($connection, $parameters); + + return $connection; + } + + return $options->connections->create($parameters); } if (is_callable($parameters)) { @@ -112,7 +128,7 @@ class Client implements ClientInterface return $connection; } - return $this->options->connections->create($parameters); + throw new InvalidArgumentException('Invalid type for connection parameters'); } /** diff --git a/lib/Predis/Connection/ConnectionFactory.php b/lib/Predis/Connection/ConnectionFactory.php index 5527232b..36bffb34 100644 --- a/lib/Predis/Connection/ConnectionFactory.php +++ b/lib/Predis/Connection/ConnectionFactory.php @@ -97,7 +97,7 @@ class ConnectionFactory implements ConnectionFactoryInterface public function create($parameters) { if (!$parameters instanceof ConnectionParametersInterface) { - $parameters = new ConnectionParameters($parameters ?: array()); + $parameters = new ConnectionParameters($parameters); } $scheme = $parameters->scheme; diff --git a/lib/Predis/Connection/ConnectionParameters.php b/lib/Predis/Connection/ConnectionParameters.php index a244c4aa..38413bfa 100644 --- a/lib/Predis/Connection/ConnectionParameters.php +++ b/lib/Predis/Connection/ConnectionParameters.php @@ -32,13 +32,13 @@ class ConnectionParameters implements ConnectionParametersInterface /** * @param string|array Connection parameters in the form of an URI string or a named array. */ - public function __construct($parameters = array()) + public function __construct($parameters = null) { - if (!is_array($parameters)) { + if (is_string($parameters)) { $parameters = self::parseURI($parameters); } - $this->parameters = $this->filter($parameters) + $this->getDefaults(); + $this->parameters = $this->filter($parameters ?: array()) + $this->getDefaults(); } /**