From 3088530d9d1d7f839cf0d71e3b1b1505b10297d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 6 Sep 2021 11:26:22 +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 --- core/Error/LoggerAspect.php | 10 ++ core/IAspect.php | 11 +- http-helper/Route/Pipeline.php | 280 +++++++++++++++++---------------- 3 files changed, 164 insertions(+), 137 deletions(-) diff --git a/core/Error/LoggerAspect.php b/core/Error/LoggerAspect.php index c4bd2980..5be5e0c7 100644 --- a/core/Error/LoggerAspect.php +++ b/core/Error/LoggerAspect.php @@ -50,4 +50,14 @@ class LoggerAspect implements IAspect } + public function before(): void + { + // TODO: Implement before() method. + } + + + public function after(mixed $response): void + { + // TODO: Implement after() method. + } } diff --git a/core/IAspect.php b/core/IAspect.php index a60527bb..fdba4136 100644 --- a/core/IAspect.php +++ b/core/IAspect.php @@ -8,11 +8,20 @@ interface IAspect { + public function before(): void; + + + /** + * @param mixed $response + */ + public function after(mixed $response): void; + + /** * @param mixed $handler * @param array $params * @return mixed */ - public function invoke(mixed $handler, array $params = []): mixed; + public function invoke(mixed $handler, array $params = []): mixed; } diff --git a/http-helper/Route/Pipeline.php b/http-helper/Route/Pipeline.php index 57b45ec5..4f135d62 100644 --- a/http-helper/Route/Pipeline.php +++ b/http-helper/Route/Pipeline.php @@ -6,168 +6,176 @@ use Annotation\Aspect; use Closure; use Http\IInterface\MiddlewareInterface; use Kiri\Di\NoteManager; +use Kiri\IAspect; use Kiri\Kiri; +use ReflectionException; use Throwable; class Pipeline { - protected $passable; + protected mixed $passable; - protected $overall; + protected mixed $overall; - protected $pipes = []; + protected mixed $pipes = []; - protected $pipeline; + protected mixed $pipeline; - protected $exceptionHandler; + protected mixed $exceptionHandler; - /** - * 初始数据 - * @param $passable - * @return $this - */ - public function send($passable) - { - $this->passable = $passable; - return $this; - } + /** + * 初始数据 + * @param $passable + * @return $this + */ + public function send($passable): static + { + $this->passable = $passable; + return $this; + } - /** - * @param $middle - * @return $this - */ - public function overall($middle): static - { - $this->overall = $middle; - return $this; - } + /** + * @param $middle + * @return $this + */ + public function overall($middle): static + { + $this->overall = $middle; + return $this; + } - /** - * 调用栈 - * @param $pipes - * @return $this - */ - public function through($pipes) - { - if (empty($pipes)) return $this; - if (empty($this->pipes)) { - $this->pipes = is_array($pipes) ? $pipes : func_get_args(); - } else { - foreach ($pipes as $pipe) { - $this->pipes[] = $pipe; - } - } - return $this; - } + /** + * 调用栈 + * @param $pipes + * @return $this + */ + public function through($pipes): static + { + if (empty($pipes)) return $this; + if (empty($this->pipes)) { + $this->pipes = is_array($pipes) ? $pipes : func_get_args(); + } else { + foreach ($pipes as $pipe) { + $this->pipes[] = $pipe; + } + } + return $this; + } - /** - * 执行 - * @param Closure $destination - * @return mixed - */ - public function then(callable $destination) - { - $parameters = $this->passable; - if (!empty($this->overall)) { - array_unshift($this->pipes, $this->overall); - } - if (is_array($destination)) { - $destination = $this->aspect_caller($destination, $parameters); - } - $this->pipeline = array_reduce(array_reverse($this->pipes), $this->carry(), - static function () use ($destination, $parameters) { - return call_user_func($destination, ...$parameters); - } - ); - return $this->clear(); - } + /** + * 执行 + * @param callable $destination + * @return static + * @throws ReflectionException + */ + public function then(callable $destination): static + { + $parameters = $this->passable; + if (!empty($this->overall)) { + array_unshift($this->pipes, $this->overall); + } + if (is_array($destination)) { + $destination = $this->aspect_caller($destination, $parameters); + } + $this->pipeline = array_reduce(array_reverse($this->pipes), $this->carry(), + static function () use ($destination, $parameters) { + return call_user_func($destination, ...$parameters); + } + ); + return $this->clear(); + } - /** - * @return $this - */ - private function clear() - { - $this->pipes = []; - $this->passable = null; - $this->overall = null; - return $this; - } + /** + * @return $this + */ + private function clear(): static + { + $this->pipes = []; + $this->passable = null; + $this->overall = null; + return $this; + } - /** - * @param $destination - * @param $parameters - * @return \Closure - */ - private function aspect_caller($destination, $parameters) - { - [$controller, $action] = $destination; - /** @var \Annotation\Aspect $aop */ - $aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action); - if (!empty($aop)) { - $aop = Kiri::getDi()->get($aop->aspect); - $destination = static function () use ($aop, $destination, $parameters) { - /** @var \Kiri\IAspect $aop */ - return $aop->invoke($destination, $parameters); - }; - } - return $destination; - } + /** + * @param $destination + * @param $parameters + * @return Closure + * @throws ReflectionException + */ + private function aspect_caller($destination, $parameters): Closure + { + [$controller, $action] = $destination; + /** @var Aspect $aop */ + $aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action); + if (!empty($aop)) { + $aop = Kiri::getDi()->get($aop->aspect); + $destination = static function () use ($aop, $destination, $parameters) { + /** @var IAspect $aop */ + $aop->before(); + $data = $aop->invoke($destination, $parameters); + $aop->after($data); + return $data; + }; + } + return $destination; + } - /** - * @return mixed - */ - public function interpreter(): mixed - { - return call_user_func($this->pipeline, request()); - } + /** + * @return mixed + * @throws \Exception + */ + public function interpreter(): mixed + { + return call_user_func($this->pipeline, request()); + } - /** - * 设置异常处理器 - * @param callable $handler - * @return $this - */ - public function whenException($handler) - { - $this->exceptionHandler = $handler; - return $this; - } + /** + * 设置异常处理器 + * @param callable $handler + * @return $this + */ + public function whenException(callable $handler): static + { + $this->exceptionHandler = $handler; + return $this; + } - /** - * @return \Closure - */ - protected function carry(): Closure - { - return static function ($stack, $pipe) { - return static function ($passable) use ($stack, $pipe) { - if ($pipe instanceof MiddlewareInterface) { - $pipe = [$pipe, 'OnHandler']; - } - return $pipe($passable, $stack); - }; - }; - } + /** + * @return Closure + */ + protected function carry(): Closure + { + return static function ($stack, $pipe) { + return static function ($passable) use ($stack, $pipe) { + if ($pipe instanceof MiddlewareInterface) { + $pipe = [$pipe, 'OnHandler']; + } + return $pipe($passable, $stack); + }; + }; + } - /** - * 异常处理 - * @param $passable - * @param $e - * @return mixed - * @throws \Throwable - */ - protected function handleException($passable, Throwable $e) - { - if ($this->exceptionHandler) { - return call_user_func($this->exceptionHandler, $passable, $e); - } - throw $e; - } + /** + * 异常处理 + * @param $passable + * @param Throwable $e + * @return mixed + * @throws Throwable + */ + protected function handleException($passable, Throwable $e): mixed + { + if ($this->exceptionHandler) { + return call_user_func($this->exceptionHandler, $passable, $e); + } + throw $e; + } }