Fixing event manager/mediator

This commit is contained in:
Dennis Eichhorn 2016-02-12 10:35:19 +01:00
parent ef00da2b43
commit f4df9a6f84
2 changed files with 68 additions and 28 deletions

View File

@ -16,7 +16,6 @@
namespace phpOMS\Event; namespace phpOMS\Event;
use phpOMS\Pattern\Mediator; use phpOMS\Pattern\Mediator;
use phpOMS\Utils\ArrayUtils;
/** /**
* EventManager class. * EventManager class.
@ -29,6 +28,7 @@ use phpOMS\Utils\ArrayUtils;
*/ */
class EventManager implements Mediator class EventManager implements Mediator
{ {
const DELIM = ':';
/** /**
* Events. * Events.
@ -51,7 +51,7 @@ class EventManager implements Mediator
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function attach(string $event, \Closure $callback = null, string $listener = null) : string public function attach(string $event, \Closure $callback, string $listener) : string
{ {
$this->events[$event][$listener] = $callback; $this->events[$event][$listener] = $callback;
@ -61,17 +61,22 @@ class EventManager implements Mediator
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function trigger(string $event, \Closure $callback = null, string $source = null) : int public function trigger(string $event, string $source, \Closure $callback = null) : int
{ {
$count = 0; $count = 0;
foreach ($this->events[$event] as $event) {
$event($source); if (isset($this->events[$event])) {
$count++; foreach ($this->events[$event] as $listener) {
foreach ($listener as $closure) {
$closure($source);
$count++;
}
}
} }
if (isset($callback)) { if (isset($callback)) {
/** @var $callback Callable */ /** @var $callback Callable */
$callback(); $callback($count);
} }
return $count; return $count;
@ -82,29 +87,31 @@ class EventManager implements Mediator
* *
* An object fires an event until it's callback returns false * An object fires an event until it's callback returns false
* *
* @param string $event Event ID * @param string $event Event ID
* @param string $source What class is invoking this event
* @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks. * @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks.
* @param string $source What class is invoking this event
* *
* @return int * @return int
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function triggerUntil(string $event, \Closure $callback = null, string $source = null) : int public function triggerUntil(string $event, string $source, \Closure $callback = null) : int
{ {
$run = true; $run = true;
$count = 0; $count = 0;
do { if (isset($this->events[$event])) {
foreach ($this->events[$event] as $event) { do {
$run = $event($source); foreach ($this->events[$event] as $eventClosure) {
$count++; $run = $eventClosure($source);
} $count++;
} while ($run); }
} while ($run);
}
if ($callback !== null) { if ($callback !== null) {
$callback(); $callback($count);
} }
return $count; return $count;
@ -113,9 +120,29 @@ class EventManager implements Mediator
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function detach(int $event) public function detachListener(string $event, string $listener) : bool
{ {
$this->events = ArrayUtils::unsetArray($event, $this->events, '/'); if (isset($this->events[$event][$listener])) {
unset($this->events[$event][$listener]);
return true;
}
return false;
}
/**
* {@inheritdoc}
*/
public function detachEvent(string $event) : bool
{
if (isset($this->events[$event])) {
unset($this->events[$event]);
return true;
}
return false;
} }
/** /**

View File

@ -34,42 +34,55 @@ interface Mediator extends \Countable
* *
* Listeners will get called if a certain event gets triggered * Listeners will get called if a certain event gets triggered
* *
* @param string $event Event ID * @param string $event Event ID
* @param \Closure $callback Function to call if the event gets triggered * @param \Closure $callback Function to call if the event gets triggered
* @param string $listener What class is attaching this listener * @param string $listener What class is attaching this listener
* *
* @return string UID for the listener * @return string UID for the listener
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function attach(string $event, \Closure $callback = null, string $listener = null) : string; public function attach(string $event, \Closure $callback, string $listener) : string;
/** /**
* Removing a listener. * Removing a event.
* *
* @param int $event ID of the listener * @param string $event ID of the event
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function detach(int $event); public function detachEvent(string $event) : bool;
/**
* Removing a listener.
*
* @param string $event ID of the event
* @param string $listener ID of the listener
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function detachListener(string $event, string $listener) : bool;
/** /**
* Trigger event. * Trigger event.
* *
* An object fires an event * An object fires an event
* *
* @param string $event Event ID * @param string $event Event ID
* @param string $source What class is invoking this event
* @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks. * @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks.
* @param string $source What class is invoking this event
* *
* @return int * @return int
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function trigger(string $event, \Closure $callback = null, string $source = null) : int; public function trigger(string $event, string $source, \Closure $callback = null) : int;
} }