Parcourir la source

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;
    },
  );
Daniele Alessandri il y a 12 ans
Parent
commit
59813cd74e

+ 1 - 1
lib/Predis/Option/ClientCluster.php

@@ -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
lib/Predis/Option/ClientProfile.php

@@ -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
lib/Predis/Option/ClientReplication.php

@@ -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');

+ 5 - 5
tests/Predis/Option/ClientClusterTest.php

@@ -53,16 +53,16 @@ class ClientClusterTest extends StandardTestCase
     {
         $value = $this->getMock('Predis\Connection\ClusterConnectionInterface');
 
+        $options = $this->getMock('Predis\Option\ClientOptionsInterface');
+        $option = new ClientCluster();
+
         $initializer = $this->getMock('stdClass', array('__invoke'));
         $initializer->expects($this->once())
                     ->method('__invoke')
-                    ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'))
+                    ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'), $option)
                     ->will($this->returnValue($value));
 
-        $options = $this->getMock('Predis\Option\ClientOptionsInterface');
-        $option = new ClientCluster();
-
-        $cluster = $option->filter($options, $initializer);
+        $cluster = $option->filter($options, $initializer, $option);
 
         $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster);
         $this->assertSame($value, $cluster);

+ 5 - 5
tests/Predis/Option/ClientProfileTest.php

@@ -59,16 +59,16 @@ class ClientProfileTest extends StandardTestCase
     {
         $value = $this->getMock('Predis\Profile\ServerProfileInterface');
 
+        $options = $this->getMock('Predis\Option\ClientOptionsInterface');
+        $option = new ClientProfile();
+
         $initializer = $this->getMock('stdClass', array('__invoke'));
         $initializer->expects($this->once())
                     ->method('__invoke')
-                    ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'))
+                    ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'), $option)
                     ->will($this->returnValue($value));
 
-        $options = $this->getMock('Predis\Option\ClientOptionsInterface');
-        $option = new ClientProfile();
-
-        $profile = $option->filter($options, $initializer);
+        $profile = $option->filter($options, $initializer, $option);
 
         $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
         $this->assertSame($value, $profile);