Commits

Thomas Weinert committed 20e9b1a

Implemented: emit events without implicit create of the event emitter subobjects (avoid to create the emitter if no callbacks are attached)

  • Participants
  • Parent commits 99a7361

Comments (0)

Files changed (2)

src/Carica/Io/Event/Emitter/Aggregation.php

       }
       return $this->_eventEmitter;
     }
+
+    /**
+     * Avoid to create the emitter object just for emitting, without any callbacks attached
+     *
+     * @param $event
+     */
+    public function emitEvent($event) {
+      if (isset($this->_eventEmitter) && !empty($event)) {
+        call_user_func_array(array($this->_eventEmitter, 'emit'), func_get_args());
+      }
+    }
   }
 }

tests/Carica/Io/Event/Emitter/AggregationTest.php

       $this->assertInstanceOf('Carica\Io\Event\Emitter', $aggregation->events());
     }
 
+    /**
+     * @covers Carica\Io\Event\Emitter\Aggregation::emitEvent
+     */
+    public function testEmitEvent() {
+      $aggregation = new Aggregation_TestProxy();
+      $result = FALSE;
+      $aggregation->events()->on('test', function() use (&$result) { $result = TRUE; });
+      $aggregation->emitEvent('test');
+      $this->assertTrue($result);
+    }
+
+    /**
+     * @covers Carica\Io\Event\Emitter\Aggregation::emitEvent
+     */
+    public function testEmitEventDoesNotImplicitCreateEmitter() {
+      $aggregation = new Aggregation_TestProxy();
+      $aggregation->emitEvent('dummy');
+      $this->assertAttributeSame(NULL, '_eventEmitter', $aggregation);
+    }
+
   }
 
   class Aggregation_TestProxy {