mirror of
https://github.com/predis/predis.git
synced 2026-09-01 21:27:46 +00:00
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:
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user