Explorar o código

Add a way to get the default value of an option from options.

It is not possible to get the default value of a client option using
either its name or instance:

  $options = array(
    'profile' => function ($options, $option) {
      // instance of Predis\Option\OptionInterface
      $profile = $options->getDefault($option);

      // string representing an option handled by $options,
      // returns NULL if the specified name is not handled.
      $profile = $options->getDefault('profile');

      return $profile;
    },
  );

This addition makes it less awkward to get the default value of an
option, especially when not in the context of a callable option
initializer.
Daniele Alessandri %!s(int64=12) %!d(string=hai) anos
pai
achega
0efcbb7992

+ 4 - 0
CHANGELOG.md

@@ -1,6 +1,10 @@
 v0.8.2 (2013-xx-xx)
 ===============================================================================
 
+- Added the ability to get the default value of a client option directly from
+  `Predis\Option\ClientOption` using the `getDefault()` method by passing the
+  option name or its instance.
+
 - __FIX__: the standard pipeline executor was not using the response parser
   methods associated to commands to process raw responses (ISSUE #101).
 

+ 2 - 2
examples/MasterSlaveReplicationComplex.php

@@ -51,8 +51,8 @@ $parameters = array(
 );
 
 $options = array(
-    'profile' => function ($options) {
-        $profile = ServerProfile::get('2.6');
+    'profile' => function ($options, $option) {
+        $profile = $options->getDefault($option);
         $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
 
         return $profile;

+ 19 - 0
lib/Predis/Option/ClientOptions.php

@@ -103,4 +103,23 @@ class ClientOptions implements ClientOptionsInterface
             return $value;
         }
     }
+
+    /**
+     * Returns the default value for the specified option.
+     *
+     * @param string|OptionInterface $option Name or instance of the option.
+     * @return mixed
+     */
+    public function getDefault($option)
+    {
+        if ($option instanceof OptionInterface) {
+            return $option->getDefault($this);
+        }
+
+        $options = $this->getDefaultOptions();
+
+        if (isset($options[$option])) {
+            return $options[$option]->getDefault($this);
+        }
+    }
 }

+ 47 - 0
tests/Predis/Option/ClientOptionsTest.php

@@ -78,4 +78,51 @@ class ClientOptionsTest extends StandardTestCase
         $this->assertTrue(isset($options->custom));
         $this->assertFalse(isset($options->profile));
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetDefaultUsingOptionName()
+    {
+        $options = new ClientOptions();
+
+        $this->assertInstanceOf('Predis\Connection\PredisCluster', $options->getDefault('cluster'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetDefaultUsingUnhandledOptionName()
+    {
+        $options = new ClientOptions();
+        $option = new ClientCluster();
+
+        $this->assertNull($options->getDefault('foo'));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetDefaultUsingOptionInstance()
+    {
+        $options = new ClientOptions();
+        $option = new ClientCluster();
+
+        $this->assertInstanceOf('Predis\Connection\PredisCluster', $options->getDefault($option));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetDefaultUsingUnhandledOptionInstance()
+    {
+        $options = new ClientOptions();
+        $option = new CustomOption(array(
+            'default' => function ($options) {
+                return 'foo';
+            },
+        ));
+
+        $this->assertSame('foo', $options->getDefault($option));
+    }
 }