mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-13 15:18:41 +00:00
Allow array multiple callbacks per trigger
This commit is contained in:
parent
51894cbbb5
commit
7a7505193d
|
|
@ -14,6 +14,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace phpOMS\Event;
|
namespace phpOMS\Event;
|
||||||
|
|
||||||
|
use phpOMS\Dispatcher\Dispatcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EventManager class.
|
* EventManager class.
|
||||||
*
|
*
|
||||||
|
|
@ -42,6 +44,31 @@ final class EventManager
|
||||||
*/
|
*/
|
||||||
private $callbacks = [];
|
private $callbacks = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatcher.
|
||||||
|
*
|
||||||
|
* @var Dispatcher|Object<dispatch>
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
private $dispatcher = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param Dispatcher $dispatcher Dispatcher
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
public function __construct(Dispatcher $dispatcher = null)
|
||||||
|
{
|
||||||
|
$this->dispatcher = $dispatcher ?? new class {
|
||||||
|
function dispatch($func, array $data)
|
||||||
|
{
|
||||||
|
$func(...$data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach new event
|
* Attach new event
|
||||||
*
|
*
|
||||||
|
|
@ -56,11 +83,11 @@ final class EventManager
|
||||||
*/
|
*/
|
||||||
public function attach(string $group, $callback, bool $remove = false, bool $reset = false) : bool
|
public function attach(string $group, $callback, bool $remove = false, bool $reset = false) : bool
|
||||||
{
|
{
|
||||||
if (isset($this->callbacks[$group])) {
|
if (!isset($this->callbacks[$group])) {
|
||||||
return false;
|
$this->callbacks[$group] = ['remove' => $remove, 'reset' => $reset, 'callbacks' => []];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->callbacks[$group] = ['remove' => $remove, 'reset' => $reset, 'func' => $callback];
|
$this->callbacks[$group]['callbacks'][] = $callback;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +139,9 @@ final class EventManager
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->hasOutstanding($group)) {
|
if (!$this->hasOutstanding($group)) {
|
||||||
$this->callbacks[$group]['func']($data);
|
foreach ($this->callbacks[$group]['callbacks'] as $func) {
|
||||||
|
$this->dispatcher->disptach($func, ...$data);
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->callbacks[$group]['remove']) {
|
if ($this->callbacks[$group]['remove']) {
|
||||||
$this->detach($group);
|
$this->detach($group);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user