3
* Flight: An extensible micro-framework.
5
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
6
* @license MIT, http://flightphp.com/license
12
* The Dispatcher class is responsible for dispatching events. Events
13
* are simply aliases for class methods or functions. The Dispatcher
14
* allows you to hook other functions to an event that can modify the
15
* input parameters and/or the output.
23
protected $events = array();
30
protected $filters = array();
33
* Dispatches an event.
35
* @param string $name Event name
36
* @param array $params Callback parameters
37
* @return string Output of callback
39
public function run($name, array $params = array()) {
43
if (!empty($this->filters[$name]['before'])) {
44
$this->filter($this->filters[$name]['before'], $params, $output);
47
// Run requested method
48
$output = $this->execute($this->get($name), $params);
51
if (!empty($this->filters[$name]['after'])) {
52
$this->filter($this->filters[$name]['after'], $params, $output);
59
* Assigns a callback to an event.
61
* @param string $name Event name
62
* @param callback $callback Callback function
64
public function set($name, $callback) {
65
$this->events[$name] = $callback;
69
* Gets an assigned callback.
71
* @param string $name Event name
72
* @return callback $callback Callback function
74
public function get($name) {
75
return isset($this->events[$name]) ? $this->events[$name] : null;
79
* Checks if an event has been set.
81
* @param string $name Event name
82
* @return bool Event status
84
public function has($name) {
85
return isset($this->events[$name]);
89
* Clears an event. If no name is given,
90
* all events are removed.
92
* @param string $name Event name
94
public function clear($name = null) {
96
unset($this->events[$name]);
97
unset($this->filters[$name]);
100
$this->events = array();
101
$this->filters = array();
106
* Hooks a callback to an event.
108
* @param string $name Event name
109
* @param string $type Filter type
110
* @param callback $callback Callback function
112
public function hook($name, $type, $callback) {
113
$this->filters[$name][$type][] = $callback;
117
* Executes a chain of method filters.
119
* @param array $filters Chain of filters
120
* @param array $params Method parameters
121
* @param mixed $output Method output
123
public function filter($filters, &$params, &$output) {
124
$args = array(&$params, &$output);
125
foreach ($filters as $callback) {
126
$continue = $this->execute($callback, $args);
127
if ($continue === false) break;
132
* Executes a callback function.
134
* @param callback $callback Callback function
135
* @param array $params Function parameters
136
* @return mixed Function results
139
public static function execute($callback, array &$params = array()) {
140
if (is_callable($callback)) {
141
return is_array($callback) ?
142
self::invokeMethod($callback, $params) :
143
self::callFunction($callback, $params);
146
throw new \Exception('Invalid callback specified.');
153
* @param string $func Name of function to call
154
* @param array $params Function parameters
155
* @return mixed Function results
157
public static function callFunction($func, array &$params = array()) {
158
switch (count($params)) {
162
return $func($params[0]);
164
return $func($params[0], $params[1]);
166
return $func($params[0], $params[1], $params[2]);
168
return $func($params[0], $params[1], $params[2], $params[3]);
170
return $func($params[0], $params[1], $params[2], $params[3], $params[4]);
172
return call_user_func_array($func, $params);
179
* @param mixed $func Class method
180
* @param array $params Class method parameters
181
* @return mixed Function results
183
public static function invokeMethod($func, array &$params = array()) {
184
list($class, $method) = $func;
186
$instance = is_object($class);
188
switch (count($params)) {
195
$class->$method($params[0]) :
196
$class::$method($params[0]);
199
$class->$method($params[0], $params[1]) :
200
$class::$method($params[0], $params[1]);
203
$class->$method($params[0], $params[1], $params[2]) :
204
$class::$method($params[0], $params[1], $params[2]);
207
$class->$method($params[0], $params[1], $params[2], $params[3]) :
208
$class::$method($params[0], $params[1], $params[2], $params[3]);
211
$class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :
212
$class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
214
return call_user_func_array($func, $params);
219
* Resets the object to the initial state.
221
public function reset() {
222
$this->events = array();
223
$this->filters = array();