1<?php
2
3class Dispatcher {
4  var $_callbacks;
5
6  function Dispatcher() {
7    $this->_callbacks = array();
8  }
9
10  /**
11   * @param String $type name of the event to dispatch
12   */
13  function add_event($type) {
14    $this->_callbacks[$type] = array();
15  }
16
17  function add_observer($type, $callback) {
18    $this->_check_event_type($type);
19    $this->_callbacks[$type][] = $callback;
20  }
21
22  function fire($type, $params) {
23    $this->_check_event_type($type);
24
25    foreach ($this->_callbacks[$type] as $callback) {
26      call_user_func($callback, $params);
27    };
28  }
29
30  function _check_event_type($type) {
31    if (!isset($this->_callbacks[$type])) {
32      die(sprintf("Invalid event type: %s", $type));
33    };
34  }
35}
36
37?>