mirror of
https://github.com/predis/predis.git
synced 2026-08-27 18:51:07 +00:00
Implement IteratorAggregate interface for Client.
Now it is possible to iterate over traversable aggregate connections and get a key/value pair of $connectionId => $clientInstance for each node.
This commit is contained in:
+20
-1
@@ -38,7 +38,7 @@ use Predis\Transaction\MultiExec as MultiExecTransaction;
|
||||
*
|
||||
* @author Daniele Alessandri <suppakilla@gmail.com>
|
||||
*/
|
||||
class Client implements ClientInterface
|
||||
class Client implements ClientInterface, \IteratorAggregate
|
||||
{
|
||||
const VERSION = '1.1.0-dev';
|
||||
|
||||
@@ -525,4 +525,23 @@ class Client implements ClientInterface
|
||||
{
|
||||
return new MonitorConsumer($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
$clients = array();
|
||||
$connection = $this->getConnection();
|
||||
|
||||
if (!$connection instanceof \Traversable) {
|
||||
throw new ClientException('The underlying connection is not traversable');
|
||||
}
|
||||
|
||||
foreach ($connection as $node) {
|
||||
$clients[(string) $node] = new static($node, $this->getOptions());
|
||||
}
|
||||
|
||||
return new \ArrayIterator($clients);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user