Skip to content
Permalink
Browse files

API improvements for the event stack and tests.

  • Loading branch information...
burzum committed May 30, 2016
1 parent 390d7fb commit eea8e82e5f413193fa771f0ee2dfcacdf76805ea
@@ -60,6 +60,13 @@ class EventManager
*/
protected $_eventStack;
/**
* Enables automatic adding of events to the event stack object if it is present.
*
* @param bool
*/
protected $_stackEvents = false;
/**
* Returns the globally available instance of a Cake\Event\EventManager
* this is used for dispatching events attached from outside the scope
@@ -353,7 +360,9 @@ public function dispatch($event)
$listeners = $this->listeners($event->name());
if (empty($listeners)) {
$this->_stackEvent($event);
if ($this->_stackEvents) {
$this->stackEvent($event);
}
return $event;
}
@@ -370,7 +379,9 @@ public function dispatch($event)
}
}
$this->_stackEvent($event);
if ($this->_stackEvents) {
$this->stackEvent($event);
}
return $event;
}
@@ -486,32 +497,45 @@ public function eventStack()
* @param \Cake\Event\Event $event An event to add to the stack.
* @return void
*/
public function _stackEvent(Event $event)
public function stackEvent(Event $event)
{
if ($this->_eventStack) {
$this->_eventStack->add($event);
}
}
/**
* Enables / disables event stacking at runtime.
*
* @param bool $enabled True or false to enable / disable it.
* @return void
*/
public function stackEvents($enabled)
{
$this->_stackEvents = (bool)$enabled;
}
/**
* Enables the stacking of dispatched events.
*
* @param \Cake\Event\EventStack $eventStack The event stack object to use.
* @return void
*/
public function enableEventStacking(EventStack $eventStack)
public function attachEventStack(EventStack $eventStack)
{
$this->_eventStack = $eventStack;
$this->_stackEvents = true;
}
/**
* Disables the stacking of dispatched events.
*
* @return void
*/
public function disableEventStacking()
public function detachEventStack()
{
$this->_eventStack = null;
$this->_stackEvents = false;
}
/**
@@ -53,7 +53,7 @@ public function add(Event $event)
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset An offset to check for.
* @return boolean true on success or false on failure.
* @return boole True on success or false on failure.
*/
public function offsetExists($offset)
{
@@ -110,4 +110,20 @@ public function count()
{
return count($this->_events);
}
/**
* Checks if an event is in the stack.
*
* @param string $name Event name.
* @return bool
*/
public function hasEvent($name)
{
foreach ($this->_events as $event) {
if ($event->name() === $name) {
return true;
}
}
return false;
}
}
@@ -726,7 +726,7 @@ public function testGetDispatchedEvents()
$event2 = new Event('my_second_event', $this);
$manager = new EventManager();
$manager->enableEventStacking($eventStack);
$manager->attachEventStack($eventStack);
$manager->dispatch($event);
$manager->dispatch($event2);
@@ -739,7 +739,7 @@ public function testGetDispatchedEvents()
$result = $manager->eventStack();
$this->assertCount(0, $result);
$manager->disableEventStacking();
$manager->detachEventStack();
$manager->dispatch($event);
$manager->dispatch($event2);
@@ -62,6 +62,9 @@ public function testArrayAccess()
$eventStack->add($event2);
$this->assertCount(2, $eventStack);
$this->assertTrue($eventStack->hasEvent('my_event'));
$this->assertFalse($eventStack->hasEvent('does-not-exist'));
$this->assertEquals($eventStack->offsetGet(0), $event);
$this->assertEquals($eventStack->offsetGet(1), $event2);
$this->assertTrue($eventStack->offsetExists(0));

0 comments on commit eea8e82

Please sign in to comment.
You can’t perform that action at this time.