diff --git a/SEOstats/Helper/Event.php b/SEOstats/Helper/Event.php new file mode 100644 index 00000000..ba47e4c5 --- /dev/null +++ b/SEOstats/Helper/Event.php @@ -0,0 +1,61 @@ + + * @license http://eyecatchup.mit-license.org/ MIT License + */ +class Event +{ + const STATUS_UNTRIGGERED = 'untriggered'; + const STATUS_CANCELLED = 'cancel'; + const STATUS_DONE = 'done'; + const STATUS_PROGRESS = 'progress'; + + protected $data = array(); + protected $status = self::STATUS_UNTRIGGERED; + protected $exception = null; + + public function cancel() + { + $this->status = self::STATUS_CANCELLED; + } + + public function isInProgress() + { + return ($this->status === self::STATUS_PROGRESS); + } + + public function startProgress() + { + $this->status = self::STATUS_PROGRESS; + } + + public function setException($exception) + { + $this->exception = $exception; + } + + public function getException() + { + return $this->exception; + } + + public function setProp($key, $value) + { + $this->data[$key] = $value; + } + + public function getProp($key) + { + return $this->data[$key]; + } + + public function stopProgress() + { + $this->status = self::STATUS_DONE; + } +} diff --git a/SEOstats/Helper/EventManager.php b/SEOstats/Helper/EventManager.php new file mode 100644 index 00000000..c339dc63 --- /dev/null +++ b/SEOstats/Helper/EventManager.php @@ -0,0 +1,83 @@ + + * @license http://eyecatchup.mit-license.org/ MIT License + */ +class EventManager +{ + protected $listeners = array(); + protected $instance = null; + + protected function __construct() + { + } + + public static function getInstance() + { + if (static::$instance === null) { + static::$instance = new static(); + } + + return static::$instance; + } + + public function add($eventKey, $callback) + { + $cEventKey = $this->canonicalizeName($eventKey); + + $this->listeners[$cEventKey][] = $callback; + } + + public function canonicalizeName($name) + { + $replace = array('-' => '', '_' => '', ' ' => '', '\\' => '', '/' => ''); + + return strtolower(strtr($name, $replace)); + } + + public function remove($eventKey, $callback = null) + { + $cEventKey = $this->canonicalizeName($eventKey); + + if (is_callable($callback)) { + // remove only one callback + $this->listeners[$cEventKey] = array_diff($this->listeners[$cEventKey], array($callback)); + } elseif (is_array($callback)) { + // remove only more callback's + $this->listeners[$cEventKey] = array_diff($this->listeners[$cEventKey], $callback); + } else { + // remove all callback's + $this->listeners[$cEventKey] = array(); + } + } + + public function trigger($eventKey, Event $event) + { + $cEventKey = $this->canonicalizeName($eventKey); + + $event->startProgress(); + + foreach ($this->listeners[$eventKey] as $listener) { + if (!$event->isInProgress()) { + return false; + } + + try { + $result = call_user_func_array($listener, array($event)); + + if (!$result) { + $event->cancel(); + } + } catch (\Exception $e) { + $event->cancel(); + } + } + + $event->stopProgress(); + } +}