Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Feature/Eventmanager #86

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions SEOstats/Helper/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace SEOstats\Helper;

/**
* Event
*
* @package SEOstats
* @author Clemens Sahs <clemens.sahs@slides-worker.org>
* @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;
}
}
83 changes: 83 additions & 0 deletions SEOstats/Helper/EventManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace SEOstats\Helper;

/**
* EventManager
*
* @package SEOstats
* @author Clemens Sahs <clemens.sahs@slides-worker.org>
* @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();
}
}