From b497164f39015a9bf93edd39811565f5702d026d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 10 Sep 2021 11:35:23 +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 --- http-helper/Route/Node.php | 501 +++++++++++++++--------------- http-server/Constrict/Request.php | 4 +- http-server/Service/Http.php | 12 +- 3 files changed, 256 insertions(+), 261 deletions(-) diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index cc173afa..e0fb3274 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -7,6 +7,7 @@ namespace Http\Route; use Closure; use Exception; +use Http\Context\Context; use Http\Exception\RequestException; use JetBrains\PhpStorm\Pure; use Kiri\Events\EventProvider; @@ -14,8 +15,8 @@ use Kiri\Exception\NotFindClassException; use Kiri\Kiri; use ReflectionException; use Server\Constant; -use Server\Events\OnAfterWorkerStart; use Server\Constrict\RequestInterface; +use Server\Events\OnAfterWorkerStart; /** * Class Node @@ -24,291 +25,291 @@ use Server\Constrict\RequestInterface; class Node { - public string $path = ''; - public int $index = 0; + public string $path = ''; + public int $index = 0; - /** @var string[] */ - public array $method = []; + /** @var string[] */ + public array $method = []; - /** @var Node[] $childes */ - public array $childes = []; + /** @var Node[] $childes */ + public array $childes = []; - public array $group = []; + public array $group = []; - private string $_error = ''; + private string $_error = ''; - private string $_dataType = ''; + private string $_dataType = ''; - /** @var array */ - private array $_handler = []; + /** @var array */ + private array $_handler = []; - public string $htmlSuffix = '.html'; - public bool $enableHtmlSuffix = false; + public string $htmlSuffix = '.html'; + public bool $enableHtmlSuffix = false; - /** @var array */ - public array $namespace = []; + /** @var array */ + public array $namespace = []; - /** @var array */ - public array $middleware = []; + /** @var array */ + public array $middleware = []; - public string $sourcePath = ''; + public string $sourcePath = ''; - /** @var array|Closure */ - public Closure|array $callback = []; + /** @var array|Closure */ + public Closure|array $callback = []; - private string $_alias = ''; + private string $_alias = ''; - /** - * @param string $dataType - */ - public function setDataType(string $dataType) - { - $this->_dataType = $dataType; - } + /** + * @param string $dataType + */ + public function setDataType(string $dataType) + { + $this->_dataType = $dataType; + } - /** - * @throws NotFindClassException - * @throws ReflectionException - */ - public function __construct(public Router $router) - { - $eventDispatcher = di(EventProvider::class); - $eventDispatcher->on(OnAfterWorkerStart::class, [$this, 'setParameters']); - } + /** + * @throws NotFindClassException + * @throws ReflectionException + */ + public function __construct(public Router $router) + { + $eventDispatcher = di(EventProvider::class); + $eventDispatcher->on(OnAfterWorkerStart::class, [$this, 'setParameters']); + } - /** - * @param string $data - * @return mixed - */ - public function unpack(string $data): mixed - { - if ($this->_dataType == 'json') { - return json_decode($data, true); - } - if ($this->_dataType == 'serializes') { - return unserialize($data); - } - return $data; - } + /** + * @param string $data + * @return mixed + */ + public function unpack(string $data): mixed + { + if ($this->_dataType == 'json') { + return json_decode($data, true); + } + if ($this->_dataType == 'serializes') { + return unserialize($data); + } + return $data; + } - /** - * @param string|array|Closure $handler - * @param string $method - * @param string $path - * @return Node - * @throws ReflectionException - */ - public function setHandler(string|array|Closure $handler, string $method, string $path): static - { - $this->sourcePath = '/' . ltrim($path, '/'); - if (is_string($handler) && str_contains($handler, '@')) { - $handler = $this->splitHandler($handler); - } else if ($handler != null && !is_callable($handler, true)) { - $this->_error = 'Controller is con\'t exec.'; - return $this; - } - $this->_handler[$method] = [$handler, $this->resolveMethodParams($handler)]; - return $this; - } + /** + * @param string|array|Closure $handler + * @param string $method + * @param string $path + * @return Node + * @throws ReflectionException + */ + public function setHandler(string|array|Closure $handler, string $method, string $path): static + { + $this->sourcePath = '/' . ltrim($path, '/'); + if (is_string($handler) && str_contains($handler, '@')) { + $handler = $this->splitHandler($handler); + } else if ($handler != null && !is_callable($handler, true)) { + $this->_error = 'Controller is con\'t exec.'; + return $this; + } + $this->_handler[$method] = [$handler, $this->resolveMethodParams($handler)]; + return $this; + } - /** - * @param $dispatcher - * @return array - * @throws \ReflectionException - */ - private function resolveMethodParams($dispatcher): array - { - $container = Kiri::getDi(); - if ($dispatcher instanceof Closure) { - $_injectParameters = $container->getFunctionParameters($dispatcher); - } else { - $_injectParameters = $container->getMethodParameters( - $dispatcher[0]::class, $dispatcher[1]); - } - return $_injectParameters; - } + /** + * @param $dispatcher + * @return array + * @throws \ReflectionException + */ + private function resolveMethodParams($dispatcher): array + { + $container = Kiri::getDi(); + if ($dispatcher instanceof Closure) { + $_injectParameters = $container->getFunctionParameters($dispatcher); + } else { + $_injectParameters = $container->getMethodParameters( + $dispatcher[0]::class, $dispatcher[1]); + } + return $_injectParameters; + } - /** - * @param string $handler - * @return array - */ - private function splitHandler(string $handler): array - { - list($controller, $action) = explode('@', $handler); - if (!class_exists($controller) && !empty($this->namespace)) { - $controller = implode('\\', $this->namespace) . '\\' . $controller; - } - return [Kiri::getDi()->get($controller), $action]; - } + /** + * @param string $handler + * @return array + */ + private function splitHandler(string $handler): array + { + list($controller, $action) = explode('@', $handler); + if (!class_exists($controller) && !empty($this->namespace)) { + $controller = implode('\\', $this->namespace) . '\\' . $controller; + } + return [Kiri::getDi()->get($controller), $action]; + } - /** - * @param string $method - * @param $handler - * @param $_injectParameters - * @throws ReflectionException - * @throws Exception - */ - private function injectMiddleware(string $method, $handler, $_injectParameters): void - { - $callback = (new Pipeline())->overall($this->router->getMiddleware()) - ->through($this->middleware[$method] ?? []) - ->through(MiddlewareManager::get($handler)) - ->send($_injectParameters) - ->then($handler); + /** + * @param string $method + * @param $handler + * @param $_injectParameters + * @throws ReflectionException + * @throws Exception + */ + private function injectMiddleware(string $method, $handler, $_injectParameters): void + { + $callback = (new Pipeline())->overall($this->router->getMiddleware()) + ->through($this->middleware[$method] ?? []) + ->through(MiddlewareManager::get($handler)) + ->send($_injectParameters) + ->then($handler); - HandlerProviders::add($method, $this->sourcePath, $callback); - } + HandlerProviders::add($method, $this->sourcePath, $callback); + } - /** - * @throws ReflectionException - */ - public function setParameters(): static - { - if (empty($this->_handler)) { - return $this; - } - foreach ($this->_handler as $method => $dispatcher) { - $this->injectMiddleware($method, ...$dispatcher); - } - $this->_handler = []; - return $this; - } + /** + * @throws ReflectionException + */ + public function setParameters(): static + { + if (empty($this->_handler)) { + return $this; + } + foreach ($this->_handler as $method => $dispatcher) { + $this->injectMiddleware($method, ...$dispatcher); + } + $this->_handler = []; + return $this; + } - /** - * @return array - */ - #[Pure] protected function annotation(): array - { - return $this->getMiddleWares(); - } + /** + * @return array + */ + #[Pure] protected function annotation(): array + { + return $this->getMiddleWares(); + } - /** - * @param RequestInterface $request - * @return bool - */ - #[Pure] public function methodAllow(RequestInterface $request): bool - { - if (!in_array($request->getMethod(), $this->method)) { - return true; - } - return $this->method == 'any'; - } + /** + * @param RequestInterface $request + * @return bool + */ + #[Pure] public function methodAllow(RequestInterface $request): bool + { + if (!in_array($request->getMethod(), $this->method)) { + return true; + } + return $this->method == 'any'; + } - /** - * @return bool - * @throws Exception - */ - public function checkSuffix(): bool - { - if ($this->enableHtmlSuffix) { - $url = request()->getUri()->getPath(); - $nowLength = strlen($this->htmlSuffix); - if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { - return false; - } - } - return true; - } + /** + * @return bool + * @throws Exception + */ + public function checkSuffix(): bool + { + if ($this->enableHtmlSuffix) { + $url = request()->getUri()->getPath(); + $nowLength = strlen($this->htmlSuffix); + if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) { + return false; + } + } + return true; + } - /** - * @return string - * 错误信息 - */ - public function getError(): string - { - return $this->_error; - } + /** + * @return string + * 错误信息 + */ + public function getError(): string + { + return $this->_error; + } - /** - * @param Node $node - * @return Node - */ - public function addChild(Node $node): Node - { - $this->childes[] = $node; - return $node; - } + /** + * @param Node $node + * @return Node + */ + public function addChild(Node $node): Node + { + $this->childes[] = $node; + return $node; + } - /** - * @param string $search - * @return Node|null - * @throws Exception - */ - public function findNode(string $search): ?Node - { - if (empty($this->childes)) { - return null; - } - foreach ($this->childes as $val) { - if ($search == $val->path) { - return $val; - } - } - return null; - } + /** + * @param string $search + * @return Node|null + * @throws Exception + */ + public function findNode(string $search): ?Node + { + if (empty($this->childes)) { + return null; + } + foreach ($this->childes as $val) { + if ($search == $val->path) { + return $val; + } + } + return null; + } - /** - * @param string $alias - * @return $this - * 别称 - */ - public function alias(string $alias): static - { - $this->_alias = $alias; - return $this; - } + /** + * @param string $alias + * @return $this + * 别称 + */ + public function alias(string $alias): static + { + $this->_alias = $alias; + return $this; + } - /** - * @return string - */ - public function getAlias(): string - { - return $this->_alias; - } + /** + * @return string + */ + public function getAlias(): string + { + return $this->_alias; + } - /** - * @param $method - * @param Closure|array $class - * @return $this - */ - public function addMiddleware($method, Closure|array $class): static - { - if (empty($class)) return $this; - if (!isset($this->middleware[$method])) { - $this->middleware[$method] = []; - } - foreach ($class as $closure) { - if (in_array($closure, $this->middleware[$method])) { - continue; - } - $this->middleware[$method][] = $closure; - } - return $this; - } + /** + * @param $method + * @param Closure|array $class + * @return $this + */ + public function addMiddleware($method, Closure|array $class): static + { + if (empty($class)) return $this; + if (!isset($this->middleware[$method])) { + $this->middleware[$method] = []; + } + foreach ($class as $closure) { + if (in_array($closure, $this->middleware[$method])) { + continue; + } + $this->middleware[$method][] = $closure; + } + return $this; + } - /** - * @return array - */ - public function getMiddleWares(): array - { - return $this->middleware; - } + /** + * @return array + */ + public function getMiddleWares(): array + { + return $this->middleware; + } /** @@ -317,16 +318,16 @@ class Node * @throws RequestException * @throws Exception */ - public function dispatch(RequestInterface $request): mixed - { - if (!in_array($request->getMethod(), $this->method)) { - throw new RequestException(Constant::STATUS_405_MESSAGE, 405); - } - $handlerProviders = HandlerProviders::get($this->sourcePath, $request->getMethod()); - if (empty($handlerProviders)) { - throw new RequestException(Constant::STATUS_404_MESSAGE, 404); - } - return $handlerProviders->interpreter($request); - } + public function dispatch(RequestInterface $request): mixed + { + if (!in_array($request->getMethod(), $this->method)) { + throw new RequestException(Constant::STATUS_405_MESSAGE, 405); + } + $handlerProviders = HandlerProviders::get($this->sourcePath, $request->getMethod()); + if (empty($handlerProviders)) { + throw new RequestException(Constant::STATUS_404_MESSAGE, 404); + } + return $handlerProviders->interpreter($request); + } } diff --git a/http-server/Constrict/Request.php b/http-server/Constrict/Request.php index 9ae58e42..f196b5a3 100644 --- a/http-server/Constrict/Request.php +++ b/http-server/Constrict/Request.php @@ -57,11 +57,11 @@ class Request implements RequestInterface { $serverRequest = ServerRequest::createServerRequest($request); - Context::setContext(ResponseInterface::class, $response = new Response()); + Context::setContext(ResponseInterface::class, new Response()); Context::setContext(RequestInterface::class, $serverRequest); - return [Kiri::getDi()->get(Request::class), $response]; + return Kiri::getDi()->get(Request::class); } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 63f9ed89..9235f67a 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -8,7 +8,7 @@ use Http\Exception\RequestException; use Http\Route\Node; use Kiri\Core\Help; use Server\Constant; -use Server\Constrict\RequestInterface; +use Server\Constrict\Request as ScRequest; use Server\Constrict\ResponseInterface; use Server\Events\OnAfterRequest; use Server\SInterface\OnClose; @@ -42,22 +42,16 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect public function onRequest(Request $request, Response $response): void { try { - [$request, $psr7Response] = \Server\Constrict\Request::create($request); - /** @var RequestInterface $request */ - $node = $this->router->Branch_search($request); + $node = $this->router->Branch_search($Psr7Request = ScRequest::create($request)); if (!($node instanceof Node)) { throw new RequestException(Constant::STATUS_404_MESSAGE, 404); } - $psr7Response = $node->dispatch($request); - if (!($psr7Response instanceof ResponseInterface)) { + if (!(($psr7Response = $node->dispatch($Psr7Request)) instanceof ResponseInterface)) { $psr7Response = $this->transferToResponse($psr7Response); } } catch (Error | \Throwable $exception) { $psr7Response = $this->exceptionHandler->emit($exception, $this->response); } finally { - if (!isset($psr7Response)) { - return; - } $this->responseEmitter->sender($response, $psr7Response); $this->eventDispatch->dispatch(new OnAfterRequest()); }