mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-10 05:58:42 +00:00
Fixes #39
This commit is contained in:
parent
85d43c57bd
commit
c2905ecf92
|
|
@ -18,7 +18,7 @@ namespace phpOMS\Event;
|
||||||
use phpOMS\Pattern\Mediator;
|
use phpOMS\Pattern\Mediator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatcher class.
|
* EventManager class.
|
||||||
*
|
*
|
||||||
* @category Framework
|
* @category Framework
|
||||||
* @package phpOMS\Event
|
* @package phpOMS\Event
|
||||||
|
|
@ -40,7 +40,15 @@ class EventManager implements Mediator
|
||||||
* @var array
|
* @var array
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
private $events = [];
|
private $groups = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callbacks.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private $callbacks = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
|
@ -55,102 +63,60 @@ class EventManager implements Mediator
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function attach(string $event, \Closure $callback, string $listener) : string
|
public function attach(string $group, \Closure $callback, bool $remove = false)
|
||||||
{
|
{
|
||||||
$this->events[$event][$listener] = $callback;
|
$this->callbacks[$group] = ['remove' => $remove, 'func' => $callback];
|
||||||
|
|
||||||
return $event . '/' . $listener;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function trigger(string $event, string $source, \Closure $callback = null) : int
|
public function detach(string $group)
|
||||||
{
|
{
|
||||||
$count = 0;
|
unset($this->callbacks[$group]);
|
||||||
|
unset($this->groups[$group]);
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($this->events[$event])) {
|
/**
|
||||||
foreach ($this->events[$event] as $listener) {
|
* {@inheritdoc}
|
||||||
foreach ($listener as $closure) {
|
*/
|
||||||
$closure($source);
|
public function trigger(string $id, string $group)
|
||||||
$count++;
|
{
|
||||||
}
|
if(isset($this->groups[$group])) {
|
||||||
|
unset($this->groups[$group][$id]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->hasOutstanding($group)) {
|
||||||
|
$this->callbacks[$group]['func'];
|
||||||
|
|
||||||
|
if($this->callbacks[$group]['remove']) {
|
||||||
|
$this->detach($group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($callback)) {
|
|
||||||
/** @var $callback Callable */
|
|
||||||
$callback($count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Trigger event.
|
|
||||||
*
|
|
||||||
* An object fires an event until it's callback returns false
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
||||||
*/
|
|
||||||
public function triggerUntil(string $event, string $source, \Closure $callback = null) : int
|
|
||||||
{
|
|
||||||
$run = true;
|
|
||||||
$count = 0;
|
|
||||||
|
|
||||||
if (isset($this->events[$event])) {
|
|
||||||
do {
|
|
||||||
foreach ($this->events[$event] as $eventClosure) {
|
|
||||||
$run = $eventClosure($source);
|
|
||||||
$count++;
|
|
||||||
}
|
|
||||||
} while ($run);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($callback !== null) {
|
|
||||||
$callback($count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function detachListener(string $event, string $listener) : bool
|
private function hasOutstanding(string $group) : bool
|
||||||
{
|
{
|
||||||
if (isset($this->events[$event][$listener])) {
|
return empty($this->groups[$group]);
|
||||||
unset($this->events[$event][$listener]);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function detachEvent(string $event) : bool
|
public function addGroup(string $id, string $group)
|
||||||
{
|
{
|
||||||
if (isset($this->events[$event])) {
|
if(!isset($this->groups[$group])) {
|
||||||
unset($this->events[$event]);
|
$this->groups[$group] = [];
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
$this->groups[$group][$id] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count event listenings.
|
* Count events.
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*
|
*
|
||||||
|
|
@ -159,7 +125,7 @@ class EventManager implements Mediator
|
||||||
*/
|
*/
|
||||||
public function count() : int
|
public function count() : int
|
||||||
{
|
{
|
||||||
return count($this->events);
|
return count($this->callbacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,55 +34,68 @@ 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 $group Group
|
||||||
* @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 bool $remove Remove event after execution
|
||||||
*
|
*
|
||||||
* @return string UID for the listener
|
* @return void
|
||||||
*
|
*
|
||||||
* @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, string $listener) : string;
|
public function attach(string $group, \Closure $callback, bool $remove = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removing a event.
|
* Removing a event.
|
||||||
*
|
*
|
||||||
* @param string $event ID of the event
|
* @param string $group Group
|
||||||
*
|
*
|
||||||
* @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 detachEvent(string $event) : bool;
|
public function detach(string $group) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removing a listener.
|
* Has outstanding.
|
||||||
*
|
*
|
||||||
* @param string $event ID of the event
|
* @param string $group Group
|
||||||
* @param string $listener ID of the listener
|
|
||||||
*
|
*
|
||||||
* @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 detachListener(string $event, string $listener) : bool;
|
private function hasOutstanding(string $group) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add group.
|
||||||
|
*
|
||||||
|
* Add new element to group
|
||||||
|
*
|
||||||
|
* @param string $id Event ID
|
||||||
|
* @param string $group Group
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
|
public function addGroup(string $id, string $group);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trigger event.
|
* Trigger event.
|
||||||
*
|
*
|
||||||
* An object fires an event
|
* An object fires an event
|
||||||
*
|
*
|
||||||
* @param string $event Event ID
|
* @param string $id Event ID
|
||||||
* @param string $source What class is invoking this event
|
* @param string $group Group
|
||||||
* @param \Closure $callback Callback function of the event. This will get triggered after firering all listener callbacks.
|
|
||||||
*
|
*
|
||||||
* @return int
|
* @return void
|
||||||
*
|
*
|
||||||
* @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, string $source, \Closure $callback = null) : int;
|
public function trigger(string $id, string $group);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user