diff --git a/CHANGELOG.md b/CHANGELOG.md index fdcb6903..66bb2215 100644 --- a/CHANGELOG.md +++ b/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). diff --git a/examples/MasterSlaveReplicationComplex.php b/examples/MasterSlaveReplicationComplex.php index 02b2e086..786d595c 100644 --- a/examples/MasterSlaveReplicationComplex.php +++ b/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; diff --git a/lib/Predis/Option/ClientOptions.php b/lib/Predis/Option/ClientOptions.php index bf88cb9e..d835524a 100644 --- a/lib/Predis/Option/ClientOptions.php +++ b/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); + } + } } diff --git a/tests/Predis/Option/ClientOptionsTest.php b/tests/Predis/Option/ClientOptionsTest.php index 0a124700..12e57fc4 100644 --- a/tests/Predis/Option/ClientOptionsTest.php +++ b/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)); + } }