From f1d1ff1f4f2c649e03e8e0eed27670aff1e42217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 13 Sep 2023 12:30:57 +0800 Subject: [PATCH] eee --- src/ControllerInterpreter.php | 133 ++++++++++++++++----------------- src/Handler.php | 137 ++++++++++++++++++++-------------- 2 files changed, 147 insertions(+), 123 deletions(-) diff --git a/src/ControllerInterpreter.php b/src/ControllerInterpreter.php index cbbf227..c7a11dd 100644 --- a/src/ControllerInterpreter.php +++ b/src/ControllerInterpreter.php @@ -15,81 +15,80 @@ class ControllerInterpreter { - /** - * @param object $class - * @param string|ReflectionMethod $method - * @param ReflectionClass|null $reflection - * @return Handler - * @throws ReflectionException - */ - public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler - { - if (is_null($reflection)) { - $reflection = \Kiri::getDi()->getReflectionClass($class::class); - } - return $this->resolveMethod($class, $method, $reflection); - } + /** + * @param object $class + * @param string|ReflectionMethod $method + * @param ReflectionClass|null $reflection + * @return Handler + * @throws ReflectionException + */ + public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler + { + if (is_null($reflection)) { + $reflection = \Kiri::getDi()->getReflectionClass($class::class); + } + return $this->resolveMethod($class, $method, $reflection); + } - /** - * @param Closure $method - * @return Handler - * @throws ReflectionException - * @throws Exception - */ - public function addRouteByClosure(Closure $method): Handler - { - $reflection = new \ReflectionFunction($method); - if ($reflection->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') { - die('Request Handler returns must implements on Psr\Http\Message\ResponseInterface'); - } - $params = \Kiri::getDi()->resolveMethodParams($reflection); - return new Handler($method, $params); - } + /** + * @param Closure $method + * @return Handler + * @throws ReflectionException + * @throws Exception + */ + public function addRouteByClosure(Closure $method): Handler + { + $reflection = new \ReflectionFunction($method); + + $params = \Kiri::getDi()->resolveMethodParams($reflection); + + return new Handler($method, $params, $reflection->getReturnType()); + } - /** - * @param object $class - * @param string|ReflectionMethod $method - * @param ReflectionClass|null $reflection - * @return Handler - * @throws ReflectionException - */ - public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler - { - if (is_null($reflection)) { - $reflection = \Kiri::getDi()->getReflectionClass($class::class); - } - return $this->resolveMethod($class, $method, $reflection); - } + /** + * @param object $class + * @param string|ReflectionMethod $method + * @param ReflectionClass|null $reflection + * @return Handler + * @throws ReflectionException + */ + public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler + { + if (is_null($reflection)) { + $reflection = \Kiri::getDi()->getReflectionClass($class::class); + } + return $this->resolveMethod($class, $method, $reflection); + } - /** - * @param object $class - * @param string|ReflectionMethod $reflectionMethod - * @param ReflectionClass $reflectionClass - * @return Handler - * @throws ReflectionException - * @throws Exception - */ - public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler - { - if (empty($reflectionMethod)) { - return new Handler([$class, $reflectionMethod], []); - } - if (is_string($reflectionMethod)) { - $reflectionMethod = $reflectionClass->getMethod($reflectionMethod); - } + /** + * @param object $class + * @param string|ReflectionMethod $reflectionMethod + * @param ReflectionClass $reflectionClass + * @return Handler + * @throws ReflectionException + * @throws Exception + */ + public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler + { + if (empty($reflectionMethod)) { + return new Handler([$class, $reflectionMethod], [], $reflectionMethod->getReturnType()); + } + if (is_string($reflectionMethod)) { + $reflectionMethod = $reflectionClass->getMethod($reflectionMethod); + } - $returnType = $reflectionMethod->getReturnType(); - if (method_exists($returnType, 'getName') && $returnType->getName() !== 'Psr\Http\Message\ResponseInterface') { - die('Request Handler<' . $class::class . '::' . $reflectionMethod->getName() . '> returns must implements on Psr\Http\Message\ResponseInterface'); - } + $returnType = $reflectionMethod->getReturnType(); + if (method_exists($returnType, 'getName') && $returnType->getName() !== 'Psr\Http\Message\ResponseInterface') { + die('Request Handler<' . $class::class . '::' . $reflectionMethod->getName() . '> returns must implements on Psr\Http\Message\ResponseInterface'); + } - $container = \Kiri::getDi(); - $parameters = $container->getMethodParams($reflectionMethod); + $container = \Kiri::getDi(); + $parameters = $container->getMethodParams($reflectionMethod); - return new Handler([$class, $reflectionMethod->getName()], $parameters); - } + return new Handler([$class, $reflectionMethod->getName()], $parameters, $reflectionMethod->getReturnType()); + } } diff --git a/src/Handler.php b/src/Handler.php index 16dc67f..ad9d9f0 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -4,79 +4,104 @@ declare(strict_types=1); namespace Kiri\Router; use Closure; +use Kiri\Router\Constrict\Stream; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use ReflectionException; +use ReflectionNamedType; +use ReflectionUnionType; class Handler implements RequestHandlerInterface { - /** - * @param array|Closure $handler - * @param array $parameter - * @throws - */ - public function __construct(public array|Closure $handler, public array $parameter) - { - } + /** + * @param array|Closure $handler + * @param array $parameter + * @param ReflectionNamedType|ReflectionUnionType|null $responseType + */ + public function __construct(public array|Closure $handler, public array $parameter, public ReflectionNamedType|ReflectionUnionType|null $responseType) + { + } - /** - * @return bool - */ - public function isClosure(): bool - { - return $this->handler instanceof Closure; - } + /** + * @return bool + */ + public function isClosure(): bool + { + return $this->handler instanceof Closure; + } - /** - * @param string $interface - * @return bool - */ - public function implement(string $interface): bool - { - if (!$this->isClosure()) { - return $this->handler[0] instanceof $interface; - } - return false; - } + /** + * @param string $interface + * @return bool + */ + public function implement(string $interface): bool + { + if (!$this->isClosure()) { + return $this->handler[0] instanceof $interface; + } + return false; + } - /** - * @return string|null - */ - public function getClass(): ?string - { - if ($this->isClosure()) { - return null; - } - return $this->handler[0]::class; - } + /** + * @return string|null + */ + public function getClass(): ?string + { + if ($this->isClosure()) { + return null; + } + return $this->handler[0]::class; + } - /** - * @return string|null - */ - public function getMethod(): ?string - { - if ($this->isClosure()) { - return null; - } - return $this->handler[1]; - } + /** + * @return string|null + */ + public function getMethod(): ?string + { + if ($this->isClosure()) { + return null; + } + return $this->handler[1]; + } - /** - * @param ServerRequestInterface $request - * @return ResponseInterface - * @throws ReflectionException - */ - public function handle(ServerRequestInterface $request): ResponseInterface - { - // TODO: Implement handle() method. - return call_user_func($this->handler, ...$this->parameter); - } + /** + * @param ServerRequestInterface $request + * @return ResponseInterface + * @throws ReflectionException + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + // TODO: Implement handle() method. + if ($this->responseType->getName() !== 'void') { + return $this->typeEncode(); + } + call_user_func($this->handler, ...$this->parameter); + return response(); + } + + + /** + * @return ResponseInterface + */ + protected function typeEncode(): ResponseInterface + { + $result = call_user_func($this->handler, ...$this->parameter); + if ($result instanceof ResponseInterface) { + return $result; + } + if (is_object($result)) { + $result = '[object]'; + } else if (is_array($result)) { + $result = json_encode($result, JSON_UNESCAPED_UNICODE); + } + return response()->withBody(new Stream($result)); + } }