Skip to content

Commit

Permalink
Disable event stacking by default.
Browse files Browse the repository at this point in the history
This needs to be explicitly enable because otherwise it could cause memory issues for people without that they'll know the cause.
  • Loading branch information
Florian Krämer committed May 20, 2016
1 parent 5dbb93a commit 1a36c88
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/Event/EventManager.php
Expand Up @@ -60,6 +60,13 @@ class EventManager
*/
protected $_dispatchedEvents = [];

/**
* Enables or disables the stacking of dispatched events
*
* @var bool
*/
protected $_eventStacking = false;

/**
* Returns the globally available instance of a Cake\Event\EventManager
* this is used for dispatching events attached from outside the scope
Expand Down Expand Up @@ -353,7 +360,7 @@ public function dispatch($event)

$listeners = $this->listeners($event->name());
if (empty($listeners)) {
$this->_dispatchedEvents[] = $event;
$this->stackEvent($event);
return $event;
}

Expand All @@ -369,7 +376,8 @@ public function dispatch($event)
$event->result = $result;
}
}
$this->_dispatchedEvents[] = $event;

$this->stackEvent($event);
return $event;
}

Expand Down Expand Up @@ -479,6 +487,50 @@ public function getDispatchedEvents()
return $this->_dispatchedEvents;
}

/**
* Enables the stacking of dispatched events.
*
* @return void
*/
public function enableEventStacking()
{
$this->_eventStacking = true;
}

/**
* Disables the stacking of dispatched events.
*
* @return void
*/
public function disableEventStacking()
{
$this->_eventStacking = false;
$this->flushEventStack();
}

/**
* Empties the stack of dispatched events.
*
* @return void
*/
public function flushEventStack()
{
$this->_dispatchedEvents = [];
}

/**
* Adds an event to the stack when stacking is enabled.
*
* @param \Cake\Event\Event An event to stack.
* @return void
*/
protected function stackEvent(Event $event)
{
if ($this->_eventStacking === true) {
$this->_dispatchedEvents[] = $event;
}
}

/**
* Debug friendly object properties.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Event/EventManagerTest.php
Expand Up @@ -724,12 +724,26 @@ public function testGetDispatchedEvents()
$event2 = new Event('my_second_event', $this);

$manager = new EventManager();
$manager->enableEventStacking();
$manager->dispatch($event);
$manager->dispatch($event2);

$result = $manager->getDispatchedEvents();
$this->assertCount(2, $result);
$this->assertEquals($result[0], $event);
$this->assertEquals($result[1], $event2);

$manager->flushEventStack();
$result = $manager->getDispatchedEvents();
$this->assertCount(0, $result);
$this->assertEquals($result, []);

$manager->disableEventStacking();
$manager->dispatch($event);
$manager->dispatch($event2);

$result = $manager->getDispatchedEvents();
$this->assertCount(0, $result);
$this->assertEquals($result, []);
}
}

0 comments on commit 1a36c88

Please sign in to comment.