10 defined(
'JPATH_PLATFORM') or die;
32 protected $_observers = array();
40 protected $_state = null;
48 protected $_methods = array();
56 protected static $instance = null;
66 public static function getInstance()
68 if (self::$instance === null)
70 self::$instance =
new static;
73 return self::$instance;
83 public function getState()
99 public function register($event, $handler)
102 if (is_callable($handler))
105 $method = array(
'event' => $event,
'handler' => $handler);
106 $this->attach($method);
108 elseif (class_exists($handler))
111 $this->attach(
new $handler($this));
115 throw new InvalidArgumentException(
'Invalid event handler.');
130 public function trigger($event, $args = array())
138 $args = (array) $args;
140 $event = strtolower($event);
143 if (!isset($this->_methods[$event]) || empty($this->_methods[$event]))
150 foreach ($this->_methods[$event] as $key)
153 if (!isset($this->_observers[$key]))
159 if (is_object($this->_observers[$key]))
161 $args[
'event'] = $event;
162 $value = $this->_observers[$key]->update($args);
165 elseif (is_array($this->_observers[$key]))
167 $value = call_user_func_array($this->_observers[$key][
'handler'], $args);
188 public function attach($observer)
190 if (is_array($observer))
192 if (!isset($observer[
'handler']) || !isset($observer[
'event']) || !is_callable($observer[
'handler']))
198 foreach ($this->_observers as $check)
200 if (is_array($check) && $check[
'event'] == $observer[
'event'] && $check[
'handler'] == $observer[
'handler'])
206 $this->_observers[] = $observer;
207 end($this->_observers);
208 $methods = array($observer[
'event']);
212 if (!($observer instanceof
JEvent))
218 $class = get_class($observer);
220 foreach ($this->_observers as $check)
222 if ($check instanceof $class)
228 $this->_observers[] = $observer;
229 $methods = array_diff(get_class_methods($observer), get_class_methods(
'JPlugin'));
232 $key = key($this->_observers);
234 foreach ($methods as $method)
236 $method = strtolower($method);
238 if (!isset($this->_methods[$method]))
240 $this->_methods[$method] = array();
243 $this->_methods[$method][] = $key;
256 public function detach($observer)
260 $key = array_search($observer, $this->_observers);
264 unset($this->_observers[$key]);
267 foreach ($this->_methods as &$method)
269 $k = array_search($key, $method);