9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie
23 * Handles connections initiated by the bot.
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie
31 class Phergie_Connection_Handler implements Countable, IteratorAggregate
34 * Map of connections indexed by hostmask
38 protected $connections;
41 * Constructor to initialize storage for connections.
45 public function __construct()
47 $this->connections = array();
51 * Adds a connection to the connection list.
53 * @param Phergie_Connection $connection Connection to add
55 * @return Phergie_Connection_Handler Provides a fluent interface
57 public function addConnection(Phergie_Connection $connection)
59 $this->connections[(string) $connection->getHostmask()] = $connection;
64 * Removes a connection from the connection list.
66 * @param Phergie_Connection|string $connection Instance or hostmask for
67 * the connection to remove
69 * @return Phergie_Connection_Handler Provides a fluent interface
71 public function removeConnection($connection)
73 if ($connection instanceof Phergie_Connection) {
74 $hostmask = (string) $connection->getHostmask();
75 } elseif (is_string($connection)
76 && isset($this->connections[$connection])) {
77 $hostmask = $connection;
81 unset($this->connections[$hostmask]);
86 * Returns the number of connections in the list.
88 * @return int Number of connections
90 public function count()
92 return count($this->connections);
96 * Returns an iterator for the connection list.
98 * @return ArrayIterator
100 public function getIterator()
102 return new ArrayIterator($this->connections);
106 * Returns a list of specified connection objects.
108 * @param array|string $keys One or more hostmasks identifying the
109 * connections to return
111 * @return array List of Phergie_Connection objects corresponding to the
112 * specified hostmask(s)
114 public function getConnections($keys)
116 $connections = array();
118 if (!is_array($keys)) {
119 $keys = array($keys);
122 foreach ($keys as $key) {
123 if (isset($this->connections[$key])) {
124 $connections[] = $this->connections[$key];