mirror of
https://github.com/predis/predis.git
synced 2026-09-13 20:07:28 +00:00
Accepts callable as first argument of Predis\Client::__construct().
Users can then use callables to wrap the creation and initialization of the underlying connection with custom strategies:
This commit is contained in:
@@ -11,6 +11,10 @@ v0.8.3 (2013-xx-xx)
|
||||
hash slots instead of 4096 to reflect this recent change from redis unstable
|
||||
([see this commit](https://github.com/antirez/redis/commit/ebd666d)).
|
||||
|
||||
- The constructor of `Predis\Client` now accepts a callable as first argument
|
||||
returning `Predis\Connection\ConnectionInterface`. Users can create their
|
||||
own self-contained strategies to create and set up the underlying connection.
|
||||
|
||||
|
||||
v0.8.2 (2013-02-03)
|
||||
===============================================================================
|
||||
|
||||
@@ -100,6 +100,18 @@ class Client implements ClientInterface
|
||||
return $this->connections->createAggregated($connection, $parameters);
|
||||
}
|
||||
|
||||
if (is_callable($parameters)) {
|
||||
$connection = call_user_func($parameters, $this->options);
|
||||
|
||||
if (!$connection instanceof ConnectionInterface) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Callable parameters must return instances of Predis\Connection\ConnectionInterface'
|
||||
);
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
return $this->connections->create($parameters);
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,42 @@ class ClientTest extends StandardTestCase
|
||||
$this->assertSame($replication, $client->getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testConstructorWithCallableArgument()
|
||||
{
|
||||
$connection = $this->getMock('Predis\Connection\ConnectionInterface');
|
||||
|
||||
$callable = $this->getMock('stdClass', array('__invoke'));
|
||||
$callable->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($this->isInstanceOf('Predis\Option\ClientOptions'))
|
||||
->will($this->returnValue($connection));
|
||||
|
||||
$client = new Client($callable);
|
||||
|
||||
$this->assertSame($connection, $client->getConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage Callable parameters must return instances of Predis\Connection\ConnectionInterface
|
||||
*/
|
||||
public function testConstructorWithCallableArgumentButInvalidReturnType()
|
||||
{
|
||||
$wrongType = $this->getMock('stdClass');
|
||||
|
||||
$callable = $this->getMock('stdClass', array('__invoke'));
|
||||
$callable->expects($this->once())
|
||||
->method('__invoke')
|
||||
->with($this->isInstanceOf('Predis\Option\ClientOptions'))
|
||||
->will($this->returnValue($wrongType));
|
||||
|
||||
$client = new Client($callable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user