$event) { [$handler] = $event; if ($handler !== $callback) { continue; } unset(static::$_events[$name][$index]); } } /** * @param $name */ public static function offName($name): void { unset(static::$_events[$name]); } /** * @param $name * @param null $callback * @return bool */ public static function exists($name, $callback): bool { if ($callback instanceof \Closure || !isset(static::$_events[$name])) { return false; } foreach (static::$_events[$name] as $event) { [$handler] = $event; if ($handler === $callback) { return true; } } return false; } /** * @param $name * @param $handler * @return mixed */ public static function get($name, $handler): mixed { if (!static::exists($name, $handler)) { return null; } if (empty($handler)) { return static::$_events[$name]; } foreach (static::$_events[$name] as $event) { [$callback] = $event; if ($callback === $handler) { return [$event]; } } return null; } public static function clean() { static::$_events = []; } /** * @param $name * @param array $params * @return bool * @throws Exception */ public function dispatch($name, array $params = []): bool { return static::trigger($name, $params); } /** * @param $name * @param null $parameter * @param false $is_remove * @return bool * @throws Exception */ public static function trigger($name, $parameter = null, bool $is_remove = false): bool { foreach ((static::$_events[$name] ?? []) as $key => $event) { static::execute($event, $parameter); if ($event instanceof \Closure) { unset(static::$_events[$name][$key]); } } if ($is_remove) { unset(static::$_events[$name]); } return true; } /** * @param $event * @param $parameter * @return void * @throws Exception */ private static function execute($event, $parameter): void { try { call_user_func($event[0], ...$parameter); } catch (\Throwable $throwable) { logger()->addError($throwable, 'throwable'); return; } } }