diff --git a/Database/Connection.php b/Database/Connection.php index 70ef5ee3..6308f943 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Database; - +use JetBrains\PhpStorm\Pure; use ReflectionException; use Snowflake\Abstracts\Component; use Database\Mysql\Schema; @@ -69,7 +69,7 @@ class Connection extends Component */ public function init() { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->on(Event::RELEASE_ALL, [$this, 'disconnect']); $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'clear_connection']); } @@ -85,7 +85,7 @@ class Connection extends Component } $this->beginTransaction(); - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); $event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); } @@ -97,7 +97,7 @@ class Connection extends Component */ public function getConnect($sql = NULL): PDO { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->initConnections($this->cds, true, $this->maxNumber); $connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber); $connections->setTimeout($this->timeout); @@ -108,10 +108,11 @@ class Connection extends Component /** * 初始化 Channel + * @throws ComponentException */ public function fill() { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->initConnections($this->cds, true, $this->maxNumber); $connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber); } @@ -152,7 +153,7 @@ class Connection extends Component * @param $sql * @return bool */ - public function isWrite($sql): bool + #[Pure] public function isWrite($sql): bool { if (empty($sql)) return false; @@ -179,13 +180,11 @@ class Connection extends Component */ public function masterInstance(): PDO { - $config = [ + return $this->connections()->getConnection([ 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password - ]; - $pool = Snowflake::app()->connections; - return $pool->getConnection($config, true); + ], true); } /** @@ -194,12 +193,20 @@ class Connection extends Component */ public function slaveInstance(): PDO { - if (empty($this->slaveConfig)) { + if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { return $this->masterInstance(); } + return $this->connections()->getConnection($this->slaveConfig, false); + } - $connections = Snowflake::app()->connections; - return $connections->getConnection($this->slaveConfig, false); + + /** + * @return \Snowflake\Pool\Connection + * @throws ComponentException + */ + private function connections(): \Snowflake\Pool\Connection + { + return Snowflake::app()->getConnections(); } @@ -209,8 +216,7 @@ class Connection extends Component */ public function beginTransaction(): static { - $connections = Snowflake::app()->connections; - $connections->beginTransaction($this->cds); + $this->connections()->beginTransaction($this->cds); return $this; } @@ -220,8 +226,7 @@ class Connection extends Component */ public function inTransaction(): bool|static { - $connections = Snowflake::app()->connections; - return $connections->inTransaction($this->cds); + return $this->connections()->inTransaction($this->cds); } /** @@ -230,8 +235,7 @@ class Connection extends Component */ public function rollback() { - $connections = Snowflake::app()->connections; - $connections->rollback($this->cds); + $this->connections()->rollback($this->cds); } /** @@ -240,8 +244,7 @@ class Connection extends Component */ public function commit() { - $connections = Snowflake::app()->connections; - $connections->commit($this->cds); + $this->connections()->commit($this->cds); } /** @@ -287,7 +290,7 @@ class Connection extends Component */ public function release() { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->release($this->cds, true); $connections->release($this->slaveConfig['cds'], false); @@ -296,10 +299,11 @@ class Connection extends Component /** * 临时回收 + * @throws ComponentException */ public function recovery() { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->release($this->cds, true); $connections->release($this->slaveConfig['cds'], false); @@ -312,7 +316,7 @@ class Connection extends Component */ public function clear_connection() { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->release($this->cds, true); $connections->release($this->slaveConfig['cds'], false); @@ -324,7 +328,7 @@ class Connection extends Component */ public function disconnect() { - $connections = Snowflake::app()->connections; + $connections = $this->connections(); $connections->disconnect($this->cds); $connections->disconnect($this->slaveConfig['cds']); } diff --git a/Database/Db.php b/Database/Db.php index 5fae723c..020de10f 100644 --- a/Database/Db.php +++ b/Database/Db.php @@ -43,7 +43,7 @@ class Db */ public static function commit() { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Connection::TRANSACTION_COMMIT); $event->offName(Connection::TRANSACTION_COMMIT); static::$isActive = false; @@ -54,7 +54,7 @@ class Db */ public static function rollback() { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Connection::TRANSACTION_ROLLBACK); $event->offName(Connection::TRANSACTION_ROLLBACK); static::$isActive = false; diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php index ac29a7c5..5cf923c6 100644 --- a/HttpServer/Abstracts/Callback.php +++ b/HttpServer/Abstracts/Callback.php @@ -39,7 +39,7 @@ abstract class Callback extends Application $logger->write($this->_MESSAGE[$message] . $worker_id); $logger->clear(); - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->offName(Event::EVENT_AFTER_REQUEST); $event->offName(Event::EVENT_BEFORE_REQUEST); $this->eventNotify($message, $event); diff --git a/HttpServer/Events/OnConnect.php b/HttpServer/Events/OnConnect.php index a63c3102..f9927628 100644 --- a/HttpServer/Events/OnConnect.php +++ b/HttpServer/Events/OnConnect.php @@ -27,7 +27,7 @@ class OnConnect extends Callback */ public function onHandler(\Swoole\Server $server, int $fd, int $reactorId) { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::RECEIVE_CONNECTION, [$server, $fd, $reactorId]); } diff --git a/HttpServer/Events/OnManagerStart.php b/HttpServer/Events/OnManagerStart.php index 5ddd2555..a6fb2c8c 100644 --- a/HttpServer/Events/OnManagerStart.php +++ b/HttpServer/Events/OnManagerStart.php @@ -23,7 +23,7 @@ class OnManagerStart extends Callback $this->debug('manager start.'); Snowflake::setWorkerId($server->manager_pid); - $events = Snowflake::app()->event; + $events = Snowflake::app()->getEvent(); $events->trigger(Event::SERVER_MANAGER_START, null, $server); if (Snowflake::isLinux()) { $prefix = Config::get('id', false, 'system:'); diff --git a/HttpServer/Events/OnManagerStop.php b/HttpServer/Events/OnManagerStop.php index 079e8b64..a526e5f4 100644 --- a/HttpServer/Events/OnManagerStop.php +++ b/HttpServer/Events/OnManagerStop.php @@ -25,7 +25,7 @@ class OnManagerStop extends Callback { $this->warning('manager stop.'); - $events = Snowflake::app()->event; + $events = Snowflake::app()->getEvent(); $events->trigger(Event::SERVER_MANAGER_STOP, [$server]); $runPath = storage(null, 'worker'); diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index 6dc75b27..b1cb00e5 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -49,7 +49,7 @@ class OnPacket extends Callback $response = DataResolve::pack($this->pack, $response); return $server->sendto($clientInfo['address'], $clientInfo['port'], $response); } finally { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::SERVER_WORKER_STOP); } } diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php index e59b78eb..c198d805 100644 --- a/HttpServer/Events/OnReceive.php +++ b/HttpServer/Events/OnReceive.php @@ -52,7 +52,7 @@ class OnReceive extends Callback $response = DataResolve::pack($this->pack, $response); return $server->send($fd, $response); } finally { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::SERVER_WORKER_STOP); } } diff --git a/HttpServer/Events/OnStart.php b/HttpServer/Events/OnStart.php index 4cf204ac..33bd5736 100644 --- a/HttpServer/Events/OnStart.php +++ b/HttpServer/Events/OnStart.php @@ -24,7 +24,7 @@ class OnStart extends Callback if (Snowflake::isLinux()) { name(Config::get('id', false, 'system:') . ': master.'); } - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::SERVER_EVENT_START, null, $server); } diff --git a/HttpServer/Events/OnTask.php b/HttpServer/Events/OnTask.php index 2313450d..c5a568bd 100644 --- a/HttpServer/Events/OnTask.php +++ b/HttpServer/Events/OnTask.php @@ -108,7 +108,7 @@ class OnTask extends Callback $finish['info'] = $this->format($exception); $this->error($exception, 'Task'); } finally { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::RELEASE_ALL); Timer::clearAll(); } diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 29e35e49..50753045 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -53,7 +53,7 @@ class OnWorkerStart extends Callback { try { $this->debug(sprintf('Worker #%d is start.....', $worker_id)); - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::SERVER_WORKER_START, [$worker_id]); } catch (\Throwable $exception) { write($exception->getMessage(), 'worker'); diff --git a/HttpServer/Http/Response.php b/HttpServer/Http/Response.php index c0464451..2800e03d 100644 --- a/HttpServer/Http/Response.php +++ b/HttpServer/Http/Response.php @@ -199,7 +199,7 @@ class Response extends Application $string .= 'Command End!' . PHP_EOL . PHP_EOL; echo $string; - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger('CONSOLE_END'); return $result; diff --git a/HttpServer/Server.php b/HttpServer/Server.php index c0b07e88..e303563d 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -314,7 +314,7 @@ class Server extends Application if (!is_array($config['events'])) { return; } - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); foreach ($config['events'] as $name => $_event) { $event->on($name, $_event); } diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index 58e29bf5..6ebf6de8 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -173,7 +173,7 @@ abstract class BaseApplication extends Service if (!isset($config['events']) || !is_array($config['events'])) { return; } - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); foreach ($config['events'] as $key => $value) { if (is_string($value)) { if (!class_exists($value)) { diff --git a/System/Abstracts/Component.php b/System/Abstracts/Component.php index 6b651016..9073e49e 100644 --- a/System/Abstracts/Component.php +++ b/System/Abstracts/Component.php @@ -76,7 +76,7 @@ class Component extends BaseObject */ public function trigger($name, $event = null, $params = [], $isRemove = false) { - $aEvents = Snowflake::app()->event; + $aEvents = Snowflake::app()->getEvent(); if (isset($this->_events[$name])) { $events = $this->_events[$name]; foreach ($events as $key => $_event) { @@ -100,7 +100,7 @@ class Component extends BaseObject */ public function off($name, $handler = NULL): void { - $aEvents = Snowflake::app()->event; + $aEvents = Snowflake::app()->getEvent(); if (!isset($this->_events[$name])) { $aEvents->of($name, $handler); return; @@ -129,7 +129,7 @@ class Component extends BaseObject public function offAll() { $this->_events = []; - $aEvents = Snowflake::app()->event; + $aEvents = Snowflake::app()->getEvent(); $aEvents->clean(); } diff --git a/System/Cache/File.php b/System/Cache/File.php index 241fd8f0..6fbe7952 100644 --- a/System/Cache/File.php +++ b/System/Cache/File.php @@ -12,6 +12,7 @@ namespace Snowflake\Cache; use Exception; +use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\Component; use Swoole\Coroutine\System; @@ -86,7 +87,7 @@ class File extends Component implements ICache /** * @param string $key * @param string $hashKey - * @return string|int|bool + * @return string|int|bool|null */ public function hGet(string $key, string $hashKey): string|int|bool|null { @@ -119,7 +120,7 @@ class File extends Component implements ICache * @param $key * @return bool */ - public function exists($key): bool + #[Pure] public function exists($key): bool { return file_exists($key); } diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index ea09958b..57728c6d 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -36,7 +36,7 @@ class Redis extends Component */ public function init() { - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->on(Event::RELEASE_ALL, [$this, 'destroy']); $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']); $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); @@ -46,10 +46,11 @@ class Redis extends Component /** * @throws ConfigException + * @throws ComponentException */ public function createPool() { - $connections = Snowflake::app()->pool->redis; + $connections = Snowflake::app()->getPool()->getRedis(); $config = $this->get_config(); $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; @@ -112,10 +113,11 @@ SCRIPT; /** * 释放连接池 * @throws ConfigException + * @throws ComponentException */ public function release() { - $connections = Snowflake::app()->pool->redis; + $connections = Snowflake::app()->getPool()->getRedis(); $connections->release($this->get_config(), true); } diff --git a/System/Error/ErrorHandler.php b/System/Error/ErrorHandler.php index d40e83c4..8a24799c 100644 --- a/System/Error/ErrorHandler.php +++ b/System/Error/ErrorHandler.php @@ -74,7 +74,7 @@ class ErrorHandler extends Component implements ErrorInterface { $this->category = 'exception'; - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::RELEASE_ALL); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); @@ -102,7 +102,7 @@ class ErrorHandler extends Component implements ErrorInterface logger()->error($data, 'error'); - $event = Snowflake::app()->event; + $event = Snowflake::app()->getEvent(); $event->trigger(Event::RELEASE_ALL); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);