Pass option handler instance to callable initializers.

This is just a convention implemented to client options supporting
callable initializers such as "profile", "cluster" and "replication".

This is useful to get a fully-initialized default value and perform
additional operations before returning it. An example with "profile":

  $options = array(
    'commands' => array(
      'test1' => 'Predis\Command\ConnectionEcho',
      'test2' => 'Predis\Command\ConnectionEcho',
    ),
    'profile'  => function ($options, $option) {
      $profile = $option->getDefault($options);

      if (is_array($options->commands)) {
        foreach ($options->commands as $id => $cmd) {
          $profile->defineCommand($id, $cmd);
        }
      }

      return $profile;
    },
  );
This commit is contained in:
Daniele Alessandri
2012-12-15 16:47:32 +01:00
parent bfd96b15dc
commit 59813cd74e
5 changed files with 17 additions and 17 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ class ClientCluster extends AbstractOption
public function filter(ClientOptionsInterface $options, $value)
{
if (is_callable($value)) {
return $this->checkInstance(call_user_func($value, $options));
return $this->checkInstance(call_user_func($value, $options, $this));
}
$initializer = $this->getInitializer($options, $value);
+1 -1
View File
@@ -35,7 +35,7 @@ class ClientProfile extends AbstractOption
}
if (is_callable($value)) {
$value = call_user_func($value, $options);
$value = call_user_func($value, $options, $this);
}
if (!$value instanceof ServerProfileInterface) {
+1 -1
View File
@@ -42,7 +42,7 @@ class ClientReplication extends AbstractOption
public function filter(ClientOptionsInterface $options, $value)
{
if (is_callable($value)) {
$connection = call_user_func($value, $options);
$connection = call_user_func($value, $options, $this);
if (!$connection instanceof ReplicationConnectionInterface) {
throw new \InvalidArgumentException('Instance of Predis\Connection\ReplicationConnectionInterface expected');
+7 -7
View File
@@ -53,16 +53,16 @@ class ClientClusterTest extends StandardTestCase
{
$value = $this->getMock('Predis\Connection\ClusterConnectionInterface');
$initializer = $this->getMock('stdClass', array('__invoke'));
$initializer->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'))
->will($this->returnValue($value));
$options = $this->getMock('Predis\Option\ClientOptionsInterface');
$option = new ClientCluster();
$cluster = $option->filter($options, $initializer);
$initializer = $this->getMock('stdClass', array('__invoke'));
$initializer->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'), $option)
->will($this->returnValue($value));
$cluster = $option->filter($options, $initializer, $option);
$this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster);
$this->assertSame($value, $cluster);
+7 -7
View File
@@ -59,16 +59,16 @@ class ClientProfileTest extends StandardTestCase
{
$value = $this->getMock('Predis\Profile\ServerProfileInterface');
$initializer = $this->getMock('stdClass', array('__invoke'));
$initializer->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'))
->will($this->returnValue($value));
$options = $this->getMock('Predis\Option\ClientOptionsInterface');
$option = new ClientProfile();
$profile = $option->filter($options, $initializer);
$initializer = $this->getMock('stdClass', array('__invoke'));
$initializer->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'), $option)
->will($this->returnValue($value));
$profile = $option->filter($options, $initializer, $option);
$this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
$this->assertSame($value, $profile);