From aa8fb228cc4ad0d2349cdb66a85a99036d26a66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 19 Apr 2023 12:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Annotate/Aspect.php | 18 ---------- src/Annotate/Delete.php | 7 ++-- src/Annotate/Get.php | 10 +++--- src/Annotate/Head.php | 7 ++-- src/Annotate/Options.php | 7 ++-- src/Annotate/Post.php | 9 ++--- src/Annotate/Put.php | 6 ++-- src/Annotate/Route.php | 9 +++-- src/Aspect/Aspect.php | 67 +++++++++++++++++++++++++++++++++++ src/Base/Controller.php | 26 ++++++++++++-- src/ControllerInterpreter.php | 32 ++++++++++++++--- src/Handler.php | 8 +---- 12 files changed, 151 insertions(+), 55 deletions(-) delete mode 100644 src/Annotate/Aspect.php create mode 100644 src/Aspect/Aspect.php diff --git a/src/Annotate/Aspect.php b/src/Annotate/Aspect.php deleted file mode 100644 index 0289985..0000000 --- a/src/Annotate/Aspect.php +++ /dev/null @@ -1,18 +0,0 @@ -path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_DELETE, $path, [$class, $method]); } diff --git a/src/Annotate/Get.php b/src/Annotate/Get.php index 6cc0fe0..48b4ff2 100644 --- a/src/Annotate/Get.php +++ b/src/Annotate/Get.php @@ -14,28 +14,28 @@ class Get extends AbstractRequestMethod implements InjectRouteInterface { - /** * @param string $path + * @param string $version */ - public function __construct(readonly public string $path) + public function __construct(readonly public string $path, readonly public string $version = 'v1') { } - /** * @param object $class * @param string $method * @return void * @throws ReflectionException * @throws Exception - * @throws ReflectionException */ public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute(RequestMethod::REQUEST_GET, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_GET, $path, [$class, $method]); } } diff --git a/src/Annotate/Head.php b/src/Annotate/Head.php index 8d1291c..bc653ba 100644 --- a/src/Annotate/Head.php +++ b/src/Annotate/Head.php @@ -19,12 +19,11 @@ class Head extends AbstractRequestMethod implements InjectRouteInterface /** * @param string $path */ - public function __construct(readonly public string $path) + public function __construct(readonly public string $path, readonly public string $version = 'v1') { } - /** * @param object $class * @param string $method @@ -35,7 +34,9 @@ class Head extends AbstractRequestMethod implements InjectRouteInterface public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute(RequestMethod::REQUEST_HEAD, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_HEAD, $path, [$class, $method]); } diff --git a/src/Annotate/Options.php b/src/Annotate/Options.php index 6e61568..903efeb 100644 --- a/src/Annotate/Options.php +++ b/src/Annotate/Options.php @@ -19,12 +19,11 @@ class Options extends AbstractRequestMethod implements InjectRouteInterface /** * @param string $path */ - public function __construct(readonly public string $path) + public function __construct(readonly public string $path, readonly public string $version = 'v1') { } - /** * @param object $class * @param string $method @@ -35,7 +34,9 @@ class Options extends AbstractRequestMethod implements InjectRouteInterface public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute(RequestMethod::REQUEST_OPTIONS, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_OPTIONS, $path, [$class, $method]); } diff --git a/src/Annotate/Post.php b/src/Annotate/Post.php index bbe281f..bb059e4 100644 --- a/src/Annotate/Post.php +++ b/src/Annotate/Post.php @@ -14,12 +14,11 @@ class Post extends AbstractRequestMethod implements InjectRouteInterface { - - /** * @param string $path + * @param string $version */ - public function __construct(readonly public string $path) + public function __construct(readonly public string $path, readonly public string $version = 'v1') { } @@ -34,7 +33,9 @@ class Post extends AbstractRequestMethod implements InjectRouteInterface public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute(RequestMethod::REQUEST_POST, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_POST, $path, [$class, $method]); } diff --git a/src/Annotate/Put.php b/src/Annotate/Put.php index 6872088..4bf5360 100644 --- a/src/Annotate/Put.php +++ b/src/Annotate/Put.php @@ -19,7 +19,7 @@ class Put extends AbstractRequestMethod implements InjectRouteInterface /** * @param string $path */ - public function __construct(readonly public string $path) + public function __construct(readonly public string $path, readonly public string $version = 'v1') { } @@ -34,7 +34,9 @@ class Put extends AbstractRequestMethod implements InjectRouteInterface public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute(RequestMethod::REQUEST_PUT, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute(RequestMethod::REQUEST_PUT, $path, [$class, $method]); } } diff --git a/src/Annotate/Route.php b/src/Annotate/Route.php index eb66129..efac4fa 100644 --- a/src/Annotate/Route.php +++ b/src/Annotate/Route.php @@ -5,6 +5,7 @@ namespace Kiri\Router\Annotate; use Kiri\Router\Constrict\RequestMethod; use Kiri\Router\Interface\InjectRouteInterface; use Kiri\Router\Router; +use ReflectionException; #[\Attribute(\Attribute::TARGET_METHOD)] class Route extends AbstractRequestMethod implements InjectRouteInterface @@ -15,7 +16,7 @@ class Route extends AbstractRequestMethod implements InjectRouteInterface * @param string $path * @param RequestMethod $method */ - public function __construct(readonly public string $path, readonly public RequestMethod $method) + public function __construct(readonly public string $path, readonly public RequestMethod $method, readonly public string $version = 'v1') { } @@ -24,11 +25,13 @@ class Route extends AbstractRequestMethod implements InjectRouteInterface * @param object $class * @param string $method * @return void - * @throws \ReflectionException + * @throws ReflectionException */ public function dispatch(object $class, string $method): void { // TODO: Implement dispatch() method. - Router::addRoute($this->method, $this->path, [$class, $method]); + $path = $this->version . '/' . ltrim($this->path, '/'); + + Router::addRoute([$this->method], $path, [$class, $method]); } } diff --git a/src/Aspect/Aspect.php b/src/Aspect/Aspect.php new file mode 100644 index 0000000..bc215b1 --- /dev/null +++ b/src/Aspect/Aspect.php @@ -0,0 +1,67 @@ +create(ParserFactory::PREFER_PHP7); + $ast = $parser->parse(file_get_contents($fileName)); + + $cacheFile = storage('proxy_functions.php'); + if (!file_exists($cacheFile)) { + file_put_contents($cacheFile, 'generateClourse($functionName) . PHP_EOL; + + file_put_contents($cacheFile, $code, FILE_APPEND); + } catch (\Throwable $throwable) { + die(throwable($throwable)); + } + } + + + private function generateClourse($functionName): string + { + return <<getFunctionParams($method); - - return new Handler($method, $reflection); + $reflection = new \ReflectionFunction($method); + if ($reflection->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') { + throw new Exception('Request Handler returns must implements on Psr\Http\Message\ResponseInterface'); + } + $params = \Kiri::getDi()->resolveMethodParams($reflection); + return new Handler($method, $params); } @@ -63,6 +70,7 @@ class ControllerInterpreter * @param ReflectionClass $reflectionClass * @return Handler * @throws ReflectionException + * @throws Exception */ public function resolveMethod(object $class, string|\ReflectionMethod $reflectionMethod, ReflectionClass $reflectionClass): Handler { @@ -70,10 +78,26 @@ class ControllerInterpreter $reflectionMethod = $reflectionClass->getMethod($reflectionMethod); } + if ($reflectionMethod->getReturnType()->getName() !== 'Psr\Http\Message\ResponseInterface') { + throw new Exception('Request Handler returns must implements on Psr\Http\Message\ResponseInterface'); + } + $container = \Kiri::getDi(); $parameters = $container->getMethodParams($reflectionMethod); - return new Handler([$class, $reflectionMethod->getName()], $parameters); + $method = $reflectionMethod->getName(); + + $call = static function (RequestInterface $request) use ($class, $method, $parameters) { + /** @var ResponseInterface $response */ + $response = \Kiri::service()->get('response'); + if (!$class->beforeAction($request)) { + return $response->withStatus(500); + } + $response = call_user_func([$class, $method], $parameters); + $class->afterAction($response); + return $response; + }; + return new Handler($call, \Kiri::service()->get('request')); } } diff --git a/src/Handler.php b/src/Handler.php index c47e344..6b5ab48 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -62,13 +62,7 @@ class Handler implements RequestHandlerInterface public function handle(ServerRequestInterface $request): ResponseInterface { // TODO: Implement handle() method. - $result = call_user_func($this->handler, ...$this->parameter); - if ($result instanceof ResponseInterface) { - return $result; - } else { - $response = \Kiri::getDi()->get(ResponseInterface::class); - return $response->rewrite(); - } + return call_user_func($this->handler, ...$this->parameter); } }