From 5fc972ac84e3503ee4e1ca6db434120a37711b19 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 24 Jul 2014 12:07:14 +0200 Subject: [PATCH 1/4] write down a simple event manager --- SEOstats/Helper/Event.php | 61 ++++++++++++++++++++++++++++ SEOstats/Helper/EventManager.php | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 SEOstats/Helper/Event.php create mode 100644 SEOstats/Helper/EventManager.php 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..0d7d0c99 --- /dev/null +++ b/SEOstats/Helper/EventManager.php @@ -0,0 +1,70 @@ + + * @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 static function add($eventKey, $callback) + { + $this->listeners[$eventKey][] = $callback; + } + + public static function remove($eventKey, $callback = null) + { + if (is_callable($callback)) { + // remove only one callback + $this->listeners[$eventKey] = array_diff($this->listeners[$eventKey], array($callback)); + } elseif (is_array($callback)) { + // remove only more callback's + $this->listeners[$eventKey] = array_diff($this->listeners[$eventKey], $callback); + } else { + // remove all callback's + $this->listeners[$eventKey] = array(); + } + } + + public static function trigger($eventKey, Event $event) + { + $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(); + } +} From b93455bd22b62d47b0f2bef004824839e4659ecf Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 24 Jul 2014 12:20:42 +0200 Subject: [PATCH 2/4] remove static method --- SEOstats/Helper/EventManager.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SEOstats/Helper/EventManager.php b/SEOstats/Helper/EventManager.php index 0d7d0c99..76536963 100644 --- a/SEOstats/Helper/EventManager.php +++ b/SEOstats/Helper/EventManager.php @@ -26,12 +26,12 @@ public static function getInstance() return static::$instance; } - public static function add($eventKey, $callback) + public function add($eventKey, $callback) { $this->listeners[$eventKey][] = $callback; } - public static function remove($eventKey, $callback = null) + public function remove($eventKey, $callback = null) { if (is_callable($callback)) { // remove only one callback @@ -45,7 +45,7 @@ public static function remove($eventKey, $callback = null) } } - public static function trigger($eventKey, Event $event) + public function trigger($eventKey, Event $event) { $event->startProgress(); From 642b8724ffeca44ad81f6359d465bdbd9cfb6243 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 24 Jul 2014 12:21:29 +0200 Subject: [PATCH 3/4] add canonicalize method --- SEOstats/Helper/EventManager.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SEOstats/Helper/EventManager.php b/SEOstats/Helper/EventManager.php index 76536963..697800e2 100644 --- a/SEOstats/Helper/EventManager.php +++ b/SEOstats/Helper/EventManager.php @@ -31,6 +31,13 @@ public function add($eventKey, $callback) $this->listeners[$eventKey][] = $callback; } + public function canonicalizeName($name) + { + $replace = array('-' => '', '_' => '', ' ' => '', '\\' => '', '/' => ''); + + return strtolower(strtr($name, $replace)); + } + public function remove($eventKey, $callback = null) { if (is_callable($callback)) { From 0a51fab373ef6e28fd870313687f2237db4688a9 Mon Sep 17 00:00:00 2001 From: Clemens Sahs Date: Thu, 24 Jul 2014 12:21:52 +0200 Subject: [PATCH 4/4] use canonicalize method --- SEOstats/Helper/EventManager.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/SEOstats/Helper/EventManager.php b/SEOstats/Helper/EventManager.php index 697800e2..c339dc63 100644 --- a/SEOstats/Helper/EventManager.php +++ b/SEOstats/Helper/EventManager.php @@ -28,7 +28,9 @@ public static function getInstance() public function add($eventKey, $callback) { - $this->listeners[$eventKey][] = $callback; + $cEventKey = $this->canonicalizeName($eventKey); + + $this->listeners[$cEventKey][] = $callback; } public function canonicalizeName($name) @@ -40,20 +42,24 @@ public function canonicalizeName($name) public function remove($eventKey, $callback = null) { + $cEventKey = $this->canonicalizeName($eventKey); + if (is_callable($callback)) { // remove only one callback - $this->listeners[$eventKey] = array_diff($this->listeners[$eventKey], array($callback)); + $this->listeners[$cEventKey] = array_diff($this->listeners[$cEventKey], array($callback)); } elseif (is_array($callback)) { // remove only more callback's - $this->listeners[$eventKey] = array_diff($this->listeners[$eventKey], $callback); + $this->listeners[$cEventKey] = array_diff($this->listeners[$cEventKey], $callback); } else { // remove all callback's - $this->listeners[$eventKey] = array(); + $this->listeners[$cEventKey] = array(); } } public function trigger($eventKey, Event $event) { + $cEventKey = $this->canonicalizeName($eventKey); + $event->startProgress(); foreach ($this->listeners[$eventKey] as $listener) {