From 0b2523ba9f9f218aa4823301e26504fdecb20f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Apr 2021 15:57:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Annotation/Event.php | 11 ++- Annotation/LocalService.php | 3 +- Database/Connection.php | 10 +-- Database/DatabasesProviders.php | 3 +- Database/Db.php | 11 ++- Database/databases.php | 66 ---------------- HttpServer/Events/OnAfterReload.php | 4 +- HttpServer/Events/OnBeforeReload.php | 3 +- HttpServer/Events/OnClose.php | 6 +- HttpServer/Events/OnMessage.php | 6 +- HttpServer/Events/OnRequest.php | 3 - HttpServer/Events/OnWorkerError.php | 3 +- HttpServer/Events/OnWorkerExit.php | 3 +- HttpServer/Events/OnWorkerStop.php | 3 +- HttpServer/Server.php | 4 +- HttpServer/config.php | 111 --------------------------- System/Abstracts/BaseApplication.php | 42 ++++------ System/Cache/Redis.php | 7 +- System/Error/ErrorHandler.php | 6 +- System/Error/Logger.php | 5 +- System/Event.php | 40 +++++----- function.php | 10 +-- 22 files changed, 73 insertions(+), 287 deletions(-) delete mode 100644 Database/databases.php delete mode 100644 HttpServer/config.php diff --git a/Annotation/Event.php b/Annotation/Event.php index 9875abdb..6e40c0f8 100644 --- a/Annotation/Event.php +++ b/Annotation/Event.php @@ -7,6 +7,7 @@ namespace Annotation; use Exception; use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; +use Snowflake\Event as SEvent; /** @@ -29,16 +30,14 @@ use Snowflake\Snowflake; /** * @param array $handler - * @return \Snowflake\Event - * @throws ComponentException + * @return bool * @throws Exception */ - public function execute(array $handler): \Snowflake\Event + public function execute(array $handler): bool { // TODO: Implement execute() method. - $event = Snowflake::app()->getEvent(); - $event->on($this->name, $handler, $this->params); - return $event; + SEvent::on($this->name, $handler, $this->params); + return true; } } diff --git a/Annotation/LocalService.php b/Annotation/LocalService.php index dfa70673..3ad630af 100644 --- a/Annotation/LocalService.php +++ b/Annotation/LocalService.php @@ -28,8 +28,7 @@ use Snowflake\Snowflake; if ($this->async_reload !== true) { return; } - $event = Snowflake::app()->getEvent(); - $event->on(Event::SERVER_WORKER_EXIT, function () { + Event::on(Event::SERVER_WORKER_EXIT, function () { Snowflake::app()->remove($this->service); }); } diff --git a/Database/Connection.php b/Database/Connection.php index 633821ab..532831cf 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -67,9 +67,8 @@ class Connection extends Component */ public function init() { - $event = Snowflake::app()->getEvent(); - $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); - $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); } @@ -83,9 +82,8 @@ class Connection extends Component } $this->beginTransaction(); - $event = Snowflake::app()->getEvent(); - $event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); - $event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); + Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); + Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); } /** diff --git a/Database/DatabasesProviders.php b/Database/DatabasesProviders.php index 95fd686c..9879b2a7 100644 --- a/Database/DatabasesProviders.php +++ b/Database/DatabasesProviders.php @@ -33,8 +33,7 @@ class DatabasesProviders extends Providers { $application->set('db', $this); - $event = Snowflake::app()->getEvent(); - $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); + Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']); } diff --git a/Database/Db.php b/Database/Db.php index eb178466..5d1022fd 100644 --- a/Database/Db.php +++ b/Database/Db.php @@ -12,6 +12,7 @@ namespace Database; use Database\Traits\QueryTrait; use Exception; use HttpServer\Http\Context; +use Snowflake\Event; use Snowflake\Snowflake; /** @@ -49,9 +50,8 @@ class Db if (!static::transactionsActive()) { return; } - $event = Snowflake::app()->getEvent(); - $event->trigger(Connection::TRANSACTION_COMMIT); - $event->offName(Connection::TRANSACTION_COMMIT); + Event::trigger(Connection::TRANSACTION_COMMIT); + Event::offName(Connection::TRANSACTION_COMMIT); static::$_inTransaction = false; } @@ -63,9 +63,8 @@ class Db if (!static::transactionsActive()) { return; } - $event = Snowflake::app()->getEvent(); - $event->trigger(Connection::TRANSACTION_ROLLBACK); - $event->offName(Connection::TRANSACTION_ROLLBACK); + Event::trigger(Connection::TRANSACTION_ROLLBACK); + Event::offName(Connection::TRANSACTION_ROLLBACK); static::$_inTransaction = false; } diff --git a/Database/databases.php b/Database/databases.php deleted file mode 100644 index 16b9c541..00000000 --- a/Database/databases.php +++ /dev/null @@ -1,66 +0,0 @@ - [ - - 'file' => [ - 'path' => strpos(null, 'data') - ], - - 'redis' => [ - 'host' => '127.0.0.1', - 'port' => '6379', - 'prefix' => 'cache_', - 'auth' => '', - 'databases' => '0', - 'timeout' => -1, - 'read_timeout' => -1, - ], - ], - 'databases' => [ - 'db' => [ - 'id' => 'db', - 'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS, - 'tablePrefix' => 'aircraftwar_', - 'maxNumber' => 100, - 'slaveConfig' => [ - 'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS - ], - ], - 'server' => [ - 'id' => 'server', - 'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS, - 'tablePrefix' => 'master_', - 'maxNumber' => 100, - 'slaveConfig' => [ - 'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS - ], - ], - 'game' => [ - 'id' => 'game', - 'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS, - 'maxNumber' => 100, - 'tablePrefix' => 'game_', - 'slaveConfig' => [ - 'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST, - 'username' => CONNECT_USER, - 'password' => CONNECT_PASS - ], - ], - ] -]; diff --git a/HttpServer/Events/OnAfterReload.php b/HttpServer/Events/OnAfterReload.php index b87339d7..8f110b20 100644 --- a/HttpServer/Events/OnAfterReload.php +++ b/HttpServer/Events/OnAfterReload.php @@ -21,13 +21,11 @@ class OnAfterReload extends Callback /** * @param Server $server - * @throws ComponentException * @throws Exception */ public function onHandler(Server $server) { - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SERVER_AFTER_RELOAD, [$server]); + Event::trigger(Event::SERVER_AFTER_RELOAD, [$server]); } } diff --git a/HttpServer/Events/OnBeforeReload.php b/HttpServer/Events/OnBeforeReload.php index 1a9d9183..16704da0 100644 --- a/HttpServer/Events/OnBeforeReload.php +++ b/HttpServer/Events/OnBeforeReload.php @@ -24,8 +24,7 @@ class OnBeforeReload extends Callback */ public function onHandler(Server $server) { - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]); + Event::trigger(Event::SERVER_BEFORE_RELOAD, [$server]); Snowflake::clearWorkerPid(); Snowflake::clearTaskPid(); diff --git a/HttpServer/Events/OnClose.php b/HttpServer/Events/OnClose.php index 546f437d..f84298b3 100644 --- a/HttpServer/Events/OnClose.php +++ b/HttpServer/Events/OnClose.php @@ -31,12 +31,10 @@ class OnClose extends Callback fire(Event::SYSTEM_RESOURCE_RELEASES); }); $clientInfo = $server->getClientInfo($fd); - $event = Snowflake::app()->getEvent(); - - if (!$event->exists(($name = $this->getName($clientInfo)))) { + if (!Event::exists(($name = $this->getName($clientInfo)))) { return; } - $event->trigger($name, [$server, $fd]); + Event::trigger($name, [$server, $fd]); } catch (\Throwable $exception) { $this->addError($exception, 'throwable'); } diff --git a/HttpServer/Events/OnMessage.php b/HttpServer/Events/OnMessage.php index 5b3ea1b1..e6ce822a 100644 --- a/HttpServer/Events/OnMessage.php +++ b/HttpServer/Events/OnMessage.php @@ -45,12 +45,10 @@ class OnMessage extends Callback return; } $clientInfo = $server->getClientInfo($frame->fd); - $event = Snowflake::app()->getEvent(); - - if (!$event->exists(($name = $this->getName($clientInfo)))) { + if (!Event::exists(($name = $this->getName($clientInfo)))) { return; } - $event->trigger($name, [$frame, $server]); + Event::trigger($name, [$frame, $server]); } catch (\Throwable $exception) { $this->addError($exception, 'websocket'); if (!swoole()->exist($frame->fd)) { diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index e7c62ffc..34242d76 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -38,7 +38,6 @@ class OnRequest extends Callback */ public function init() { - $this->event = Snowflake::app()->getEvent(); $this->router = Snowflake::app()->getRouter(); } @@ -93,8 +92,6 @@ class OnRequest extends Callback [$sRequest, $sResponse] = [HRequest::create($sRequest), HResponse::create($sResponse)]; } - $this->event->dispatch(Event::EVENT_AFTER_REQUEST, [$sRequest, $exception]); - $headers = $sRequest->headers->get('access-control-request-headers'); $methods = $sRequest->headers->get('access-control-request-method'); diff --git a/HttpServer/Events/OnWorkerError.php b/HttpServer/Events/OnWorkerError.php index 0ca29f48..b24f3a21 100644 --- a/HttpServer/Events/OnWorkerError.php +++ b/HttpServer/Events/OnWorkerError.php @@ -28,8 +28,7 @@ class OnWorkerError extends Callback */ public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) { - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SERVER_WORKER_ERROR); + Event::trigger(Event::SERVER_WORKER_ERROR); $message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d', $worker_id, $signal, $worker_pid, $exit_code diff --git a/HttpServer/Events/OnWorkerExit.php b/HttpServer/Events/OnWorkerExit.php index 02ac5b1f..8e233bd7 100644 --- a/HttpServer/Events/OnWorkerExit.php +++ b/HttpServer/Events/OnWorkerExit.php @@ -25,11 +25,10 @@ class OnWorkerExit extends Callback public function onHandler($server, $worker_id) { putenv('state=exit'); - $event = Snowflake::app()->getEvent(); $channel = Snowflake::app()->getChannel(); $channel->cleanAll(); - $event->trigger(Event::SERVER_WORKER_EXIT); + Event::trigger(Event::SERVER_WORKER_EXIT); logger()->insert(); } diff --git a/HttpServer/Events/OnWorkerStop.php b/HttpServer/Events/OnWorkerStop.php index 49cc5677..6c34d339 100644 --- a/HttpServer/Events/OnWorkerStop.php +++ b/HttpServer/Events/OnWorkerStop.php @@ -26,8 +26,7 @@ class OnWorkerStop extends Callback */ public function onHandler($server, $worker_id) { - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SERVER_WORKER_STOP); + Event::trigger(Event::SERVER_WORKER_STOP); $this->clearMysqlClient(); $this->clearRedisClient(); diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 21bd94c3..e1b8c774 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -17,6 +17,7 @@ use Exception; use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Error\LoggerProcess; +use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Exception\NotFindClassException; use Snowflake\Process\Biomonitoring; @@ -300,9 +301,8 @@ class Server extends HttpService if (!is_array($config['events'])) { return; } - $event = Snowflake::app()->getEvent(); foreach ($config['events'] as $name => $_event) { - $event->on('listen ' . $config['port'] . ' ' . $name, $_event); + Event::on('listen ' . $config['port'] . ' ' . $name, $_event); } } diff --git a/HttpServer/config.php b/HttpServer/config.php deleted file mode 100644 index cb74f957..00000000 --- a/HttpServer/config.php +++ /dev/null @@ -1,111 +0,0 @@ - [ - [ - 'id' => '', - 'type' => Server::HTTP, - 'host' => '127.0.0.1', - 'port' => 9527, - 'settings' => [ - 'worker_num' => 10, - 'enable_coroutine' => 1 - ], - 'events' => [ - Event::SERVER_WORKER_START => function () { - $router = Snowflake::app()->getRouter(); - $router->loadRouterSetting(); - }, - ] - ], - - [ - 'id' => '', - 'type' => Server::PACKAGE, - 'host' => '127.0.0.1', - 'port' => 9628, - 'settings' => [ - 'worker_num' => 10, - 'enable_coroutine' => 1 - ], - 'message' => [ - 'pack' => function ($data) { - return \Snowflake\Core\Json::encode($data); - }, - 'unpack' => function ($data) { - return \Snowflake\Core\Json::decode($data); - }, - ], - 'events' => [ - ] - ], - [ - 'id' => '', - 'type' => Server::TCP, - 'host' => '127.0.0.1', - 'port' => 9629, - 'settings' => [ - 'worker_num' => 10, - 'enable_coroutine' => 1 - ], - 'message' => [ - 'pack' => function ($data) { - return \Snowflake\Core\Json::encode($data); - }, - 'unpack' => function ($data) { - return \Snowflake\Core\Json::decode($data); - }, - ], - 'events' => [ - Event::RECEIVE_CONNECTION => function ($data) { - return 'hello word~'; - } - ] - ], - [ - 'id' => '', - 'type' => Server::WEBSOCKET, - 'host' => '127.0.0.1', - 'port' => 9530, - 'settings' => [ - 'worker_num' => 10, - 'enable_coroutine' => 1 - ], - 'events' => [ - Event::SERVER_WORKER_START => function () { - $path = APP_PATH . 'app/Websocket'; - $websocket = Snowflake::app()->annotation->websocket; - $websocket->registration_notes($path, 'App\\Sockets\\'); - }, - Event::SERVER_HANDSHAKE => function (Request $request, Response $response) { - $this->error($request->fd . ' connect.'); - $response->status(101); - $response->end(); - }, - Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) { - $this->error('websocket SERVER_MESSAGE.'); - if (is_null($json = json_decode($frame->data, true))) { - return $server->push($frame->fd, 'format error~'); - } - $websocket = Snowflake::app()->annotation->websocket; - if ($websocket->has($json['path'])) { - return $websocket->runWith($json['path'], [$frame->fd, $json]); - } else { - return $server->push($frame->fd, 'hello word~'); - } - }, - Event::SERVER_CLOSE => function (int $fd) { - $this->error($fd . ' disconnect.'); - return 'hello word~'; - } - ] - ], - ] -]; diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index f61f4892..17941ddb 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -180,43 +180,42 @@ abstract class BaseApplication extends Service if (!isset($config['events']) || !is_array($config['events'])) { return; } - $event = Snowflake::app()->getEvent(); foreach ($config['events'] as $key => $value) { if (is_string($value)) { $value = Snowflake::createObject($value); } - $this->addEvent($event, $key, $value); + $this->addEvent($key, $value); } } - /** - * @param $event - * @param $key - * @param $value - * @throws InitException - * @throws NotFindClassException - * @throws ReflectionException - */ - private function addEvent($event, $key, $value): void + /** + * @param $key + * @param $value + * @throws InitException + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function addEvent($key, $value): void { if ($value instanceof \Closure) { - $event->on($key, $value, [], true); + Event::on($key, $value, [], true); return; } if (is_object($value)) { - $event->on($key, $value, [], true); + Event::on($key, $value, [], true); return; } if (is_array($value)) { if (is_object($value[0]) && !($value[0] instanceof \Closure)) { - $event->on($key, $value, [], true); + Event::on($key, $value, [], true); return; } if (is_string($value[0])) { $value[0] = Snowflake::createObject($value[0]); - $event->on($key, $value, [], true); + Event::on($key, $value, [], true); return; } @@ -224,7 +223,7 @@ abstract class BaseApplication extends Service if (!is_callable($item, true)) { throw new InitException("Class does not hav callback."); } - $event->on($key, $item, [], true); + Event::on($key, $item, [], true); } } @@ -367,16 +366,6 @@ abstract class BaseApplication extends Service } - /** - * @return Event - * @throws Exception - */ - public function getEvent(): Event - { - return $this->get('event'); - } - - /** * @return Jwt * @throws Exception @@ -454,7 +443,6 @@ abstract class BaseApplication extends Service { $this->setComponents([ 'error' => ['class' => ErrorHandler::class], - 'event' => ['class' => Event::class], 'connections' => ['class' => Connection::class], 'redis_connections' => ['class' => SRedis::class], 'pool' => ['class' => SPool::class], diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index 06a95978..458e7c4c 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -35,10 +35,9 @@ class Redis extends Component */ public function init() { - $event = Snowflake::app()->getEvent(); - $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']); - $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']); - $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']); + Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']); } diff --git a/System/Error/ErrorHandler.php b/System/Error/ErrorHandler.php index 280addd5..9550de87 100644 --- a/System/Error/ErrorHandler.php +++ b/System/Error/ErrorHandler.php @@ -74,8 +74,7 @@ class ErrorHandler extends Component implements ErrorInterface { $this->category = 'exception'; - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); + Event::trigger(Event::SYSTEM_RESOURCE_CLEAN); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); } @@ -103,8 +102,7 @@ class ErrorHandler extends Component implements ErrorInterface Snowflake::app()->error($data, 'error'); - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SYSTEM_RESOURCE_CLEAN); + Event::trigger(Event::SYSTEM_RESOURCE_CLEAN); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } diff --git a/System/Error/Logger.php b/System/Error/Logger.php index f33cde46..c00ff2d6 100644 --- a/System/Error/Logger.php +++ b/System/Error/Logger.php @@ -33,9 +33,8 @@ class Logger extends Component public function init() { - $event = Snowflake::app()->getEvent(); - $event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']); - $event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']); + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']); } diff --git a/System/Event.php b/System/Event.php index 4259c780..02de4a89 100644 --- a/System/Event.php +++ b/System/Event.php @@ -71,7 +71,7 @@ class Event extends BaseObject * @param bool $isAppend * @throws Exception */ - public function on($name, $callback, $parameter = [], $isAppend = false) + public static function on($name, $callback, $parameter = [], $isAppend = false) { if (!isset(static::$_events[$name])) { static::$_events[$name] = []; @@ -84,7 +84,7 @@ class Event extends BaseObject } $callback[0] = Snowflake::createObject($callback[0]); } - if ($this->exists($name, $callback)) { + if (static::exists($name, $callback)) { return; } if (!empty(static::$_events[$name]) && $isAppend === true) { @@ -99,7 +99,7 @@ class Event extends BaseObject * @param $name * @param $callback */ - public function of($name, $callback): void + public static function of($name, $callback): void { if (!isset(static::$_events[$name])) { return; @@ -118,13 +118,13 @@ class Event extends BaseObject * @param $name * @return bool */ - public function offName($name): bool + public static function offName($name): bool { - if (!$this->exists($name)) { + if (!static::exists($name)) { return true; } unset(static::$_events[$name]); - return $this->exists($name); + return static::exists($name); } @@ -133,7 +133,7 @@ class Event extends BaseObject * @param null $callback * @return bool */ - public function exists($name, $callback = null): bool + public static function exists($name, $callback = null): bool { if (!isset(static::$_events[$name])) { return false; @@ -156,9 +156,9 @@ class Event extends BaseObject * @param $handler * @return mixed */ - public function get($name, $handler): mixed + public static function get($name, $handler): mixed { - if (!$this->exists($name, $handler)) { + if (!static::exists($name, $handler)) { return null; } @@ -175,7 +175,7 @@ class Event extends BaseObject } - public function clean() + public static function clean() { static::$_events = []; } @@ -188,9 +188,9 @@ class Event extends BaseObject * @return bool * @throws Exception */ - public function dispatch($name, $params = [], $scope = null): bool + public static function dispatch($name, $params = [], $scope = null): bool { - return $this->trigger($name, $params, $scope); + return static::trigger($name, $params, $scope); } @@ -202,17 +202,17 @@ class Event extends BaseObject * @return bool * @throws Exception */ - public function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool + public static function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool { - $events = $this->get($name, $handler); + $events = static::get($name, $handler); if (empty($events)) { return true; } foreach ($events as $event) { - $this->execute($event, $parameter); + static::execute($event, $parameter); } if ($is_remove) { - $this->offName($name); + static::offName($name); } return true; } @@ -224,16 +224,16 @@ class Event extends BaseObject * @return bool * @throws Exception */ - private function execute($event, $parameter): bool + private static function execute($event, $parameter): bool { try { - $meta = $this->mergeParams($event[1], $parameter); + $meta = static::mergeParams($event[1], $parameter); if (call_user_func($event[0], ...$meta) === false) { return false; } return true; } catch (\Throwable $throwable) { - return $this->addError($throwable,'throwable'); + return static::addError($throwable,'throwable'); } } @@ -243,7 +243,7 @@ class Event extends BaseObject * @param $parameter * @return array */ - private function mergeParams($defaultParameter, $parameter = []): array + private static function mergeParams($defaultParameter, $parameter = []): array { if (empty($defaultParameter)) { $defaultParameter = $parameter; diff --git a/function.php b/function.php index b1e5dc27..265fe26e 100644 --- a/function.php +++ b/function.php @@ -19,6 +19,7 @@ use Snowflake\Abstracts\Config; use Snowflake\Application; use Snowflake\Core\ArrayAccess; use Snowflake\Error\Logger; +use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; use Swoole\WebSocket\Server; @@ -350,8 +351,7 @@ if (!function_exists('fire')) { */ function fire(string $event, array $params = []) { - $logger = Snowflake::app()->getEvent(); - $logger->trigger($event, $params); + Event::trigger($event, $params); } } @@ -586,8 +586,7 @@ if (!function_exists('listen')) { */ function listen($name, $callback, $params = [], $isAppend = true) { - $event = Snowflake::app()->getEvent(); - $event->on($name, $callback, $params, $isAppend); + Event::on($name, $callback, $params, $isAppend); } } @@ -606,8 +605,7 @@ if (!function_exists('event')) { */ function event($name, $callback, $params = [], $isAppend = true) { - $event = Snowflake::app()->getEvent(); - $event->on($name, $callback, $params, $isAppend); + Event::on($name, $callback, $params, $isAppend); } }