Browse Source

API improvements for the event stack and tests.

Florian Krämer 9 years ago
parent
commit
eea8e82e5f

+ 29 - 5
src/Event/EventManager.php

@@ -61,6 +61,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
      * other managers were created. Usually for creating hook systems or inter-class
@@ -353,7 +360,9 @@ class EventManager
 
         $listeners = $this->listeners($event->name());
         if (empty($listeners)) {
-            $this->_stackEvent($event);
+            if ($this->_stackEvents) {
+                $this->stackEvent($event);
+            }
             return $event;
         }
 
@@ -370,7 +379,9 @@ class EventManager
             }
         }
 
-        $this->_stackEvent($event);
+        if ($this->_stackEvents) {
+            $this->stackEvent($event);
+        }
         return $event;
     }
 
@@ -486,7 +497,7 @@ class EventManager
      * @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);
@@ -494,14 +505,26 @@ class EventManager
     }
 
     /**
+     * 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;
     }
 
     /**
@@ -509,9 +532,10 @@ class EventManager
      *
      * @return void
      */
-    public function disableEventStacking()
+    public function detachEventStack()
     {
         $this->_eventStack = null;
+        $this->_stackEvents = false;
     }
 
     /**

+ 17 - 1
src/Event/EventStack.php

@@ -53,7 +53,7 @@ class EventStack implements \ArrayAccess, \Countable
      *
      * @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 @@ class EventStack implements \ArrayAccess, \Countable
     {
         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;
+    }
 }

+ 2 - 2
tests/TestCase/Event/EventManagerTest.php

@@ -726,7 +726,7 @@ class EventManagerTest extends TestCase
         $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 @@ class EventManagerTest extends TestCase
         $result = $manager->eventStack();
         $this->assertCount(0, $result);
 
-        $manager->disableEventStacking();
+        $manager->detachEventStack();
         $manager->dispatch($event);
         $manager->dispatch($event2);
 

+ 3 - 0
tests/TestCase/Event/EventStackTest.php

@@ -62,6 +62,9 @@ class EvenStackTest extends TestCase
         $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));