From a5bf0eb41abdbb979962d273511ecc0a8a5df89b Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Thu, 1 Dec 2011 09:01:13 +0100 Subject: [PATCH] Partially rewrite the client options bits and remove some madness. The concept is now similar to a little dependency-injection system. --- lib/Predis/Client.php | 4 +- lib/Predis/Options/ClientCluster.php | 4 +- .../Options/ClientConnectionFactory.php | 6 +- lib/Predis/{ => Options}/ClientOptions.php | 63 +++++-------------- lib/Predis/Options/ClientPrefix.php | 2 +- lib/Predis/Options/ClientProfile.php | 4 +- lib/Predis/Options/CustomOption.php | 14 ++--- lib/Predis/Options/IClientOptions.php | 21 +++++++ lib/Predis/Options/IOption.php | 6 +- lib/Predis/Options/Option.php | 10 +-- 10 files changed, 61 insertions(+), 73 deletions(-) rename lib/Predis/{ => Options}/ClientOptions.php (59%) create mode 100644 lib/Predis/Options/IClientOptions.php diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index b83791d8..8f1a8ead 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -12,9 +12,11 @@ namespace Predis; use Predis\Commands\ICommand; +use Predis\Options\IClientOptions; use Predis\Network\IConnection; use Predis\Network\IConnectionSingle; use Predis\Profiles\IServerProfile; +use Predis\Options\ClientOptions; use Predis\Profiles\ServerProfile; use Predis\PubSub\PubSubContext; use Predis\Pipeline\PipelineContext; @@ -72,7 +74,7 @@ class Client if (is_array($options)) { return new ClientOptions($options); } - if ($options instanceof ClientOptions) { + if ($options instanceof IClientOptions) { return $options; } if ($options instanceof IServerProfile) { diff --git a/lib/Predis/Options/ClientCluster.php b/lib/Predis/Options/ClientCluster.php index 05e61382..53cf5a22 100644 --- a/lib/Predis/Options/ClientCluster.php +++ b/lib/Predis/Options/ClientCluster.php @@ -41,7 +41,7 @@ class ClientCluster extends Option /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { if (is_callable($value)) { return $this->checkInstance(call_user_func($value)); @@ -73,7 +73,7 @@ class ClientCluster extends Option /** * {@inheritdoc} */ - public function getDefault() + public function getDefault(IClientOptions $options) { return new PredisCluster(); } diff --git a/lib/Predis/Options/ClientConnectionFactory.php b/lib/Predis/Options/ClientConnectionFactory.php index 26f6b3ab..934bb56b 100644 --- a/lib/Predis/Options/ClientConnectionFactory.php +++ b/lib/Predis/Options/ClientConnectionFactory.php @@ -24,13 +24,13 @@ class ClientConnectionFactory extends Option /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { if ($value instanceof IConnectionFactory) { return $value; } if (is_array($value)) { - $factory = $this->getDefault(); + $factory = $this->getDefault($options); foreach ($value as $scheme => $initializer) { $factory->define($scheme, $initializer); } @@ -41,7 +41,7 @@ class ClientConnectionFactory extends Option /** * {@inheritdoc} */ - public function getDefault() + public function getDefault(IClientOptions $options) { return new ConnectionFactory(); } diff --git a/lib/Predis/ClientOptions.php b/lib/Predis/Options/ClientOptions.php similarity index 59% rename from lib/Predis/ClientOptions.php rename to lib/Predis/Options/ClientOptions.php index ec8ace71..cd252a03 100644 --- a/lib/Predis/ClientOptions.php +++ b/lib/Predis/Options/ClientOptions.php @@ -9,26 +9,17 @@ * file that was distributed with this source code. */ -namespace Predis; - -use Predis\Options\IOption; -use Predis\Options\ClientPrefix; -use Predis\Options\ClientProfile; -use Predis\Options\ClientCluster; -use Predis\Options\ClientConnectionFactory; +namespace Predis\Options; /** * Class that manages validation and conversion of client options. * * @author Daniele Alessandri */ -class ClientOptions +class ClientOptions implements IClientOptions { - private static $sharedOptions; - private $handlers; private $defined; - private $options = array(); /** @@ -37,7 +28,7 @@ class ClientOptions public function __construct(Array $options = array()) { $this->handlers = $this->initialize($options); - $this->defined = array_keys($options); + $this->defined = array_fill_keys(array_keys($options), true); } /** @@ -45,43 +36,14 @@ class ClientOptions * * @return array */ - private static function getSharedOptions() + protected function getDefaultOptions() { - if (isset(self::$sharedOptions)) { - return self::$sharedOptions; - } - - self::$sharedOptions = array( + return array( 'profile' => new ClientProfile(), 'connections' => new ClientConnectionFactory(), 'cluster' => new ClientCluster(), 'prefix' => new ClientPrefix(), ); - - return self::$sharedOptions; - } - - /** - * Defines an option handler or overrides an existing one. - * - * @param string $option Name of the option. - * @param IOption $handler Handler for the option. - */ - public static function define($option, IOption $handler) - { - self::getSharedOptions(); - self::$sharedOptions[$option] = $handler; - } - - /** - * Undefines the handler for the specified option. - * - * @param string $option Name of the option. - */ - public static function undefine($option) - { - self::getSharedOptions(); - unset(self::$sharedOptions[$option]); } /** @@ -90,17 +52,20 @@ class ClientOptions * @param array $options List of client options values. * @return array */ - private function initialize($options) + protected function initialize(Array $options) { - $handlers = self::getSharedOptions(); + $handlers = $this->getDefaultOptions(); foreach ($options as $option => $value) { if (isset($handlers[$option])) { $handler = $handlers[$option]; - $handlers[$option] = function() use($handler, $value) { - return $handler->validate($value); + $handlers[$option] = function($options) use($handler, $value) { + return $handler->validate($options, $value); }; } + else { + $this->options[$option] = $value; + } } return $handlers; @@ -114,7 +79,7 @@ class ClientOptions */ public function __isset($option) { - return in_array($option, $this->defined); + return isset($this->defined[$option]); } /** @@ -131,7 +96,7 @@ class ClientOptions if (isset($this->handlers[$option])) { $handler = $this->handlers[$option]; - $value = $handler instanceof IOption ? $handler->getDefault() : $handler(); + $value = $handler instanceof IOption ? $handler->getDefault($this) : $handler($this); $this->options[$option] = $value; return $value; diff --git a/lib/Predis/Options/ClientPrefix.php b/lib/Predis/Options/ClientPrefix.php index 644efe3c..bebff882 100644 --- a/lib/Predis/Options/ClientPrefix.php +++ b/lib/Predis/Options/ClientPrefix.php @@ -23,7 +23,7 @@ class ClientPrefix extends Option /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { return new KeyPrefixProcessor($value); } diff --git a/lib/Predis/Options/ClientProfile.php b/lib/Predis/Options/ClientProfile.php index 3e71644e..2ce92fa7 100644 --- a/lib/Predis/Options/ClientProfile.php +++ b/lib/Predis/Options/ClientProfile.php @@ -24,7 +24,7 @@ class ClientProfile extends Option /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { if ($value instanceof IServerProfile) { return $value; @@ -42,7 +42,7 @@ class ClientProfile extends Option /** * {@inheritdoc} */ - public function getDefault() + public function getDefault(IClientOptions $options) { return ServerProfile::getDefault(); } diff --git a/lib/Predis/Options/CustomOption.php b/lib/Predis/Options/CustomOption.php index 7b8686d6..5a8e2fe9 100644 --- a/lib/Predis/Options/CustomOption.php +++ b/lib/Predis/Options/CustomOption.php @@ -53,7 +53,7 @@ class CustomOption implements IOption /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { if (isset($value)) { if ($this->validate === null) { @@ -61,32 +61,32 @@ class CustomOption implements IOption } $validator = $this->validate; - return $validator($value); + return $validator($options, $value); } } /** * {@inheritdoc} */ - public function getDefault() + public function getDefault(IClientOptions $options) { if (!isset($this->default)) { return; } $default = $this->default; - return $default(); + return $default($options); } /** * {@inheritdoc} */ - public function __invoke($value) + public function __invoke(IClientOptions $options, $value) { if (isset($value)) { - return $this->validate($value); + return $this->validate($options, $value); } - return $this->getDefault(); + return $this->getDefault($options); } } diff --git a/lib/Predis/Options/IClientOptions.php b/lib/Predis/Options/IClientOptions.php new file mode 100644 index 00000000..ebbce433 --- /dev/null +++ b/lib/Predis/Options/IClientOptions.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Options; + +/** + * Marker interface defining a client options bag. + * + * @author Daniele Alessandri + */ +interface IClientOptions +{ +} diff --git a/lib/Predis/Options/IOption.php b/lib/Predis/Options/IOption.php index 9b966c8a..0f84f30c 100644 --- a/lib/Predis/Options/IOption.php +++ b/lib/Predis/Options/IOption.php @@ -24,7 +24,7 @@ interface IOption * @param mixed $value Input value. * @return mixed */ - public function validate($value); + public function validate(IClientOptions $options, $value); /** * Returns a default value for the option. @@ -32,7 +32,7 @@ interface IOption * @param mixed $value Input value. * @return mixed */ - public function getDefault(); + public function getDefault(IClientOptions $options); /** * Validates a value and, if no value is specified, returns @@ -41,5 +41,5 @@ interface IOption * @param mixed $value Input value. * @return mixed */ - public function __invoke($value); + public function __invoke(IClientOptions $options, $value); } diff --git a/lib/Predis/Options/Option.php b/lib/Predis/Options/Option.php index 46044e0e..f47785a3 100644 --- a/lib/Predis/Options/Option.php +++ b/lib/Predis/Options/Option.php @@ -21,7 +21,7 @@ class Option implements IOption /** * {@inheritdoc} */ - public function validate($value) + public function validate(IClientOptions $options, $value) { return $value; } @@ -29,7 +29,7 @@ class Option implements IOption /** * {@inheritdoc} */ - public function getDefault() + public function getDefault(IClientOptions $options) { return null; } @@ -37,12 +37,12 @@ class Option implements IOption /** * {@inheritdoc} */ - public function __invoke($value) + public function __invoke(IClientOptions $options, $value) { if (isset($value)) { - return $this->validate($value); + return $this->validate($options, $value); } - return $this->getDefault(); + return $this->getDefault($options); } }