mirror of
https://github.com/predis/predis.git
synced 2026-08-28 03:02:04 +00:00
Implement callable initializer for "connections" client option.
This commit is contained in:
@@ -40,8 +40,20 @@ class ClientConnectionFactory extends AbstractOption
|
||||
return $factory;
|
||||
}
|
||||
|
||||
if (is_string($value) && class_exists($value)) {
|
||||
if (!($factory = new $value()) && !$factory instanceof ConnectionFactoryInterface) {
|
||||
if (is_callable($value)) {
|
||||
$factory = call_user_func($value, $options, $this);
|
||||
|
||||
if (!$factory instanceof ConnectionFactoryInterface) {
|
||||
throw new \InvalidArgumentException('Instance of Predis\Connection\ConnectionFactoryInterface expected');
|
||||
}
|
||||
|
||||
return $factory;
|
||||
}
|
||||
|
||||
if (@class_exists($value)) {
|
||||
$factory = new $value();
|
||||
|
||||
if (!$factory instanceof ConnectionFactoryInterface) {
|
||||
throw new \InvalidArgumentException("Class $value must be an instance of Predis\Connection\ConnectionFactoryInterface");
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,27 @@ class ClientConnectionFactoryTest extends StandardTestCase
|
||||
$this->assertSame($value, $option->filter($options, $value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testValidationAcceptsCallableObjectAsInitializers()
|
||||
{
|
||||
$value = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
|
||||
$options = $this->getMock('Predis\Option\ClientOptionsInterface');
|
||||
$option = new ClientConnectionFactory();
|
||||
|
||||
$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\ConnectionFactoryInterface', $cluster);
|
||||
$this->assertSame($value, $cluster);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user