Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Core: Event: Add method register_list().

Which is dramatically faster than multiple `register()` calls.
  • Loading branch information...
commit e634244343c2258a50c172a591f6de0da998bd00 1 parent 92c19ed
@Seza Seza authored
Showing with 181 additions and 1 deletion.
  1. +26 −0 app/core/Event.php
  2. +155 −1 test/app/core/EventTest.php
View
26 app/core/Event.php
@@ -56,6 +56,8 @@ public static function create(array $data = array())
* thrown (see: signal()), the callback method is called with the event
* class argument.
*
+ * Note: If you want register two or more listener, see `register_list` method.
+ *
* @param string $name The name of the event want to listen.
* @param callback $callback The callback method to call when the event is thrown
* @param int $priotiy The default priority is set to 100, less is more prior
@@ -73,6 +75,30 @@ public static function register($name, $callback, $priority = 100)
}
/**
+ * Register a list of callback method to events name. See `register()` method
+ * The list is an array where values are `event`, `callback` and optionnaly
+ * `priority`.
+ *
+ * Note: If you want register two or more listener, this method is the fastest one.
+ *
+ * @param array $event The name of the event want to listen.
+ */
+ public static function register_list(array $listeners)
+ {
+ foreach ($listeners as $l)
+ {
+ list($name, $callback, $priority) = ($l + array(2 => 100));
+
+ isset(static::$_listen[$name]) or static::$_listen[$name] = array();
+
+ while (isset(static::$_listen[$name][$priority])) ++$priority;
+
+ static::$_listen[$name][$priority] = $callback;
+ static::$_ordered[$name] = false;
+ }
+ }
+
+ /**
* Throw an event. Return the event is thrown which can be contain response
* content or modified content.
* By default if the event have at least one listener, it's state is set to
View
156 test/app/core/EventTest.php
@@ -200,6 +200,161 @@ public function test_register_and_signal_priority()
}
/**
+ * @covers pixelpost\core\Event::register_list
+ * @covers pixelpost\core\Event::signal
+ */
+ public function test_register_list_and_signal()
+ {
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ $eventName = 'my-unit-test-event-11';
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertFalse($event->is_processed());
+ $this->assertFalse(property_exists($event, 'unit_test'));
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ Event::register_list(array(
+ array($eventName, __CLASS__ . '::on_event1')
+ ));
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertTrue($event->is_processed());
+ $this->assertTrue(property_exists($event, 'unit_test'));
+ $this->assertTrue($event->unit_test);
+ $this->assertTrue(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+ }
+
+ /**
+ * @covers pixelpost\core\Event::register_list
+ * @covers pixelpost\core\Event::signal
+ */
+ public function test_register_list_and_signal_normal_order_call()
+ {
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ $eventName = 'my-unit-test-event-12';
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertFalse($event->is_processed());
+ $this->assertFalse(property_exists($event, 'unit_test'));
+
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ Event::register_list(array(
+ array($eventName, __CLASS__ . '::on_event1'),
+ array($eventName, __CLASS__ . '::on_event2')
+ ));
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertTrue($event->is_processed());
+ $this->assertTrue(property_exists($event, 'unit_test'));
+ $this->assertSame('bar', $event->unit_test);
+
+ $this->assertTrue(self::$event1Called);
+ $this->assertTrue(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+ }
+
+ /**
+ * @covers pixelpost\core\Event::register_list
+ * @covers pixelpost\core\Event::signal
+ */
+ public function test_register_list_and_signal_event_break_call_chain()
+ {
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ $eventName = 'my-unit-test-event-13';
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertFalse($event->is_processed());
+ $this->assertFalse(property_exists($event, 'unit_test'));
+
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ Event::register_list(array(
+ array($eventName, __CLASS__ . '::on_event1'),
+ array($eventName, __CLASS__ . '::on_event3'),
+ array($eventName, __CLASS__ . '::on_event2')
+ ));
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertTrue($event->is_processed());
+ $this->assertTrue(property_exists($event, 'unit_test'));
+ $this->assertSame('foo', $event->unit_test);
+
+ $this->assertTrue(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertTrue(self::$event3Called);
+ }
+
+ /**
+ * @covers pixelpost\core\Event::register_list
+ * @covers pixelpost\core\Event::signal
+ */
+ public function test_register_list_and_signal_priority()
+ {
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ $eventName = 'my-unit-test-event-14';
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertFalse($event->is_processed());
+ $this->assertFalse(property_exists($event, 'unit_test'));
+
+ $this->assertFalse(self::$event1Called);
+ $this->assertFalse(self::$event2Called);
+ $this->assertFalse(self::$event3Called);
+
+ Event::register_list(array(
+ array($eventName, __CLASS__ . '::on_event1', 120),
+ array($eventName, __CLASS__ . '::on_event2', 5),
+ array($eventName, __CLASS__ . '::on_event3', 120)
+ ));
+
+ $event = Event::signal($eventName);
+
+ $this->assertTrue($event instanceof Event);
+ $this->assertTrue($event->is_processed());
+ $this->assertTrue(property_exists($event, 'unit_test'));
+ $this->assertSame('foo', $event->unit_test);
+
+ $this->assertTrue(self::$event1Called);
+ $this->assertTrue(self::$event2Called);
+ $this->assertTrue(self::$event3Called);
+ }
+
+ /**
* @covers pixelpost\core\Event::register
* @covers pixelpost\core\Event::signal
*/
@@ -233,5 +388,4 @@ public function test_set_and_is_processed()
$event->set_processed(true);
$this->assertTrue($event->is_processed());
}
-
}
Please sign in to comment.
Something went wrong with that request. Please try again.