Эх сурвалжийг харах

Partially rewrite the client options bits and remove some madness.

The concept is now similar to a little dependency-injection system.
Daniele Alessandri 13 жил өмнө
parent
commit
a5bf0eb41a

+ 3 - 1
lib/Predis/Client.php

@@ -12,9 +12,11 @@
 namespace Predis;
 namespace Predis;
 
 
 use Predis\Commands\ICommand;
 use Predis\Commands\ICommand;
+use Predis\Options\IClientOptions;
 use Predis\Network\IConnection;
 use Predis\Network\IConnection;
 use Predis\Network\IConnectionSingle;
 use Predis\Network\IConnectionSingle;
 use Predis\Profiles\IServerProfile;
 use Predis\Profiles\IServerProfile;
+use Predis\Options\ClientOptions;
 use Predis\Profiles\ServerProfile;
 use Predis\Profiles\ServerProfile;
 use Predis\PubSub\PubSubContext;
 use Predis\PubSub\PubSubContext;
 use Predis\Pipeline\PipelineContext;
 use Predis\Pipeline\PipelineContext;
@@ -72,7 +74,7 @@ class Client
         if (is_array($options)) {
         if (is_array($options)) {
             return new ClientOptions($options);
             return new ClientOptions($options);
         }
         }
-        if ($options instanceof ClientOptions) {
+        if ($options instanceof IClientOptions) {
             return $options;
             return $options;
         }
         }
         if ($options instanceof IServerProfile) {
         if ($options instanceof IServerProfile) {

+ 2 - 2
lib/Predis/Options/ClientCluster.php

@@ -41,7 +41,7 @@ class ClientCluster extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         if (is_callable($value)) {
         if (is_callable($value)) {
             return $this->checkInstance(call_user_func($value));
             return $this->checkInstance(call_user_func($value));
@@ -73,7 +73,7 @@ class ClientCluster extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function getDefault()
+    public function getDefault(IClientOptions $options)
     {
     {
         return new PredisCluster();
         return new PredisCluster();
     }
     }

+ 3 - 3
lib/Predis/Options/ClientConnectionFactory.php

@@ -24,13 +24,13 @@ class ClientConnectionFactory extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         if ($value instanceof IConnectionFactory) {
         if ($value instanceof IConnectionFactory) {
             return $value;
             return $value;
         }
         }
         if (is_array($value)) {
         if (is_array($value)) {
-            $factory = $this->getDefault();
+            $factory = $this->getDefault($options);
             foreach ($value as $scheme => $initializer) {
             foreach ($value as $scheme => $initializer) {
                 $factory->define($scheme, $initializer);
                 $factory->define($scheme, $initializer);
             }
             }
@@ -41,7 +41,7 @@ class ClientConnectionFactory extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function getDefault()
+    public function getDefault(IClientOptions $options)
     {
     {
         return new ConnectionFactory();
         return new ConnectionFactory();
     }
     }

+ 14 - 49
lib/Predis/ClientOptions.php → lib/Predis/Options/ClientOptions.php

@@ -9,26 +9,17 @@
  * file that was distributed with this source code.
  * 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.
  * Class that manages validation and conversion of client options.
  *
  *
  * @author Daniele Alessandri <suppakilla@gmail.com>
  * @author Daniele Alessandri <suppakilla@gmail.com>
  */
  */
-class ClientOptions
+class ClientOptions implements IClientOptions
 {
 {
-    private static $sharedOptions;
-
     private $handlers;
     private $handlers;
     private $defined;
     private $defined;
-
     private $options = array();
     private $options = array();
 
 
     /**
     /**
@@ -37,7 +28,7 @@ class ClientOptions
     public function __construct(Array $options = array())
     public function __construct(Array $options = array())
     {
     {
         $this->handlers = $this->initialize($options);
         $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
      * @return array
      */
      */
-    private static function getSharedOptions()
+    protected function getDefaultOptions()
     {
     {
-        if (isset(self::$sharedOptions)) {
-            return self::$sharedOptions;
-        }
-
-        self::$sharedOptions = array(
+        return array(
             'profile' => new ClientProfile(),
             'profile' => new ClientProfile(),
             'connections' => new ClientConnectionFactory(),
             'connections' => new ClientConnectionFactory(),
             'cluster' => new ClientCluster(),
             'cluster' => new ClientCluster(),
             'prefix' => new ClientPrefix(),
             '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.
      * @param array $options List of client options values.
      * @return array
      * @return array
      */
      */
-    private function initialize($options)
+    protected function initialize(Array $options)
     {
     {
-        $handlers = self::getSharedOptions();
+        $handlers = $this->getDefaultOptions();
 
 
         foreach ($options as $option => $value) {
         foreach ($options as $option => $value) {
             if (isset($handlers[$option])) {
             if (isset($handlers[$option])) {
                 $handler = $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;
         return $handlers;
@@ -114,7 +79,7 @@ class ClientOptions
      */
      */
     public function __isset($option)
     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])) {
         if (isset($this->handlers[$option])) {
             $handler = $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;
             $this->options[$option] = $value;
 
 
             return $value;
             return $value;

+ 1 - 1
lib/Predis/Options/ClientPrefix.php

@@ -23,7 +23,7 @@ class ClientPrefix extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         return new KeyPrefixProcessor($value);
         return new KeyPrefixProcessor($value);
     }
     }

+ 2 - 2
lib/Predis/Options/ClientProfile.php

@@ -24,7 +24,7 @@ class ClientProfile extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         if ($value instanceof IServerProfile) {
         if ($value instanceof IServerProfile) {
             return $value;
             return $value;
@@ -42,7 +42,7 @@ class ClientProfile extends Option
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function getDefault()
+    public function getDefault(IClientOptions $options)
     {
     {
         return ServerProfile::getDefault();
         return ServerProfile::getDefault();
     }
     }

+ 7 - 7
lib/Predis/Options/CustomOption.php

@@ -53,7 +53,7 @@ class CustomOption implements IOption
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         if (isset($value)) {
         if (isset($value)) {
             if ($this->validate === null) {
             if ($this->validate === null) {
@@ -61,32 +61,32 @@ class CustomOption implements IOption
             }
             }
             $validator = $this->validate;
             $validator = $this->validate;
 
 
-            return $validator($value);
+            return $validator($options, $value);
         }
         }
     }
     }
 
 
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function getDefault()
+    public function getDefault(IClientOptions $options)
     {
     {
         if (!isset($this->default)) {
         if (!isset($this->default)) {
             return;
             return;
         }
         }
         $default = $this->default;
         $default = $this->default;
 
 
-        return $default();
+        return $default($options);
     }
     }
 
 
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function __invoke($value)
+    public function __invoke(IClientOptions $options, $value)
     {
     {
         if (isset($value)) {
         if (isset($value)) {
-            return $this->validate($value);
+            return $this->validate($options, $value);
         }
         }
 
 
-        return $this->getDefault();
+        return $this->getDefault($options);
     }
     }
 }
 }

+ 21 - 0
lib/Predis/Options/IClientOptions.php

@@ -0,0 +1,21 @@
+<?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\Options;
+
+/**
+ * Marker interface defining a client options bag.
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+interface IClientOptions
+{
+}

+ 3 - 3
lib/Predis/Options/IOption.php

@@ -24,7 +24,7 @@ interface IOption
      * @param mixed $value Input value.
      * @param mixed $value Input value.
      * @return mixed
      * @return mixed
      */
      */
-    public function validate($value);
+    public function validate(IClientOptions $options, $value);
 
 
     /**
     /**
      * Returns a default value for the option.
      * Returns a default value for the option.
@@ -32,7 +32,7 @@ interface IOption
      * @param mixed $value Input value.
      * @param mixed $value Input value.
      * @return mixed
      * @return mixed
      */
      */
-    public function getDefault();
+    public function getDefault(IClientOptions $options);
 
 
     /**
     /**
      * Validates a value and, if no value is specified, returns
      * Validates a value and, if no value is specified, returns
@@ -41,5 +41,5 @@ interface IOption
      * @param mixed $value Input value.
      * @param mixed $value Input value.
      * @return mixed
      * @return mixed
      */
      */
-    public function __invoke($value);
+    public function __invoke(IClientOptions $options, $value);
 }
 }

+ 5 - 5
lib/Predis/Options/Option.php

@@ -21,7 +21,7 @@ class Option implements IOption
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function validate($value)
+    public function validate(IClientOptions $options, $value)
     {
     {
         return $value;
         return $value;
     }
     }
@@ -29,7 +29,7 @@ class Option implements IOption
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function getDefault()
+    public function getDefault(IClientOptions $options)
     {
     {
         return null;
         return null;
     }
     }
@@ -37,12 +37,12 @@ class Option implements IOption
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function __invoke($value)
+    public function __invoke(IClientOptions $options, $value)
     {
     {
         if (isset($value)) {
         if (isset($value)) {
-            return $this->validate($value);
+            return $this->validate($options, $value);
         }
         }
 
 
-        return $this->getDefault();
+        return $this->getDefault($options);
     }
     }
 }
 }