From 312c17bbe2e913ae88b8d2cbf857c73c6791ecc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 28 Jul 2021 14:09:39 +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 --- HttpServer/Http/Request.php | 21 +++++++++++++-------- HttpServer/Shutdown.php | 7 +++++-- function.php | 18 ++---------------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index 300aa879..30a28f55 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace HttpServer\Http; +use Annotation\Inject; use Exception; use HttpServer\Abstracts\HttpService; use HttpServer\IInterface\AuthIdentity; @@ -36,8 +37,18 @@ class Request extends HttpService public int $fd = 0; + + /** + * @var HttpParams|null + */ + #[Inject(HttpParams::class)] public ?HttpParams $params = null; + + /** + * @var HttpHeaders|null + */ + #[Inject(HttpHeaders::class)] public ?HttpHeaders $headers = null; public bool $isCli = FALSE; @@ -440,20 +451,14 @@ class Request extends HttpService * @return Request * @throws ReflectionException * @throws NotFindClassException + * @throws Exception */ public static function create(\Swoole\Http\Request $request): Request { $request->header = array_merge($request->header, $request->server); Context::setContext('request', $request); /** @var Request $sRequest */ - $sRequest = Snowflake::getDi()->get(Request::class); - if ($sRequest->headers === null) { - $sRequest->headers = di(HttpHeaders::class); - } - if ($sRequest->params === null) { - $sRequest->params = di(HttpParams::class); - } - return $sRequest; + return Snowflake::app()->get('request'); } diff --git a/HttpServer/Shutdown.php b/HttpServer/Shutdown.php index 6088eabb..720e3f3d 100644 --- a/HttpServer/Shutdown.php +++ b/HttpServer/Shutdown.php @@ -5,6 +5,7 @@ namespace HttpServer; use Exception; +use Server\ServerManager; use Snowflake\Abstracts\Component; @@ -48,7 +49,8 @@ class Shutdown extends Component return; } - $master_pid = Server()->setting['pid_file'] ?? PID_PATH; + $server = ServerManager::getContext()->getServer(); + $master_pid = $server->setting['pid_file'] ?? PID_PATH; if (file_exists($master_pid)) { $this->close(file_get_contents($master_pid)); } @@ -75,7 +77,8 @@ class Shutdown extends Component */ public function isRunning(): bool { - $master_pid = Server()->setting['pid_file'] ?? PID_PATH; + $server = ServerManager::getContext()->getServer(); + $master_pid = $server->setting['pid_file'] ?? PID_PATH; if (!file_exists($master_pid)) { return false; diff --git a/function.php b/function.php index c5e08631..47405328 100644 --- a/function.php +++ b/function.php @@ -388,7 +388,7 @@ if (!function_exists('trim_blank')) { * @param bool $htmlTags * @return array|string|null */ - function trim_blank(string $content, $len = 0, $encode = 'utf-8', $htmlTags = true): array|string|null + function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = true): array|string|null { $str = trim($content); if ($htmlTags) { @@ -505,20 +505,6 @@ if (!function_exists('Input')) { } -if (!function_exists('Server')) { - - - /** - * @return Http|Packet|Receive|Websocket|null - * @throws Exception - */ - function Server(): Http|Packet|Receive|Websocket|null - { - return Snowflake::app()->getSwoole(); - } - -} - if (!function_exists('storage')) { /** @@ -527,7 +513,7 @@ if (!function_exists('storage')) { * @return string * @throws Exception */ - function storage($fileName = '', $path = ''): string + function storage(string $fileName = '', string $path = ''): string { $basePath = rtrim(Snowflake::getStoragePath(), '/');