Browse files

Merge branch 'master' of git://github.com/zendframework/zf2

  • Loading branch information...
2 parents c7e6a11 + 2d341e6 commit cf353768cee05f32bccca5cb97293225daca0bf0 @sgehrig sgehrig committed Feb 2, 2011
View
35 library/Zend/EventManager/Event.php
@@ -23,6 +23,8 @@
*/
namespace Zend\EventManager;
+use ArrayAccess;
+
/**
* Representation of an event
*
@@ -47,7 +49,7 @@ class Event
protected $target;
/**
- * @var array|ArrayAccess The event parameters
+ * @var array|ArrayAccess|object The event parameters
*/
protected $params = array();
@@ -100,14 +102,14 @@ public function getTarget()
*
* Overwrites parameters
*
- * @param array|ArrayAccess $params
+ * @param array|ArrayAccess|object $params
* @return Event
*/
public function setParams($params)
{
- if (!is_array($params) && !$params instanceof \ArrayAccess) {
+ if (!is_array($params) && !is_object($params)) {
throw new Exception\InvalidArgumentException(sprintf(
- 'Event parameters must be an array or implement ArrayAccess; received "%s"',
+ 'Event parameters must be an array or object; received "%s"',
(is_object($params) ? get_class($params) : gettype($params))
));
}
@@ -119,7 +121,7 @@ public function setParams($params)
/**
* Get all parameters
*
- * @return array|ArrayAccess
+ * @return array|object|ArrayAccess
*/
public function getParams()
{
@@ -137,11 +139,20 @@ public function getParams()
*/
public function getParam($name, $default = null)
{
- if (!isset($this->params[$name])) {
- return $default;
+ // Check in params that are arrays or implement array access
+ if (is_array($this->params) || $this->params instanceof ArrayAccess) {
+ if (!isset($this->params[$name])) {
+ return $default;
+ }
+
+ return $this->params[$name];
}
- return $this->params[$name];
+ // Check in normal objects
+ if (!isset($this->params->{$name})) {
+ return $default;
+ }
+ return $this->params->{$name};
}
/**
@@ -153,7 +164,13 @@ public function getParam($name, $default = null)
*/
public function setParam($name, $value)
{
- $this->params[$name] = $value;
+ if (is_array($this->params) || $this->params instanceof ArrayAccess) {
+ // Arrays or objects implementing array access
+ $this->params[$name] = $value;
+ } else {
+ // Objects
+ $this->params->{$name} = $value;
+ }
return $this;
}
View
99 library/Zend/EventManager/EventCollection.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\EventManager;
+
+use Zend\Stdlib\CallbackHandler;
+
+/**
+ * Interface for messengers
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+interface EventCollection
+{
+ /**
+ * Trigger an event
+ *
+ * @param string $event
+ * @param object|string $context
+ * @param array|object $argv
+ * @return ResponseCollection
+ */
+ public function trigger($event, $context, $argv = array());
+
+ /**
+ * Trigger an event until the given callback returns a boolean false
+ *
+ * @param string $event
+ * @param object|string $context
+ * @param array|object $argv
+ * @param callback $callback
+ * @return ResponseCollection
+ */
+ public function triggerUntil($event, $context, $argv, $callback);
+
+ /**
+ * Attach a handler to an event
+ *
+ * @param string $event
+ * @param callback $callback
+ * @param int $priority Priority at which to register handler
+ * @return CallbackHandler
+ */
+ public function attach($event, $callback, $priority = 1);
+
+ /**
+ * Detach an event handler
+ *
+ * @param CallbackHandler $handle
+ * @return void
+ */
+ public function detach(CallbackHandler $handle);
+
+ /**
+ * Get a list of events for which this collection has handlers
+ *
+ * @return array
+ */
+ public function getEvents();
+
+ /**
+ * Retrieve a list of handlers registered to a given event
+ *
+ * @param string $event
+ * @return array|object
+ */
+ public function getHandlers($event);
+
+ /**
+ * Clear all handlers for a given event
+ *
+ * @param string $event
+ * @return void
+ */
+ public function clearHandlers($event);
+}
View
269 library/Zend/EventManager/EventManager.php
@@ -38,7 +38,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-class EventManager implements EventDispatcher
+class EventManager implements EventCollection
{
/**
* Subscribed events and their handlers
@@ -58,6 +58,12 @@ class EventManager implements EventDispatcher
protected $identifier;
/**
+ * Static connections
+ * @var false|null|StaticEventCollection
+ */
+ protected $staticConnections = null;
+
+ /**
* Constructor
*
* Allows optionally specifying an identifier to use to pull signals from a
@@ -84,34 +90,63 @@ public function setEventClass($class)
}
/**
- * Publish to all handlers for a given event
+ * Set static connections container
+ *
+ * @param null|StaticEventCollection $connections
+ * @return void
+ */
+ public function setStaticConnections(StaticEventCollection $connections = null)
+ {
+ if (null === $connections) {
+ $this->staticConnections = false;
+ } else {
+ $this->staticConnections = $connections;
+ }
+ return $this;
+ }
+
+ /**
+ * Get static connections container
+ *
+ * @return false|StaticEventCollection
+ */
+ public function getStaticConnections()
+ {
+ if (null === $this->staticConnections) {
+ $this->setStaticConnections(StaticEventManager::getInstance());
+ }
+ return $this->staticConnections;
+ }
+
+ /**
+ * Trigger all handlers for a given event
*
* @param string $event
* @param string|object $context Object calling emit, or symbol describing context (such as static method name)
* @param array|ArrayAccess $argv Array of arguments; typically, should be associative
* @return ResponseCollection All handler return values
*/
- public function emit($event, $context, $argv = array())
+ public function trigger($event, $context, $argv = array())
{
- return $this->emitUntil($event, $context, $argv, function(){
+ return $this->triggerUntil($event, $context, $argv, function(){
return false;
});
}
/**
- * Notify handlers until return value of one causes a callback to
+ * Trigger handlers until return value of one causes a callback to
* evaluate to true
*
- * Publishes handlers until the provided callback evaluates the return
+ * Triggers handlers until the provided callback evaluates the return
* value of one as true, or until all handlers have been executed.
*
- * @param Callable $callback
* @param string $event
* @param string|object $context Object calling emit, or symbol describing context (such as static method name)
* @param array|ArrayAccess $argv Array of arguments; typically, should be associative
+ * @param Callable $callback
* @throws InvalidCallbackException if invalid callback provided
*/
- public function emitUntil($event, $context, $argv, $callback)
+ public function triggerUntil($event, $context, $argv, $callback)
{
if (!is_callable($callback)) {
throw new InvalidCallbackException('Invalid callback provided');
@@ -121,7 +156,7 @@ public function emitUntil($event, $context, $argv, $callback)
$e = new $this->eventClass($event, $context, $argv);
if (empty($this->events[$event])) {
- return $this->emitStaticHandlers($callback, $e, $responses);
+ return $this->triggerStaticHandlers($callback, $e, $responses);
}
foreach ($this->events[$event] as $handler) {
@@ -136,64 +171,29 @@ public function emitUntil($event, $context, $argv, $callback)
}
}
if (!$responses->stopped()) {
- $this->emitStaticHandlers($callback, $e, $responses);
+ $this->triggerStaticHandlers($callback, $e, $responses);
}
return $responses;
}
/**
- * Subscribe to an event
- *
- * If an HandlerAggregate is provided as the first argument (as either a class
- * name or instance), this method will call the aggregate's connect()
- * method, passing itself as the argument. Once done, the HandlerAggregate
- * instance will be returned.
+ * Attach a handler to an event
*
- * Otherwise, the assumption is that the first argument is the event, and
- * that the next argument describes a callback that will respond to that
- * event. A CallbackHandler instance describing the event handler
- * combination will be returned.
+ * The first argument is the event, and the next argument describes a
+ * callback that will respond to that event. A CallbackHandler instance
+ * describing the event handler combination will be returned.
*
* The last argument indicates a priority at which the event should be
- * executed. By default, this value is 1000; however, you may set it for any
+ * 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|HandlerAggregate $eventOrAggregate
- * @param null|callback $callback PHP callback
- * @param null|int $priority If provided, the priority at which to register the callback
+ * @param string $event
+ * @param callback $callback PHP callback
+ * @param int $priority If provided, the priority at which to register the callback
* @return HandlerAggregate|CallbackHandler (to allow later unsubscribe)
*/
- public function connect($eventOrAggregate, $callback = null, $priority = 1000)
+ public function attach($event, $callback, $priority = 1)
{
- if (null === $callback) {
- // Assuming we have an aggregate that will self-register
- if (is_string($eventOrAggregate)) {
- // Class name?
- if (!class_exists($eventOrAggregate)) {
- // Class doesn't exist; probably didn't provide a context
- throw new Exception\InvalidArgumentException(sprintf(
- 'No context provided for event "%s"',
- $eventOrAggregate
- ));
- }
- // Create instance
- $eventOrAggregate = new $eventOrAggregate();
- }
- if (!$eventOrAggregate instanceof HandlerAggregate) {
- // Not an HandlerAggregate? We don't know how to handle it.
- throw new Exception\InvalidArgumentException(
- 'Invalid class or object provided as event aggregate; must implement HandlerAggregate'
- );
- }
-
- // Have the event aggregate wire itself, and return it.
- $eventOrAggregate->connect($this);
- return $eventOrAggregate;
- }
-
- // Handle normal events
- $event = $eventOrAggregate;
-
if (empty($this->events[$event])) {
$this->events[$event] = new PriorityQueue();
}
@@ -203,25 +203,90 @@ public function connect($eventOrAggregate, $callback = null, $priority = 1000)
}
/**
+ * Attach a handler aggregate
+ *
+ * Handler aggregates accept an EventCollection instance, and call attach()
+ * one or more times, typically to attach to multiple events using local
+ * methods.
+ *
+ * @param HandlerAggregate|string $aggregate
+ * @return HandlerAggregate
+ */
+ public function attachAggregate($aggregate)
+ {
+ if (is_string($aggregate)) {
+ // Class name?
+ if (!class_exists($aggregate)) {
+ // Class doesn't exist; probably didn't provide a context
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'No context provided for event "%s"',
+ $aggregate
+ ));
+ }
+ // Create instance
+ $aggregate = new $aggregate();
+ }
+ if (!$aggregate instanceof HandlerAggregate) {
+ // Not an HandlerAggregate? We don't know how to handle it.
+ throw new Exception\InvalidArgumentException(
+ 'Invalid class or object provided as event aggregate; must implement HandlerAggregate'
+ );
+ }
+
+ // Have the event aggregate wire itself, and return it.
+ $aggregate->attach($this);
+ return $aggregate;
+ }
+
+ /**
* Unsubscribe a handler from an event
*
- * @param HandlerAggregate|CallbackHandler $handler
+ * @param CallbackHandler $handler
* @return bool Returns true if event and handle found, and unsubscribed; returns false if either event or handle not found
*/
- public function detach($handler)
+ public function detach(CallbackHandler $handler)
{
- if (!$handler instanceof HandlerAggregate && !$handler instanceof CallbackHandler) {
- throw new Exception\InvalidArgumentException(sprintf(
- 'Expected CallbackHandler or HandlerAggregate; received "%s"',
- (is_object($handler) ? get_class($handler) : gettype($handler))
- ));
+ $event = $handler->getEvent();
+ if (empty($this->events[$event])) {
+ return false;
}
-
- if ($handler instanceof HandlerAggregate) {
- return $this->detachAggregate($handler);
+ $return = $this->events[$event]->remove($handler);
+ if (!$return) {
+ return false;
}
+ if (!count($this->events[$event])) {
+ unset($this->events[$event]);
+ }
+ return true;
+ }
- return $this->detachHandler($handler);
+ /**
+ * Detach a callback aggregate
+ *
+ * Loops through all handlers of all events to identify handlers that are
+ * represented by the aggregate; for all matches, the handlers will be
+ * removed.
+ *
+ * @param HandlerAggregate $aggregate
+ * @return bool
+ */
+ public function detachAggregate(HandlerAggregate $aggregate)
+ {
+ foreach ($this->events as $event => $handlers) {
+ foreach ($handlers as $key => $handler) {
+ $callback = $handler->getCallback();
+ if (is_object($callback)) {
+ if ($callback === $aggregate) {
+ $this->detach($handler);
+ }
+ } elseif (is_array($callback)) {
+ if ($callback[0] === $aggregate) {
+ $this->detach($handler);
+ }
+ }
+ }
+ }
+ return true;
}
/**
@@ -266,7 +331,7 @@ public function clearHandlers($event)
*
* Use this method if you want to be able to modify arguments from within a
* handler. It returns an ArrayObject of the arguments, which may then be
- * passed to emit() or emitUntil().
+ * passed to trigger() or triggerUntil().
*
* @param array $args
* @return ArrayObject
@@ -277,72 +342,38 @@ public function prepareArgs(array $args)
}
/**
- * Detach a HandlerAggregate
- *
- * Iterates through all events, testing handlers to determine if they
- * represent the provided HandlerAggregate; if so, they are then removed.
- *
- * @param HandlerAggregate $aggregate
- * @return true
- */
- protected function detachAggregate(HandlerAggregate $aggregate)
- {
- foreach ($this->events as $event => $handlers) {
- foreach ($handlers as $key => $handler) {
- $callback = $handler->getCallback();
- if (is_object($callback)) {
- if ($callback === $aggregate) {
- $this->detachHandler($handler);
- }
- } elseif (is_array($callback)) {
- if ($callback[0] === $aggregate) {
- $this->detachHandler($handler);
- }
- }
- }
- }
- return true;
- }
-
- /**
- * Detach an event handler
- *
- * @param CallbackHandler $handler
- * @return bool
- */
- protected function detachHandler(CallbackHandler $handler)
- {
- $event = $handler->getEvent();
- if (empty($this->events[$event])) {
- return false;
- }
- return $this->events[$event]->remove($handler);
- }
-
- /**
* Emit handlers matching the current identifier found in the static handler
*
* @param callback $callback
* @param Event $event
* @param ResponseCollection $responses
* @return ResponseCollection
*/
- protected function emitStaticHandlers($callback, Event $event, ResponseCollection $responses)
+ protected function triggerStaticHandlers($callback, Event $event, ResponseCollection $responses)
{
- if (!$handlers = StaticEventManager::getInstance()->getHandlers($this->identifier, $event->getName())) {
+ if (!$staticConnections = $this->getStaticConnections()) {
return $responses;
}
- foreach ($handlers as $handler) {
- $responses->push(call_user_func($handler->getCallback(), $event));
- if ($event->propagationIsStopped()) {
- $responses->setStopped(true);
- break;
+
+ $identifiers = (array) $this->identifier;
+
+ foreach ($identifiers as $id) {
+ if (!$handlers = $staticConnections->getHandlers($id, $event->getName())) {
+ continue;
}
- if (call_user_func($callback, $responses->last())) {
- $responses->setStopped(true);
- break;
+ foreach ($handlers as $handler) {
+ $responses->push(call_user_func($handler->getCallback(), $event));
+ if ($event->propagationIsStopped()) {
+ $responses->setStopped(true);
+ break;
+ }
+ if (call_user_func($callback, $responses->last())) {
+ $responses->setStopped(true);
+ break;
+ }
}
}
+
return $responses;
}
}
View
41 library/Zend/EventManager/Filter.php
@@ -35,10 +35,49 @@
*/
interface Filter
{
+ /**
+ * Execute the filter chain
+ *
+ * @param string|object $context
+ * @param array $params
+ * @return mixed
+ */
public function run($context, array $params = array());
- public function connect($callback);
+
+ /**
+ * Attach an intercepting filter
+ *
+ * @param callback $callback
+ * @return CallbackHandler
+ */
+ public function attach($callback);
+
+ /**
+ * Detach an intercepting filter
+ *
+ * @param CallbackHandler $filter
+ * @return bool
+ */
public function detach(CallbackHandler $filter);
+
+ /**
+ * Get all intercepting filters
+ *
+ * @return array
+ */
public function getFilters();
+
+ /**
+ * Clear all filters
+ *
+ * @return void
+ */
public function clearFilters();
+
+ /**
+ * Get all filter responses
+ *
+ * @return ResponseCollection
+ */
public function getResponses();
}
View
6 library/Zend/EventManager/FilterChain.php
@@ -82,10 +82,10 @@ public function run($context, array $argv = array())
* Connect a filter to the chain
*
* @param callback $callback PHP Callback
- * @param int $priority Priority in the queue at which to execute; defaults to 1000 (higher numbers == higher priority)
+ * @param int $priority Priority in the queue at which to execute; defaults to 1 (higher numbers == higher priority)
* @return CallbackHandler (to allow later unsubscribe)
*/
- public function connect($callback, $priority = 1000)
+ public function attach($callback, $priority = 1)
{
if (empty($callback)) {
throw new InvalidCallbackException('No callback provided');
@@ -96,7 +96,7 @@ public function connect($callback, $priority = 1000)
}
/**
- * Unsubscribe a filter
+ * Detach a filter from the chain
*
* @param CallbackHandler $filter
* @return bool Returns true if filter found and unsubscribed; returns false otherwise
View
155 library/Zend/EventManager/GlobalEventManager.php
@@ -0,0 +1,155 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\EventManager;
+
+use Zend\Stdlib\CallbackHandler,
+ Zend\Stdlib\PriorityQueue,
+ ArrayObject;
+
+/**
+ * Event manager: notification system
+ *
+ * Use the EventManager when you want to create a per-instance notification
+ * system for your objects.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class GlobalEventManager
+{
+ /**
+ * @var EventCollection
+ */
+ protected static $events;
+
+ /**
+ * Set the event collection on which this will operate
+ *
+ * @param null|EventCollection $events
+ * @return void
+ */
+ public static function setEventCollection(EventCollection $events = null)
+ {
+ static::$events = $events;
+ }
+
+ /**
+ * Get event collection on which this operates
+ *
+ * @return void
+ */
+ public static function getEventCollection()
+ {
+ if (null === static::$events) {
+ static::setEventCollection(new EventManager());
+ }
+ return static::$events;
+ }
+
+ /**
+ * Trigger an event
+ *
+ * @param string $event
+ * @param object|string $context
+ * @param array|object $argv
+ * @return ResponseCollection
+ */
+ public static function trigger($event, $context, $argv = array())
+ {
+ return static::getEventCollection()->trigger($event, $context, $argv);
+ }
+
+ /**
+ * Trigger handlers until return value of one causes a callback to evaluate
+ * to true.
+ *
+ * @param string $event
+ * @param string|object $context
+ * @param array|object $argv
+ * @param callback $callback
+ * @return ResponseCollection
+ */
+ public static function triggerUntil($event, $context, $argv, $callback)
+ {
+ return static::getEventCollection()->triggerUntil($event, $context, $argv, $callback);
+ }
+
+ /**
+ * Attach a handler to an event
+ *
+ * @param string $event
+ * @param callback $callback
+ * @param int $priority
+ * @return CallbackHandler
+ */
+ public static function attach($event, $callback, $priority = 1)
+ {
+ return static::getEventCollection()->attach($event, $callback, $priority);
+ }
+
+ /**
+ * Detach a callback from a handler
+ *
+ * @param CallbackHandler $handler
+ * @return bool
+ */
+ public static function detach(CallbackHandler $handler)
+ {
+ return static::getEventCollection()->detach($handler);
+ }
+
+ /**
+ * Retrieve list of events this object manages
+ *
+ * @return array
+ */
+ public static function getEvents()
+ {
+ return static::getEventCollection()->getEvents();
+ }
+
+ /**
+ * Retrieve all handlers for a given event
+ *
+ * @param string $event
+ * @return PriorityQueue|array
+ */
+ public static function getHandlers($event)
+ {
+ return static::getEventCollection()->getHandlers($event);
+ }
+
+ /**
+ * Clear all handlers for a given event
+ *
+ * @param string $event
+ * @return void
+ */
+ public static function clearHandlers($event)
+ {
+ return static::getEventCollection()->clearHandlers($event);
+ }
+}
View
2 library/Zend/EventManager/HandlerAggregate.php
@@ -38,5 +38,5 @@
*/
interface HandlerAggregate
{
- public function connect(EventDispatcher $events);
+ public function attach(EventCollection $events);
}
View
4 ...nd/EventManager/StaticEventDispatcher.php → ...nd/EventManager/StaticEventCollection.php
@@ -24,14 +24,14 @@
namespace Zend\EventManager;
/**
- * Interface for global (static) message delivery
+ * Interface for global (static) event handler collections
*
* @category Zend
* @package Zend_EventManager
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-interface StaticEventDispatcher
+interface StaticEventCollection
{
public function getHandlers($id, $event);
}
View
16 library/Zend/EventManager/StaticEventManager.php
@@ -23,6 +23,8 @@
*/
namespace Zend\EventManager;
+use Zend\Stdlib\CallbackHandler;
+
/**
* Static version of EventManager
*
@@ -31,7 +33,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-class StaticEventManager implements StaticEventDispatcher
+class StaticEventManager implements StaticEventCollection
{
/**
* @var StaticEventManager
@@ -100,30 +102,30 @@ public static function resetInstance()
* </code>
*
* @param string|array $id Identifier(s) for event emitting component(s)
- * @param string|HandlerAggregate $event
- * @param null|callback $callback PHP Callback
+ * @param string $event
+ * @param callback $callback PHP Callback
* @param int $priority Priority at which handler should execute
* @return void
*/
- public function connect($id, $eventOrAggregate, $callback = null, $priority = 1000)
+ public function attach($id, $event, $callback, $priority = 1000)
{
$ids = (array) $id;
foreach ($ids as $id) {
if (!array_key_exists($id, $this->identifiers)) {
$this->identifiers[$id] = new EventManager();
}
- $this->identifiers[$id]->connect($eventOrAggregate, $callback, $priority);
+ $this->identifiers[$id]->attach($event, $callback, $priority);
}
}
/**
* Detach a handler from an event offered by a given resource
*
* @param string|int $id
- * @param HandlerAggregate|\Zend\Stdlib\CallbackHandler $handler
+ * @param CallbackHandler $handler
* @return bool Returns true if event and handler found, and unsubscribed; returns false if either event or handler not found
*/
- public function detach($id, $handler)
+ public function detach($id, CallbackHandler $handler)
{
if (!array_key_exists($id, $this->identifiers)) {
return false;
View
4 library/Zend/Session/Manager.php
@@ -23,7 +23,7 @@
*/
namespace Zend\Session;
-use Zend\EventManager\EventDispatcher;
+use Zend\EventManager\EventCollection;
/**
* Session manager interface
@@ -55,7 +55,7 @@ public function rememberMe($ttl = null);
public function forgetMe();
public function expireSessionCookie();
- public function setValidatorChain(EventDispatcher $chain);
+ public function setValidatorChain(EventCollection $chain);
public function getValidatorChain();
public function isValid();
}
View
20 library/Zend/Session/SessionManager.php
@@ -24,7 +24,7 @@
namespace Zend\Session;
use Zend\Validator\Alnum as AlnumValidator,
- Zend\EventManager\EventDispatcher;
+ Zend\EventManager\EventCollection;
/**
* Session Manager implementation utilizing ext/session
@@ -63,7 +63,7 @@ class SessionManager extends AbstractManager
protected $_name;
/**
- * @var EventDispatcher Validation chain to determine if session is valid
+ * @var EventCollection Validation chain to determine if session is valid
*/
protected $_validatorChain;
@@ -298,10 +298,10 @@ public function forgetMe()
*
* In most cases, you should use an instance of {@link ValidatorChain}.
*
- * @param EventDispatcher $chain
+ * @param EventCollection $chain
* @return SessionManager
*/
- public function setValidatorChain(EventDispatcher $chain)
+ public function setValidatorChain(EventCollection $chain)
{
$this->_validatorChain = $chain;
return $this;
@@ -333,13 +333,15 @@ public function getValidatorChain()
public function isValid()
{
$validator = $this->getValidatorChain();
- $responses = $validator->emitUntil(function($test) {
+ $responses = $validator->triggerUntil('session.validate', $this, array($this), function($test) {
return !$test;
- }, 'session.validate', $this);
- if (null === $responses->last()) {
- return true;
+ });
+ if ($responses->stopped()) {
+ // If execution was halted, validation failed
+ return false;
}
- return (bool) $responses->last();
+ // Otherwise, we're good to go
+ return true;
}
/**
View
16 library/Zend/Session/ValidatorChain.php
@@ -63,17 +63,15 @@ public function __construct(Storage $storage)
/**
* Attach a handler to the session validator chain
*
- * @param string|\Zend\EventManager\HandlerAggregate $eventOrAggregate
- * @param string|object|Closure $context
- * @param null|string $handler
- * @return Zend\Stdlib\SignalHandler
+ * @param string $event
+ * @param callback $context
+ * @param int $priority
+ * @return Zend\Stdlib\CallbackHandler
*/
- public function connect($eventOrAggregate, $callback = null, $priority = 1000)
+ public function attach($event, $callback, $priority = 1)
{
$context = null;
- if (null === $callback) {
- $context = $eventOrAggregate;
- } elseif ($callback instanceof Validator) {
+ if ($callback instanceof Validator) {
$context = $callback;
} elseif (is_array($callback)) {
$test = array_shift($callback);
@@ -88,7 +86,7 @@ public function connect($eventOrAggregate, $callback = null, $priority = 1000)
$this->getStorage()->setMetadata('_VALID', array($name => $data));
}
- $handle = parent::connect($eventOrAggregate, $callback, $priority);
+ $handle = parent::connect($event, $callback, $priority);
return $handle;
}
View
150 tests/Zend/EventManager/EventManagerTest.php
@@ -42,33 +42,33 @@ public function setUp()
$this->events = new EventManager;
}
- public function testConnectShouldReturnCallbackHandler()
+ public function testAttachShouldReturnCallbackHandler()
{
- $handle = $this->events->connect('test', array($this, __METHOD__));
+ $handle = $this->events->attach('test', array($this, __METHOD__));
$this->assertTrue($handle instanceof CallbackHandler);
}
- public function testConnectShouldAddHandlerToEvent()
+ public function testAttachShouldAddHandlerToEvent()
{
- $handle = $this->events->connect('test', array($this, __METHOD__));
+ $handle = $this->events->attach('test', array($this, __METHOD__));
$handles = $this->events->getHandlers('test');
$this->assertEquals(1, count($handles));
$this->assertContains($handle, $handles);
}
- public function testConnectShouldAddEventIfItDoesNotExist()
+ public function testAttachShouldAddEventIfItDoesNotExist()
{
$events = $this->events->getEvents();
$this->assertTrue(empty($events), var_export($events, 1));
- $handle = $this->events->connect('test', array($this, __METHOD__));
+ $handle = $this->events->attach('test', array($this, __METHOD__));
$events = $this->events->getEvents();
$this->assertFalse(empty($events));
$this->assertContains('test', $events);
}
public function testDetachShouldRemoveHandlerFromEvent()
{
- $handle = $this->events->connect('test', array($this, __METHOD__));
+ $handle = $this->events->attach('test', array($this, __METHOD__));
$handles = $this->events->getHandlers('test');
$this->assertContains($handle, $handles);
$this->events->detach($handle);
@@ -78,62 +78,62 @@ public function testDetachShouldRemoveHandlerFromEvent()
public function testDetachShouldReturnFalseIfEventDoesNotExist()
{
- $handle = $this->events->connect('test', array($this, __METHOD__));
+ $handle = $this->events->attach('test', array($this, __METHOD__));
$this->events->clearHandlers('test');
$this->assertFalse($this->events->detach($handle));
}
public function testDetachShouldReturnFalseIfHandlerDoesNotExist()
{
- $handle1 = $this->events->connect('test', array($this, __METHOD__));
+ $handle1 = $this->events->attach('test', array($this, __METHOD__));
$this->events->clearHandlers('test');
- $handle2 = $this->events->connect('test', array($this, 'handleTestEvent'));
+ $handle2 = $this->events->attach('test', array($this, 'handleTestEvent'));
$this->assertFalse($this->events->detach($handle1));
}
- public function testRetrievingConnectedHandlersShouldReturnEmptyArrayWhenEventDoesNotExist()
+ public function testRetrievingAttachedHandlersShouldReturnEmptyArrayWhenEventDoesNotExist()
{
$handles = $this->events->getHandlers('test');
$this->assertEquals(0, count($handles));
}
- public function testEmitShouldEmitConnectedHandlers()
+ public function testTriggerShouldTriggerAttachedHandlers()
{
- $handle = $this->events->connect('test', array($this, 'handleTestEvent'));
- $this->events->emit('test', $this, array('message' => 'test message'));
+ $handle = $this->events->attach('test', array($this, 'handleTestEvent'));
+ $this->events->trigger('test', $this, array('message' => 'test message'));
$this->assertEquals('test message', $this->message);
}
- public function testEmitShouldReturnAllHandlerReturnValues()
+ public function testTriggerShouldReturnAllHandlerReturnValues()
{
- $this->events->connect('string.transform', function ($e) {
+ $this->events->attach('string.transform', function ($e) {
$string = $e->getParam('string', '__NOT_FOUND__');
return trim($string);
});
- $this->events->connect('string.transform', function ($e) {
+ $this->events->attach('string.transform', function ($e) {
$string = $e->getParam('string', '__NOT_FOUND__');
return str_rot13($string);
});
- $responses = $this->events->emit('string.transform', $this, array('string' => ' foo '));
+ $responses = $this->events->trigger('string.transform', $this, array('string' => ' foo '));
$this->assertTrue($responses instanceof ResponseCollection);
$this->assertEquals(2, $responses->count());
$this->assertEquals('foo', $responses->first());
$this->assertEquals(\str_rot13(' foo '), $responses->last());
}
- public function testEmitUntilShouldReturnAsSoonAsCallbackReturnsTrue()
+ public function testTriggerUntilShouldReturnAsSoonAsCallbackReturnsTrue()
{
- $this->events->connect('foo.bar', function ($e) {
+ $this->events->attach('foo.bar', function ($e) {
$string = $e->getParam('string', '');
$search = $e->getParam('search', '?');
return strpos($string, $search);
});
- $this->events->connect('foo.bar', function ($e) {
+ $this->events->attach('foo.bar', function ($e) {
$string = $e->getParam('string', '');
$search = $e->getParam('search', '?');
return strstr($string, $search);
});
- $responses = $this->events->emitUntil(
+ $responses = $this->events->triggerUntil(
'foo.bar',
$this,
array('string' => 'foo', 'search' => 'f'),
@@ -143,17 +143,17 @@ public function testEmitUntilShouldReturnAsSoonAsCallbackReturnsTrue()
$this->assertSame(0, $responses->last());
}
- public function testEmitResponseCollectionContains()
+ public function testTriggerResponseCollectionContains()
{
- $this->events->connect('string.transform', function ($e) {
+ $this->events->attach('string.transform', function ($e) {
$string = $e->getParam('string', '');
return trim($string);
});
- $this->events->connect('string.transform', function ($e) {
+ $this->events->attach('string.transform', function ($e) {
$string = $e->getParam('string', '');
return str_rot13($string);
});
- $responses = $this->events->emit('string.transform', $this, array('string' => ' foo '));
+ $responses = $this->events->trigger('string.transform', $this, array('string' => ' foo '));
$this->assertTrue($responses->contains('foo'));
$this->assertTrue($responses->contains(\str_rot13(' foo ')));
$this->assertFalse($responses->contains(' foo '));
@@ -170,13 +170,13 @@ public function evaluateStringCallback($value)
return (!$value);
}
- public function testEmitUntilShouldMarkResponseCollectionStoppedWhenConditionMet()
+ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenConditionMet()
{
- $this->events->connect('foo.bar', function () { return 'bogus'; }, 4);
- $this->events->connect('foo.bar', function () { return 'nada'; }, 3);
- $this->events->connect('foo.bar', function () { return 'found'; }, 2);
- $this->events->connect('foo.bar', function () { return 'zero'; }, 1);
- $responses = $this->events->emitUntil('foo.bar', $this, array(), function ($result) {
+ $this->events->attach('foo.bar', function () { return 'bogus'; }, 4);
+ $this->events->attach('foo.bar', function () { return 'nada'; }, 3);
+ $this->events->attach('foo.bar', function () { return 'found'; }, 2);
+ $this->events->attach('foo.bar', function () { return 'zero'; }, 1);
+ $responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
return ($result === 'found');
});
$this->assertTrue($responses instanceof ResponseCollection);
@@ -186,77 +186,77 @@ public function testEmitUntilShouldMarkResponseCollectionStoppedWhenConditionMet
$this->assertFalse($responses->contains('zero'));
}
- public function testEmitUntilShouldMarkResponseCollectionStoppedWhenConditionMetByLastHandler()
+ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenConditionMetByLastHandler()
{
- $this->events->connect('foo.bar', function () { return 'bogus'; });
- $this->events->connect('foo.bar', function () { return 'nada'; });
- $this->events->connect('foo.bar', function () { return 'zero'; });
- $this->events->connect('foo.bar', function () { return 'found'; });
- $responses = $this->events->emitUntil('foo.bar', $this, array(), function ($result) {
+ $this->events->attach('foo.bar', function () { return 'bogus'; });
+ $this->events->attach('foo.bar', function () { return 'nada'; });
+ $this->events->attach('foo.bar', function () { return 'zero'; });
+ $this->events->attach('foo.bar', function () { return 'found'; });
+ $responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
return ($result === 'found');
});
$this->assertTrue($responses instanceof ResponseCollection);
$this->assertTrue($responses->stopped());
$this->assertEquals('found', $responses->last());
}
- public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByEmitUntil()
+ public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByTriggerUntil()
{
- $this->events->connect('foo.bar', function () { return 'bogus'; }, 4);
- $this->events->connect('foo.bar', function () { return 'nada'; }, 3);
- $this->events->connect('foo.bar', function () { return 'found'; }, 2);
- $this->events->connect('foo.bar', function () { return 'zero'; }, 1);
- $responses = $this->events->emitUntil('foo.bar', $this, array(), function ($result) {
+ $this->events->attach('foo.bar', function () { return 'bogus'; }, 4);
+ $this->events->attach('foo.bar', function () { return 'nada'; }, 3);
+ $this->events->attach('foo.bar', function () { return 'found'; }, 2);
+ $this->events->attach('foo.bar', function () { return 'zero'; }, 1);
+ $responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
return ($result === 'never found');
});
$this->assertTrue($responses instanceof ResponseCollection);
$this->assertFalse($responses->stopped());
$this->assertEquals('zero', $responses->last());
}
- public function testConnectAllowsPassingAHandlerAggregateInstance()
+ public function testCanAttachAggregateInstances()
{
$aggregate = new TestAsset\MockAggregate();
- $this->events->connect($aggregate);
+ $this->events->attachAggregate($aggregate);
$events = $this->events->getEvents();
foreach (array('foo.bar', 'foo.baz') as $event) {
$this->assertContains($event, $events);
}
}
- public function testPassingHandlerAggregateInstanceToConnectReturnsHandlerAggregate()
+ public function testHandlerAggregateReturnedByAttachAggregate()
{
$aggregate = new TestAsset\MockAggregate();
- $test = $this->events->connect($aggregate);
+ $test = $this->events->attachAggregate($aggregate);
$this->assertSame($aggregate, $test);
}
- public function testConnectAllowsPassingAHandlerAggregateClassName()
+ public function testAttachAggregateAllowsPassingAHandlerAggregateClassName()
{
- $this->events->connect('ZendTest\EventManager\TestAsset\MockAggregate');
+ $this->events->attachAggregate('ZendTest\EventManager\TestAsset\MockAggregate');
$events = $this->events->getEvents();
foreach (array('foo.bar', 'foo.baz') as $event) {
$this->assertContains($event, $events);
}
}
- public function testPassingHandlerAggregateClassNameToConnectReturnsHandlerAggregateInstance()
+ public function testPassingClassNameToAttachAggregateReturnsHandlerAggregateInstance()
{
- $test = $this->events->connect('ZendTest\EventManager\TestAsset\MockAggregate');
+ $test = $this->events->attachAggregate('ZendTest\EventManager\TestAsset\MockAggregate');
$this->assertInstanceOf('ZendTest\EventManager\TestAsset\MockAggregate', $test);
}
public function testCanDetachHandlerAggregates()
{
// setup some other event handlers, to ensure appropriate items are detached
- $handlerFooBar1 = $this->events->connect('foo.bar', function(){ return true; });
- $handlerFooBar2 = $this->events->connect('foo.bar', function(){ return true; });
- $handlerFooBaz1 = $this->events->connect('foo.baz', function(){ return true; });
- $handlerOther = $this->events->connect('other', function(){ return true; });
+ $handlerFooBar1 = $this->events->attach('foo.bar', function(){ return true; });
+ $handlerFooBar2 = $this->events->attach('foo.bar', function(){ return true; });
+ $handlerFooBaz1 = $this->events->attach('foo.baz', function(){ return true; });
+ $handlerOther = $this->events->attach('other', function(){ return true; });
$aggregate = new TestAsset\MockAggregate();
- $this->events->connect($aggregate);
- $this->events->detach($aggregate);
+ $this->events->attachAggregate($aggregate);
+ $this->events->detachAggregate($aggregate);
$events = $this->events->getEvents();
foreach (array('foo.bar', 'foo.baz', 'other') as $event) {
$this->assertContains($event, $events);
@@ -278,11 +278,11 @@ public function testCanDetachHandlerAggregates()
public function testCallingEventsStopPropagationMethodHaltsEventEmission()
{
- $this->events->connect('foo.bar', function ($e) { return 'bogus'; }, 4);
- $this->events->connect('foo.bar', function ($e) { $e->stopPropagation(true); return 'nada'; }, 3);
- $this->events->connect('foo.bar', function ($e) { return 'found'; }, 2);
- $this->events->connect('foo.bar', function ($e) { return 'zero'; }, 1);
- $responses = $this->events->emit('foo.bar', $this, array());
+ $this->events->attach('foo.bar', function ($e) { return 'bogus'; }, 4);
+ $this->events->attach('foo.bar', function ($e) { $e->stopPropagation(true); return 'nada'; }, 3);
+ $this->events->attach('foo.bar', function ($e) { return 'found'; }, 2);
+ $this->events->attach('foo.bar', function ($e) { return 'zero'; }, 1);
+ $responses = $this->events->trigger('foo.bar', $this, array());
$this->assertTrue($responses instanceof ResponseCollection);
$this->assertTrue($responses->stopped());
$this->assertEquals('nada', $responses->last());
@@ -293,25 +293,35 @@ public function testCallingEventsStopPropagationMethodHaltsEventEmission()
public function testCanAlterParametersWithinAEvent()
{
- $this->events->connect('foo.bar', function ($e) { $e->setParam('foo', 'bar'); });
- $this->events->connect('foo.bar', function ($e) { $e->setParam('bar', 'baz'); });
- $this->events->connect('foo.bar', function ($e) {
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('foo', 'bar'); });
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('bar', 'baz'); });
+ $this->events->attach('foo.bar', function ($e) {
$foo = $e->getParam('foo', '__NO_FOO__');
$bar = $e->getParam('bar', '__NO_BAR__');
return $foo . ":" . $bar;
});
- $responses = $this->events->emit('foo.bar', $this, array());
+ $responses = $this->events->trigger('foo.bar', $this, array());
$this->assertEquals('bar:baz', $responses->last());
}
public function testParametersArePassedToEventByReference()
{
$params = array( 'foo' => 'bar', 'bar' => 'baz');
$args = $this->events->prepareArgs($params);
- $this->events->connect('foo.bar', function ($e) { $e->setParam('foo', 'FOO'); });
- $this->events->connect('foo.bar', function ($e) { $e->setParam('bar', 'BAR'); });
- $responses = $this->events->emit('foo.bar', $this, $args);
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('foo', 'FOO'); });
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('bar', 'BAR'); });
+ $responses = $this->events->trigger('foo.bar', $this, $args);
$this->assertEquals('FOO', $args['foo']);
$this->assertEquals('BAR', $args['bar']);
}
+
+ public function testCanPassObjectForEventParameters()
+ {
+ $params = (object) array( 'foo' => 'bar', 'bar' => 'baz');
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('foo', 'FOO'); });
+ $this->events->attach('foo.bar', function ($e) { $e->setParam('bar', 'BAR'); });
+ $responses = $this->events->trigger('foo.bar', $this, $params);
+ $this->assertEquals('FOO', $params->foo);
+ $this->assertEquals('BAR', $params->bar);
+ }
}
View
26 tests/Zend/EventManager/FilterChainTest.php
@@ -44,21 +44,21 @@ public function setUp()
public function testSubscribeShouldReturnCallbackHandler()
{
- $handle = $this->filterchain->connect(array( $this, __METHOD__ ));
+ $handle = $this->filterchain->attach(array( $this, __METHOD__ ));
$this->assertTrue($handle instanceof CallbackHandler);
}
public function testSubscribeShouldAddCallbackHandlerToFilters()
{
- $handler = $this->filterchain->connect(array($this, __METHOD__));
+ $handler = $this->filterchain->attach(array($this, __METHOD__));
$handlers = $this->filterchain->getFilters();
$this->assertEquals(1, count($handlers));
$this->assertTrue($handlers->contains($handler));
}
public function testDetachShouldRemoveCallbackHandlerFromFilters()
{
- $handle = $this->filterchain->connect(array( $this, __METHOD__ ));
+ $handle = $this->filterchain->attach(array( $this, __METHOD__ ));
$handles = $this->filterchain->getFilters();
$this->assertTrue($handles->contains($handle));
$this->filterchain->detach($handle);
@@ -68,28 +68,28 @@ public function testDetachShouldRemoveCallbackHandlerFromFilters()
public function testDetachShouldReturnFalseIfCallbackHandlerDoesNotExist()
{
- $handle1 = $this->filterchain->connect(array( $this, __METHOD__ ));
+ $handle1 = $this->filterchain->attach(array( $this, __METHOD__ ));
$this->filterchain->clearFilters();
- $handle2 = $this->filterchain->connect(array( $this, 'handleTestTopic' ));
+ $handle2 = $this->filterchain->attach(array( $this, 'handleTestTopic' ));
$this->assertFalse($this->filterchain->detach($handle1));
}
- public function testRetrievingConnectedFiltersShouldReturnEmptyArrayWhenNoFiltersExist()
+ public function testRetrievingAttachedFiltersShouldReturnEmptyArrayWhenNoFiltersExist()
{
$handles = $this->filterchain->getFilters();
$this->assertEquals(0, count($handles));
}
public function testFilterChainShouldReturnLastResponse()
{
- $this->filterchain->connect(function($context, $params, $chain) {
+ $this->filterchain->attach(function($context, $params, $chain) {
if (isset($params['string'])) {
$params['string'] = trim($params['string']);
}
$return = $chain->next($context, $params, $chain);
return $return;
});
- $this->filterchain->connect(function($context, array $params) {
+ $this->filterchain->attach(function($context, array $params) {
$string = isset($params['string']) ? $params['string'] : '';
return str_rot13($string);
});
@@ -99,7 +99,7 @@ public function testFilterChainShouldReturnLastResponse()
public function testFilterIsPassedContextAndArguments()
{
- $this->filterchain->connect(array( $this, 'filterTestCallback1' ));
+ $this->filterchain->attach(array( $this, 'filterTestCallback1' ));
$obj = (object) array('foo' => 'bar', 'bar' => 'baz');
$value = $this->filterchain->run($this, array('object' => $obj));
$this->assertEquals('filtered', $value);
@@ -109,23 +109,23 @@ public function testFilterIsPassedContextAndArguments()
public function testInterceptingFilterShouldReceiveChain()
{
- $this->filterchain->connect(array($this, 'filterReceivalCallback'));
+ $this->filterchain->attach(array($this, 'filterReceivalCallback'));
$this->filterchain->run($this);
}
public function testFilteringStopsAsSoonAsAFilterFailsToCallNext()
{
- $this->filterchain->connect(function($context, $params, $chain) {
+ $this->filterchain->attach(function($context, $params, $chain) {
if (isset($params['string'])) {
$params['string'] = trim($params['string']);
}
return $chain->next($context, $params, $chain);
}, 10000);
- $this->filterchain->connect(function($context, array $params) {
+ $this->filterchain->attach(function($context, array $params) {
$string = isset($params['string']) ? $params['string'] : '';
return str_rot13($string);
}, 1000);
- $this->filterchain->connect(function($context, $params, $chain) {
+ $this->filterchain->attach(function($context, $params, $chain) {
$string = isset($params['string']) ? $params['string'] : '';
return hash('md5', $string);
}, 100);
View
102 tests/Zend/EventManager/GlobalEventManagerTest.php
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 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\GlobalEventManager,
+ Zend\EventManager\EventManager;
+
+/**
+ * @category Zend
+ * @package Zend_EventManager
+ * @subpackage UnitTests
+ * @group Zend_EventManager
+ * @copyright Copyright (c) 2005-2010 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
+{
+ public function setUp()
+ {
+ GlobalEventManager::setEventCollection(null);
+ }
+
+ public function testStoresAnEventManagerInstanceByDefault()
+ {
+ $events = GlobalEventManager::getEventCollection();
+ $this->assertInstanceOf('Zend\EventManager\EventManager', $events);
+ }
+
+ public function testPassingNullValueForEventCollectionResetsInstance()
+ {
+ $events = GlobalEventManager::getEventCollection();
+ $this->assertInstanceOf('Zend\EventManager\EventManager', $events);
+ GlobalEventManager::setEventCollection(null);
+ $events2 = GlobalEventManager::getEventCollection();
+ $this->assertInstanceOf('Zend\EventManager\EventManager', $events2);
+ $this->assertNotSame($events, $events2);
+ }
+
+ public function testProxiesAllStaticOperationsToEventCollectionInstance()
+ {
+ $test = new \stdClass();
+ $handler = GlobalEventManager::attach('foo.bar', function ($e) use ($test) {
+ $test->event = $e->getName();
+ $test->target = $e->getTarget();
+ $test->params = $e->getParams();
+ return $test->params;
+ });
+ $this->assertInstanceOf('Zend\Stdlib\CallbackHandler', $handler);
+
+ GlobalEventManager::trigger('foo.bar', $this, array('foo' => 'bar'));
+ $this->assertSame($this, $test->target);
+ $this->assertEquals('foo.bar', $test->event);
+ $this->assertEquals(array('foo' => 'bar'), $test->params);
+
+ $results = GlobalEventManager::triggerUntil('foo.bar', $this, array('baz' => 'bat'), function ($r) {
+ return is_array($r);
+ });
+ $this->assertTrue($results->stopped());
+ $this->assertEquals(array('baz' => 'bat'), $test->params);
+ $this->assertEquals(array('baz' => 'bat'), $results->last());
+
+ $events = GlobalEventManager::getEvents();
+ $this->assertEquals(array('foo.bar'), $events);
+
+ $handlers = GlobalEventManager::getHandlers('foo.bar');
+ $this->assertEquals(1, count($handlers));
+ $this->assertTrue($handlers->contains($handler));
+
+ GlobalEventManager::detach($handler);
+ $events = GlobalEventManager::getEvents();
+ $this->assertEquals(array(), $events);
+
+ $handler = GlobalEventManager::attach('foo.bar', function ($e) use ($test) {
+ $test->event = $e->getEvent();
+ $test->target = $e->getTarget();
+ $test->params = $e->getParams();
+ });
+ $events = GlobalEventManager::getEvents();
+ $this->assertEquals(array('foo.bar'), $events);
+ GlobalEventManager::clearHandlers('foo.bar');
+ $events = GlobalEventManager::getEvents();
+ $this->assertEquals(array(), $events);
+ }
+}
View
48 tests/Zend/EventManager/StaticEventManagerTest.php
@@ -21,6 +21,7 @@
namespace ZendTest\EventManager;
use Zend\EventManager\StaticEventManager,
+ Zend\EventManager\EventManager,
PHPUnit_Framework_TestCase as TestCase;
/**
@@ -63,10 +64,10 @@ public function testSingletonInstanceIsInstanceOfClass()
$this->assertInstanceOf('Zend\EventManager\StaticEventManager', StaticEventManager::getInstance());
}
- public function testCanConnectCallbackToEvent()
+ public function testCanAttachCallbackToEvent()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
$this->assertContains('bar', $events->getEvents('foo'));
$expected = array($this, __FUNCTION__);
$found = false;
@@ -82,10 +83,10 @@ public function testCanConnectCallbackToEvent()
$this->assertTrue($found, 'Did not find handler!');
}
- public function testCanConnectSameEventToMultipleResourcesAtOnce()
+ public function testCanAttachSameEventToMultipleResourcesAtOnce()
{
$events = StaticEventManager::getInstance();
- $events->connect(array('foo', 'test'), 'bar', array($this, __FUNCTION__));
+ $events->attach(array('foo', 'test'), 'bar', array($this, __FUNCTION__));
$this->assertContains('bar', $events->getEvents('foo'));
$this->assertContains('bar', $events->getEvents('test'));
$expected = array($this, __FUNCTION__);
@@ -107,7 +108,7 @@ public function testCanConnectSameEventToMultipleResourcesAtOnce()
public function testCanDetachHandlerFromResource()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
foreach ($events->getHandlers('foo', 'bar') as $handler) {
// only one; retrieving it so we can detach
}
@@ -119,14 +120,14 @@ public function testCanDetachHandlerFromResource()
public function testCanGetEventsByResource()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
$this->assertEquals(array('bar'), $events->getEvents('foo'));
}
public function testCanGetHandlersByResourceAndEvent()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
$handlers = $events->getHandlers('foo', 'bar');
$this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $handlers);
$this->assertEquals(1, count($handlers));
@@ -135,8 +136,8 @@ public function testCanGetHandlersByResourceAndEvent()
public function testCanClearHandlersByResource()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
- $events->connect('foo', 'baz', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'baz', array($this, __FUNCTION__));
$events->clearHandlers('foo');
$this->assertFalse($events->getHandlers('foo', 'bar'));
$this->assertFalse($events->getHandlers('foo', 'baz'));
@@ -145,9 +146,9 @@ public function testCanClearHandlersByResource()
public function testCanClearHandlersByResourceAndEvent()
{
$events = StaticEventManager::getInstance();
- $events->connect('foo', 'bar', array($this, __FUNCTION__));
- $events->connect('foo', 'baz', array($this, __FUNCTION__));
- $events->connect('foo', 'bat', array($this, __FUNCTION__));
+ $events->attach('foo', 'bar', array($this, __FUNCTION__));
+ $events->attach('foo', 'baz', array($this, __FUNCTION__));
+ $events->attach('foo', 'bat', array($this, __FUNCTION__));
$events->clearHandlers('foo', 'baz');
$this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $events->getHandlers('foo', 'baz'));
$this->assertEquals(0, count($events->getHandlers('foo', 'baz')));
@@ -156,4 +157,27 @@ public function testCanClearHandlersByResourceAndEvent()
$this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $events->getHandlers('foo', 'bat'));
$this->assertEquals(1, count($events->getHandlers('foo', 'bat')));
}
+
+ public function testCanPassArrayOfIdentifiersToConstructor()
+ {
+ $identifiers = array('foo', 'bar');
+ $manager = new EventManager($identifiers);
+ }
+
+ public function testHandlersAttachedToAnyIdentifierProvidedToEventManagerWillBeTriggered()
+ {
+ $identifiers = array('foo', 'bar');
+ $manager = new EventManager($identifiers);
+ $events = StaticEventManager::getInstance();
+ $test = new \stdClass;
+ $test->triggered = 0;
+ $events->attach('foo', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $events->attach('bar', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $manager->trigger('bar', $this, array());
+ $this->assertEquals(2, $test->triggered);
+ }
}
View
48 tests/Zend/EventManager/StaticIntegrationTest.php
@@ -43,7 +43,7 @@ public function setUp()
public function testCanConnectStaticallyToClassWithEvents()
{
$counter = (object) array('count' => 0);
- StaticEventManager::getInstance()->connect(
+ StaticEventManager::getInstance()->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($counter) {
@@ -58,15 +58,15 @@ function ($e) use ($counter) {
public function testLocalHandlersAreExecutedPriorToStaticHandlers()
{
$test = (object) array('results' => array());
- StaticEventManager::getInstance()->connect(
+ StaticEventManager::getInstance()->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($test) {
$test->results[] = 'static';
}
);
$class = new TestAsset\ClassWithEvents();
- $class->events()->connect('foo', function ($e) use ($test) {
+ $class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local';
});
$class->foo();
@@ -76,7 +76,7 @@ function ($e) use ($test) {
public function testLocalHandlersAreExecutedPriorToStaticHandlersRegardlessOfPriority()
{
$test = (object) array('results' => array());
- StaticEventManager::getInstance()->connect(
+ StaticEventManager::getInstance()->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($test) {
@@ -85,16 +85,50 @@ function ($e) use ($test) {
10000 // high priority
);
$class = new TestAsset\ClassWithEvents();
- $class->events()->connect('foo', function ($e) use ($test) {
+ $class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local';
}, 1); // low priority
- $class->events()->connect('foo', function ($e) use ($test) {
+ $class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local2';
}, 1000); // medium priority
- $class->events()->connect('foo', function ($e) use ($test) {
+ $class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local3';
}, 15000); // highest priority
$class->foo();
$this->assertEquals(array('local3', 'local2', 'local', 'static'), $test->results);
}
+
+ public function testPassingNullValueToSetStaticConnectionsDisablesStaticConnections()
+ {
+ $counter = (object) array('count' => 0);
+ StaticEventManager::getInstance()->attach(
+ 'ZendTest\EventManager\TestAsset\ClassWithEvents',
+ 'foo',
+ function ($e) use ($counter) {
+ $counter->count++;
+ }
+ );
+ $class = new TestAsset\ClassWithEvents();
+ $class->events()->setStaticConnections(null);
+ $class->foo();
+ $this->assertEquals(0, $counter->count);
+ }
+
+ public function testCanPassAlternateStaticConnectionsHolder()
+ {
+ $counter = (object) array('count' => 0);
+ StaticEventManager::getInstance()->attach(
+ 'ZendTest\EventManager\TestAsset\ClassWithEvents',
+ 'foo',
+ function ($e) use ($counter) {
+ $counter->count++;
+ }
+ );
+ $mockStaticEvents = new TestAsset\StaticEventsMock();
+ $class = new TestAsset\ClassWithEvents();
+ $class->events()->setStaticConnections($mockStaticEvents);
+ $this->assertSame($mockStaticEvents, $class->events()->getStaticConnections());
+ $class->foo();
+ $this->assertEquals(0, $counter->count);
+ }
}
View
6 tests/Zend/EventManager/TestAsset/ClassWithEvents.php
@@ -21,7 +21,7 @@
namespace ZendTest\EventManager\TestAsset;
-use Zend\EventManager\EventDispatcher,
+use Zend\EventManager\EventCollection,
Zend\EventManager\EventManager;
/**
@@ -36,7 +36,7 @@ class ClassWithEvents
{
protected $events;
- public function events(EventDispatcher $events = null)
+ public function events(EventCollection $events = null)
{
if (null !== $events) {
$this->events = $events;
@@ -49,6 +49,6 @@ public function events(EventDispatcher $events = null)
public function foo()
{
- $this->events()->emit(__FUNCTION__, $this, array());
+ $this->events()->trigger(__FUNCTION__, $this, array());
}
}
View
8 tests/Zend/EventManager/TestAsset/MockAggregate.php
@@ -21,7 +21,7 @@
namespace ZendTest\EventManager\TestAsset;
-use Zend\EventManager\EventDispatcher,
+use Zend\EventManager\EventCollection,
Zend\EventManager\HandlerAggregate;
/**
@@ -34,10 +34,10 @@
*/
class MockAggregate implements HandlerAggregate
{
- public function connect(EventDispatcher $signals)
+ public function attach(EventCollection $events)
{
- $signals->connect('foo.bar', array( $this, 'fooBar' ));
- $signals->connect('foo.baz', array( $this, 'fooBaz' ));
+ $events->attach('foo.bar', array( $this, 'fooBar' ));
+ $events->attach('foo.baz', array( $this, 'fooBaz' ));
}
public function fooBar()
View
25 ...ary/Zend/EventManager/EventDispatcher.php → ...entManager/TestAsset/StaticEventsMock.php
@@ -14,30 +14,27 @@
*
* @category Zend
* @package Zend_EventManager
+ * @subpackage UnitTests
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
-/**
- * @namespace
- */
-namespace Zend\EventManager;
+namespace ZendTest\EventManager\TestAsset;
+
+use Zend\EventManager\StaticEventCollection;
/**
- * Interface for messengers
- *
* @category Zend
* @package Zend_EventManager
+ * @subpackage UnitTests
+ * @group Zend_EventManager