diff --git a/src/Event.php b/src/Event.php index e360d20..52ae188 100644 --- a/src/Event.php +++ b/src/Event.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -33,7 +33,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Event implements EventDescription diff --git a/src/EventCollection.php b/src/EventCollection.php index 048ec4c..f94504d 100644 --- a/src/EventCollection.php +++ b/src/EventCollection.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface EventCollection @@ -84,10 +84,10 @@ public function attach($event, $callback, $priority = 1); /** * Detach an event listener * - * @param CallbackHandler $listener + * @param CallbackHandler|ListenerAggregate $listener * @return void */ - public function detach(CallbackHandler $listener); + public function detach($listener); /** * Get a list of events for which this collection has listeners diff --git a/src/EventDescription.php b/src/EventDescription.php index 670d496..426f86e 100644 --- a/src/EventDescription.php +++ b/src/EventDescription.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface EventDescription diff --git a/src/EventManager.php b/src/EventManager.php index 456d88d..e3cf290 100644 --- a/src/EventManager.php +++ b/src/EventManager.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -38,7 +38,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class EventManager implements EventCollection @@ -122,8 +122,8 @@ public function getStaticConnections() } /** - * Get the identifier(s) for this EventManager - * + * Get the identifier(s) for this EventManager + * * @return array */ public function getIdentifiers() @@ -132,9 +132,9 @@ public function getIdentifiers() } /** - * Set the identifiers (overrides any currently set identifiers) - * - * @param string|int|array|Traversable $identifiers + * Set the identifiers (overrides any currently set identifiers) + * + * @param string|int|array|Traversable $identifiers * @return ModuleManager */ public function setIdentifiers($identifiers) @@ -148,9 +148,9 @@ public function setIdentifiers($identifiers) } /** - * Add some identifier(s) (appends to any currently set identifiers) - * - * @param string|int|array|Traversable $identifiers + * Add some identifier(s) (appends to any currently set identifiers) + * + * @param string|int|array|Traversable $identifiers * @return ModuleManager */ public function addIdentifiers($identifiers) @@ -171,7 +171,7 @@ public function addIdentifiers($identifiers) * @param string $event * @param string|object $target Object calling emit, or symbol describing target (such as static method name) * @param array|ArrayAccess $argv Array of arguments; typically, should be associative - * @param null|callback $callback + * @param null|callback $callback * @return ResponseCollection All listener return values */ public function trigger($event, $target = null, $argv = array(), $callback = null) @@ -195,10 +195,8 @@ public function trigger($event, $target = null, $argv = array(), $callback = nul $e->setParams($argv); } - if (!$callback) { - $callback = function() { - return false; - }; + if ($callback && !is_callable($callback)) { + throw new InvalidCallbackException('Invalid callback provided'); } return $this->triggerListeners($event, $e, $callback); @@ -256,17 +254,47 @@ public function triggerUntil($event, $target, $argv = null, $callback = null) * executed. By default, this value is 1; however, you may set it for any * integer value. Higher values have higher priority (i.e., execute first). * - * @param string $event - * @param callback $callback PHP callback + * You can specify "*" for the event name. In such cases, the listener will + * be triggered for every event. + * + * @param string|array|ListenerAggregate $event An event or array of event names. If a ListenerAggregate, proxies to {@link attachAggregate()}. + * @param callback|int $callback If string $event provided, expects PHP callback; for a ListenerAggregate $event, this will be the priority * @param int $priority If provided, the priority at which to register the callback - * @return ListenerAggregate (to allow later unsubscribe) + * @return CallbackHandler|mixed CallbackHandler if attaching callback (to allow later unsubscribe); mixed if attaching aggregate */ - public function attach($event, $callback, $priority = 1) + public function attach($event, $callback = null, $priority = 1) { + // Proxy ListenerAggregate arguments to attachAggregate() + if ($event instanceof ListenerAggregate) { + return $this->attachAggregate($event, $callback); + } + + // Null callback is invalid + if (null === $callback) { + throw new Exception\InvalidArgumentException(sprintf( + '%s: expects a callback; none provided', + __METHOD__ + )); + } + + // Array of events should be registered individually, and return an array of all listeners + if (is_array($event)) { + $listeners = array(); + foreach ($event as $name) { + $listeners[] = $this->attach($name, $callback, $priority); + } + return $listeners; + } + + // If we don't have a priority queue for the event yet, create one if (empty($this->events[$event])) { $this->events[$event] = new PriorityQueue(); } - $listener = new CallbackHandler($event, $callback, array('priority' => $priority)); + + // Create a callback handler, setting the event and priority in its metadata + $listener = new CallbackHandler($callback, array('event' => $event, 'priority' => $priority)); + + // Inject the callback handler into the queue $this->events[$event]->insert($listener, $priority); return $listener; } @@ -279,23 +307,37 @@ public function attach($event, $callback, $priority = 1) * methods. * * @param ListenerAggregate $aggregate + * @param int $priority If provided, a suggested priority for the aggregate to use * @return mixed return value of {@link ListenerAggregate::attach()} */ - public function attachAggregate(ListenerAggregate $aggregate) + public function attachAggregate(ListenerAggregate $aggregate, $priority = 1) { - return $aggregate->attach($this); + return $aggregate->attach($this, $priority); } /** * Unsubscribe a listener from an event * - * @param CallbackHandler $listener + * @param CallbackHandler|ListenerAggregate $listener * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found + * @throws Exception\InvalidArgumentException if invalid listener provided */ - public function detach(CallbackHandler $listener) + public function detach($listener) { - $event = $listener->getEvent(); - if (empty($this->events[$event])) { + if ($listener instanceof ListenerAggregate) { + return $this->detachAggregate($listener); + } + + if (!$listener instanceof CallbackHandler) { + throw new Exception\InvalidArgumentException(sprintf( + '%s: expected a ListenerAggregate or CallbackHandler; received "%s"', + __METHOD__, + (is_object($listener) ? get_class($listener) : gettype($listener)) + )); + } + + $event = $listener->getMetadatum('event'); + if (!$event || empty($this->events[$event])) { return false; } $return = $this->events[$event]->remove($listener); @@ -377,43 +419,43 @@ public function prepareArgs(array $args) /** * Trigger listeners * - * Actual functionality for triggering listeners, to which both trigger() and triggerUntil() + * Actual functionality for triggering listeners, to which both trigger() and triggerUntil() * delegate. - * - * @param string $event Event name - * @param EventDescription $e - * @param callback $callback + * + * @param string $event Event name + * @param EventDescription $e + * @param null|callback $callback * @return ResponseCollection */ - protected function triggerListeners($event, EventDescription $e, $callback) + protected function triggerListeners($event, EventDescription $e, $callback = null) { $responses = new ResponseCollection; - - $listeners = clone $this->getListeners($event); - foreach ($this->getStaticListeners($event) as $listener) { - $priority = $listener->getOption('priority'); - if (null === $priority) { - $priority = 1; - } elseif (is_array($priority)) { - // If we have an array, likely using PriorityQueue. Grab first - // element of the array, as that's the actual priority. - $priority = array_shift($priority); - } - $listeners->insert($listener, $priority); + $listeners = $this->getListeners($event); + + // Add static/wildcard listeners to the list of listeners, + // but don't modify the listeners object + $staticListeners = $this->getStaticListeners($event); + $staticWildcardListeners = $this->getStaticListeners('*'); + $wildcardListeners = $this->getListeners('*'); + if (count($staticListeners) || count($staticWildcardListeners) || count($wildcardListeners)) { + $listeners = clone $listeners; } + // Static listeners on this specific event + $this->insertListeners($listeners, $staticListeners); + + // Static wildcard listeners + $this->insertListeners($listeners, $staticWildcardListeners); + + // Add wildcard listeners + $this->insertListeners($listeners, $wildcardListeners); + if ($listeners->isEmpty()) { return $responses; } foreach ($listeners as $listener) { - // If we have an invalid listener, detach it, and move on to the next - if (!$listener->isValid()) { - $this->detach($listener); - continue; - } - - // Trigger the listener's callback, and push its result onto the + // Trigger the listener's callback, and push its result onto the // response collection $responses->push(call_user_func($listener->getCallback(), $e)); @@ -423,9 +465,9 @@ protected function triggerListeners($event, EventDescription $e, $callback) break; } - // If the result causes our validation callback to return true, + // If the result causes our validation callback to return true, // stop propagation - if (call_user_func($callback, $responses->last())) { + if ($callback && call_user_func($callback, $responses->last())) { $responses->setStopped(true); break; } @@ -435,10 +477,10 @@ protected function triggerListeners($event, EventDescription $e, $callback) } /** - * Get list of all listeners attached to the static collection for + * Get list of all listeners attached to the static collection for * identifiers registered by this instance - * - * @param string $event + * + * @param string $event * @return array */ protected function getStaticListeners($event) @@ -469,4 +511,32 @@ protected function getStaticListeners($event) return $staticListeners; } + + /** + * Add listeners to the master queue of listeners + * + * Used to inject static listeners and wildcard listeners. + * + * @param PriorityQueue $masterListeners + * @param PriorityQueue $listeners + * @return void + */ + protected function insertListeners($masterListeners, $listeners) + { + if (!count($listeners)) { + return; + } + + foreach ($listeners as $listener) { + $priority = $listener->getMetadatum('priority'); + if (null === $priority) { + $priority = 1; + } elseif (is_array($priority)) { + // If we have an array, likely using PriorityQueue. Grab first + // element of the array, as that's the actual priority. + $priority = array_shift($priority); + } + $masterListeners->insert($listener, $priority); + } + } } diff --git a/src/Exception.php b/src/Exception.php index 6e85e67..4c60821 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface Exception diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index affca38..c256c68 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class InvalidArgumentException diff --git a/src/Filter.php b/src/Filter.php index c4dbd1a..26357b2 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface Filter diff --git a/src/Filter/FilterIterator.php b/src/Filter/FilterIterator.php index 238da09..ac087c1 100644 --- a/src/Filter/FilterIterator.php +++ b/src/Filter/FilterIterator.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -34,7 +34,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class FilterIterator extends SplPriorityQueue diff --git a/src/FilterChain.php b/src/FilterChain.php index e4a5f54..5db8449 100644 --- a/src/FilterChain.php +++ b/src/FilterChain.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -31,7 +31,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class FilterChain implements Filter @@ -90,7 +90,7 @@ public function attach($callback, $priority = 1) if (empty($callback)) { throw new InvalidCallbackException('No callback provided'); } - $filter = new CallbackHandler(null, $callback, array('priority' => $priority)); + $filter = new CallbackHandler($callback, array('priority' => $priority)); $this->filters->insert($filter, $priority); return $filter; } diff --git a/src/GlobalEventManager.php b/src/GlobalEventManager.php index b233cf1..32561fe 100644 --- a/src/GlobalEventManager.php +++ b/src/GlobalEventManager.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -35,7 +35,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class GlobalEventManager diff --git a/src/ListenerAggregate.php b/src/ListenerAggregate.php index 28e3d97..26a4439 100644 --- a/src/ListenerAggregate.php +++ b/src/ListenerAggregate.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -33,7 +33,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface ListenerAggregate @@ -41,7 +41,11 @@ interface ListenerAggregate /** * Attach one or more listeners * + * Implementors may add an optional $priority argument; the EventManager + * implementation will pass this to the aggregate. + * * @param EventCollection $events + * @param null|int $priority Optional priority "hint" to use when attaching listeners */ public function attach(EventCollection $events); diff --git a/src/ProvidesEvents.php b/src/ProvidesEvents.php index 6420679..05ec83f 100644 --- a/src/ProvidesEvents.php +++ b/src/ProvidesEvents.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ trait ProvidesEvents diff --git a/src/ResponseCollection.php b/src/ResponseCollection.php index 5c373f9..5ec72fd 100644 --- a/src/ResponseCollection.php +++ b/src/ResponseCollection.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class ResponseCollection extends SplStack diff --git a/src/StaticEventCollection.php b/src/StaticEventCollection.php index 219622c..8d504d4 100644 --- a/src/StaticEventCollection.php +++ b/src/StaticEventCollection.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface StaticEventCollection diff --git a/src/StaticEventManager.php b/src/StaticEventManager.php index c32819b..88841e3 100644 --- a/src/StaticEventManager.php +++ b/src/StaticEventManager.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * * @category Zend * @package Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class StaticEventManager implements StaticEventCollection diff --git a/test/EventManagerTest.php b/test/EventManagerTest.php index ceb95bf..3200603 100644 --- a/test/EventManagerTest.php +++ b/test/EventManagerTest.php @@ -15,13 +15,15 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace ZendTest\EventManager; -use Zend\EventManager\Event, +use ArrayIterator, + stdClass, + Zend\EventManager\Event, Zend\EventManager\EventDescription, Zend\EventManager\EventManager, Zend\EventManager\ResponseCollection, @@ -32,7 +34,7 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class EventManagerTest extends \PHPUnit_Framework_TestCase @@ -69,6 +71,37 @@ public function testAttachShouldAddEventIfItDoesNotExist() $this->assertContains('test', $events); } + public function testAllowsPassingArrayOfEventNamesWhenAttaching() + { + $callback = function ($e) { + return $e->getName(); + }; + $this->events->attach(array('foo', 'bar'), $callback); + + foreach (array('foo', 'bar') as $event) { + $listeners = $this->events->getListeners($event); + $this->assertTrue(count($listeners) > 0); + foreach ($listeners as $listener) { + $this->assertSame($callback, $listener->getCallback()); + } + } + } + + public function testPassingArrayOfEventNamesWhenAttachingReturnsArrayOfCallbackHandlers() + { + $callback = function ($e) { + return $e->getName(); + }; + $listeners = $this->events->attach(array('foo', 'bar'), $callback); + + $this->assertInternalType('array', $listeners); + + foreach ($listeners as $listener) { + $this->assertInstanceOf('Zend\Stdlib\CallbackHandler', $listener); + $this->assertSame($callback, $listener->getCallback()); + } + } + public function testDetachShouldRemoveListenerFromEvent() { $listener = $this->events->attach('test', array($this, __METHOD__)); @@ -227,6 +260,16 @@ public function testCanAttachListenerAggregate() } } + public function testCanAttachListenerAggregateViaAttach() + { + $aggregate = new TestAsset\MockAggregate(); + $this->events->attach($aggregate); + $events = $this->events->getEvents(); + foreach (array('foo.bar', 'foo.baz') as $event) { + $this->assertContains($event, $events); + } + } + public function testAttachAggregateReturnsAttachOfListenerAggregate() { $aggregate = new TestAsset\MockAggregate(); @@ -264,6 +307,36 @@ public function testCanDetachListenerAggregates() $this->assertContains($listenerOther, $listeners); } + public function testCanDetachListenerAggregatesViaDetach() + { + // setup some other event listeners, to ensure appropriate items are detached + $listenerFooBar1 = $this->events->attach('foo.bar', function(){ return true; }); + $listenerFooBar2 = $this->events->attach('foo.bar', function(){ return true; }); + $listenerFooBaz1 = $this->events->attach('foo.baz', function(){ return true; }); + $listenerOther = $this->events->attach('other', function(){ return true; }); + + $aggregate = new TestAsset\MockAggregate(); + $this->events->attach($aggregate); + $this->events->detach($aggregate); + $events = $this->events->getEvents(); + foreach (array('foo.bar', 'foo.baz', 'other') as $event) { + $this->assertContains($event, $events); + } + + $listeners = $this->events->getListeners('foo.bar'); + $this->assertEquals(2, count($listeners)); + $this->assertContains($listenerFooBar1, $listeners); + $this->assertContains($listenerFooBar2, $listeners); + + $listeners = $this->events->getListeners('foo.baz'); + $this->assertEquals(1, count($listeners)); + $this->assertContains($listenerFooBaz1, $listeners); + + $listeners = $this->events->getListeners('other'); + $this->assertEquals(1, count($listeners)); + $this->assertContains($listenerOther, $listeners); + } + public function testDetachAggregateReturnsDetachOfListenerAggregate() { $aggregate = new TestAsset\MockAggregate(); @@ -272,6 +345,20 @@ public function testDetachAggregateReturnsDetachOfListenerAggregate() $this->assertSame('ZendTest\EventManager\TestAsset\MockAggregate::detach', $method); } + public function testAttachAggregateAcceptsOptionalPriorityValue() + { + $aggregate = new TestAsset\MockAggregate(); + $this->events->attachAggregate($aggregate, 1); + $this->assertEquals(1, $aggregate->priority); + } + + public function testAttachAggregateAcceptsOptionalPriorityValueViaAttachCallbackArgument() + { + $aggregate = new TestAsset\MockAggregate(); + $this->events->attach($aggregate, 1); + $this->assertEquals(1, $aggregate->priority); + } + public function testCallingEventsStopPropagationMethodHaltsEventEmission() { $this->events->attach('foo.bar', function ($e) { return 'bogus'; }, 4); @@ -485,11 +572,26 @@ public function testIdentifierGetterSettersWorkWithArrays() public function testIdentifierGetterSettersWorkWithTraversables() { - $identifiers = new \ArrayIterator(array('foo', 'bar')); + $identifiers = new ArrayIterator(array('foo', 'bar')); $this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->setIdentifiers($identifiers)); $this->assertSame($this->events->getIdentifiers(), (array) $identifiers); - $identifiers = new \ArrayIterator(array('foo', 'bar', 'baz')); + $identifiers = new ArrayIterator(array('foo', 'bar', 'baz')); $this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->addIdentifiers($identifiers)); $this->assertSame($this->events->getIdentifiers(), (array) $identifiers); } + + public function testListenersAttachedWithWildcardAreTriggeredForAllEvents() + { + $test = new stdClass; + $test->events = array(); + $callback = function($e) use ($test) { + $test->events[] = $e->getName(); + }; + + $this->events->attach('*', $callback); + foreach (array('foo', 'bar', 'baz') as $event) { + $this->events->trigger($event); + $this->assertContains($event, $test->events); + } + } } diff --git a/test/FilterChainTest.php b/test/FilterChainTest.php index 804a963..653aedd 100644 --- a/test/FilterChainTest.php +++ b/test/FilterChainTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Stdlib * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id:$ */ @@ -29,7 +29,7 @@ * @package Zend_Stdlib * @subpackage UnitTests * @group Zend_Stdlib - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class FilterChainTest extends \PHPUnit_Framework_TestCase diff --git a/test/GlobalEventManagerTest.php b/test/GlobalEventManagerTest.php index 85e5f21..cf0933d 100644 --- a/test/GlobalEventManagerTest.php +++ b/test/GlobalEventManagerTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class GlobalEventManagerTest extends \PHPUnit_Framework_TestCase diff --git a/test/StaticEventManagerTest.php b/test/StaticEventManagerTest.php index b88389b..140f191 100644 --- a/test/StaticEventManagerTest.php +++ b/test/StaticEventManagerTest.php @@ -15,21 +15,23 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace ZendTest\EventManager; -use Zend\EventManager\StaticEventManager, + +use PHPUnit_Framework_TestCase as TestCase, + stdClass, Zend\EventManager\EventManager, - PHPUnit_Framework_TestCase as TestCase; + Zend\EventManager\StaticEventManager; /** * @category Zend * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class StaticEventManagerTest extends TestCase @@ -83,6 +85,28 @@ public function testCanAttachCallbackToEvent() $this->assertTrue($found, 'Did not find listener!'); } + public function testCanAttachCallbackToMultipleEventsAtOnce() + { + $events = StaticEventManager::getInstance(); + $events->attach('bar', array('foo', 'test'), array($this, __FUNCTION__)); + $this->assertContains('foo', $events->getEvents('bar')); + $this->assertContains('test', $events->getEvents('bar')); + $expected = array($this, __FUNCTION__); + foreach (array('foo', 'test') as $event) { + $found = false; + $listeners = $events->getListeners('bar', $event); + $this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $listeners); + $this->assertTrue(0 < count($listeners), 'Empty listeners!'); + foreach ($listeners as $listener) { + if ($expected === $listener->getCallback()) { + $found = true; + break; + } + } + $this->assertTrue($found, 'Did not find listener!'); + } + } + public function testCanAttachSameEventToMultipleResourcesAtOnce() { $events = StaticEventManager::getInstance(); @@ -105,6 +129,49 @@ public function testCanAttachSameEventToMultipleResourcesAtOnce() } } + public function testCanAttachCallbackToMultipleEventsOnMultipleResourcesAtOnce() + { + $events = StaticEventManager::getInstance(); + $events->attach(array('bar', 'baz'), array('foo', 'test'), array($this, __FUNCTION__)); + $this->assertContains('foo', $events->getEvents('bar')); + $this->assertContains('test', $events->getEvents('bar')); + $expected = array($this, __FUNCTION__); + foreach (array('bar', 'baz') as $resource) { + foreach (array('foo', 'test') as $event) { + $found = false; + $listeners = $events->getListeners($resource, $event); + $this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $listeners); + $this->assertTrue(0 < count($listeners), 'Empty listeners!'); + foreach ($listeners as $listener) { + if ($expected === $listener->getCallback()) { + $found = true; + break; + } + } + $this->assertTrue($found, 'Did not find listener!'); + } + } + } + + public function testListenersAttachedUsingWildcardEventWillBeTriggeredByResource() + { + $test = new stdClass; + $test->events = array(); + $callback = function($e) use ($test) { + $test->events[] = $e->getName(); + }; + + $staticEvents = StaticEventManager::getInstance(); + $staticEvents->attach('bar', '*', $callback); + + $events = new EventManager('bar'); + + foreach (array('foo', 'bar', 'baz') as $event) { + $events->trigger($event); + $this->assertContains($event, $test->events); + } + } + public function testCanDetachListenerFromResource() { $events = StaticEventManager::getInstance(); diff --git a/test/StaticIntegrationTest.php b/test/StaticIntegrationTest.php index 0735264..7093fbe 100644 --- a/test/StaticIntegrationTest.php +++ b/test/StaticIntegrationTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -30,7 +30,7 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class StaticIntegrationTest extends TestCase diff --git a/test/TestAsset/ClassWithEvents.php b/test/TestAsset/ClassWithEvents.php index 12fa1f8..590bebe 100644 --- a/test/TestAsset/ClassWithEvents.php +++ b/test/TestAsset/ClassWithEvents.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -29,7 +29,7 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class ClassWithEvents diff --git a/test/TestAsset/MockAggregate.php b/test/TestAsset/MockAggregate.php index 2de0338..6f8272f 100644 --- a/test/TestAsset/MockAggregate.php +++ b/test/TestAsset/MockAggregate.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -29,16 +29,19 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class MockAggregate implements ListenerAggregate { protected $listeners = array(); + public $priority; - public function attach(EventCollection $events) + public function attach(EventCollection $events, $priority = null) { + $this->priority = $priority; + $listeners = array(); $listeners[] = $events->attach('foo.bar', array( $this, 'fooBar' )); $listeners[] = $events->attach('foo.baz', array( $this, 'fooBaz' )); diff --git a/test/TestAsset/StaticEventsMock.php b/test/TestAsset/StaticEventsMock.php index c27433e..cdf53eb 100644 --- a/test/TestAsset/StaticEventsMock.php +++ b/test/TestAsset/StaticEventsMock.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_EventManager * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * @package Zend_EventManager * @subpackage UnitTests * @group Zend_EventManager - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class StaticEventsMock implements StaticEventCollection