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:
Daniele Alessandri
2016-06-01 12:33:20 +02:00
parent a22fc17800
commit ecab7e4642
3 changed files with 99 additions and 1 deletions
+20 -1
View File
@@ -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);
}
}