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 events initiated by plugins.
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_Event_Handler implements IteratorAggregate, Countable
34 * Current queue of events
41 * Constructor to initialize the event queue.
45 public function __construct()
47 $this->events = array();
51 * Adds an event to the queue.
53 * @param Phergie_Plugin_Abstract $plugin Plugin originating the event
54 * @param string $type Event type, corresponding to a
55 * Phergie_Event_Command::TYPE_* constant
56 * @param array $args Optional event arguments
58 * @return Phergie_Event_Handler Provides a fluent interface
60 public function addEvent(Phergie_Plugin_Abstract $plugin, $type,
63 if (!defined('Phergie_Event_Command::TYPE_' . strtoupper($type))) {
64 throw new Phergie_Event_Exception(
65 'Unknown event type "' . $type . '"',
66 Phergie_Event_Exception::ERR_UNKNOWN_EVENT_TYPE
70 $event = new Phergie_Event_Command;
74 ->setArguments($args);
76 $this->events[] = $event;
82 * Returns the current event queue.
84 * @return array Enumerated array of Phergie_Event_Command objects
86 public function getEvents()
92 * Clears the event queue.
94 * @return Phergie_Event_Handler Provides a fluent interface
96 public function clearEvents()
98 $this->events = array();
103 * Replaces the current event queue with a given queue of events.
105 * @param array $events Ordered list of objects of the class
106 * Phergie_Event_Command
108 * @return Phergie_Event_Handler Provides a fluent interface
110 public function replaceEvents(array $events)
112 $this->events = $events;
117 * Returns whether an event of the given type exists in the queue.
119 * @param string $type Event type from Phergie_Event_Request::TYPE_*
122 * @return bool TRUE if an event of the specified type exists in the
123 * queue, FALSE otherwise
125 public function hasEventOfType($type)
127 foreach ($this->events as $event) {
128 if ($event->getType() == $type) {
136 * Returns a list of events of a specified type.
138 * @param string $type Event type from Phergie_Event_Request::TYPE_*
141 * @return array Array containing event instances of the specified type
142 * or an empty array if no such events were found
144 public function getEventsOfType($type)
147 foreach ($this->events as $event) {
148 if ($event->getType() == $type) {
156 * Removes a single event from the event queue.
158 * @param Phergie_Event_Command $event Event to remove
160 * @return Phergie_Event_Handler Provides a fluent interface
162 public function removeEvent(Phergie_Event_Command $event)
164 $key = array_search($event, $this->events);
165 if ($key !== false) {
166 unset($this->events[$key]);
172 * Returns an iterator for the current event queue.
174 * @return ArrayIterator
176 public function getIterator()
178 return new ArrayIterator($this->events);
182 * Returns the number of events in the event queue
184 * @return int number of queued events
186 public function count()
188 return count($this->events);