From 74532d2509796e086d3ed5add31a2a8b71dc63f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 27 Jul 2021 17:27:56 +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/Route/Node.php | 13 +++++----- System/Error/LoggerAspect.php | 49 ++++++++++++++++------------------- System/IAspect.php | 8 +----- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 0d4c5ccf..29f9e8bd 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -12,6 +12,7 @@ use HttpServer\Abstracts\HttpService; use HttpServer\Http\Context; use HttpServer\Http\Request; use JetBrains\PhpStorm\Pure; +use ReflectionClass; use ReflectionException; use Snowflake\Aop; use Snowflake\Core\Json; @@ -141,18 +142,18 @@ class Node extends HttpService /** - * @param $aop + * @param ReflectionClass $reflect * @param $application * @return Closure + * @throws ReflectionException|NotFindClassException */ - private function aopHandler($aop, $application): Closure + private function aopHandler(ReflectionClass $reflect, $application): Closure { - $reflect = $aop->getAop($this->handler); $callback = [$reflect->getMethod('invoke'), 'invokeArgs']; - $instance = $reflect->newInstance($application->handler); + $instance = Snowflake::getDi()->get($reflect->getName()); return static function () use ($callback, $application, $instance, $reflect) { - call_user_func($callback, $instance, ...func_get_args()); + call_user_func($callback, $instance); }; } @@ -164,7 +165,7 @@ class Node extends HttpService private function normalHandler($application): Closure { return static function () use ($application) { - return call_user_func($application->handler, ...func_get_args()); + return call_user_func($application->handler); }; } diff --git a/System/Error/LoggerAspect.php b/System/Error/LoggerAspect.php index 37925973..7d9961f1 100644 --- a/System/Error/LoggerAspect.php +++ b/System/Error/LoggerAspect.php @@ -4,7 +4,6 @@ namespace Snowflake\Error; -use JetBrains\PhpStorm\Pure; use Snowflake\IAspect; @@ -15,43 +14,39 @@ use Snowflake\IAspect; class LoggerAspect implements IAspect { - private string $className = ''; - private string $methodName = ''; - - - /** - * LoggerAspect constructor. - * @param array $handler - */ - #[Pure] public function __construct(public array $handler) - { - } + private string $className = ''; + private string $methodName = ''; /** + * @param mixed $handler * @return mixed */ - public function invoke(): mixed - { - $startTime = microtime(true); + public function invoke(mixed $handler): mixed + { + $startTime = microtime(true); - $data = call_user_func($this->handler, func_get_args()); + $data = call_user_func($handler); - $this->print_runtime($startTime); + $this->print_runtime($handler, $startTime); - return $data; - } + return $data; + } - private function print_runtime($startTime) - { - $className = $this->handler[0]::class; - $methodName = $this->handler[1]; + /** + * @param $handler + * @param $startTime + */ + private function print_runtime($handler, $startTime) + { + $className = $handler::class; + $methodName = $handler; - $runTime = round(microtime(true) - $startTime, 6); - echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime); - echo PHP_EOL; - } + $runTime = round(microtime(true) - $startTime, 6); + echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime); + echo PHP_EOL; + } } diff --git a/System/IAspect.php b/System/IAspect.php index 850fc877..e946095d 100644 --- a/System/IAspect.php +++ b/System/IAspect.php @@ -8,16 +8,10 @@ interface IAspect { - /** - * IAspect constructor. - * @param array $handler - */ - public function __construct(array $handler); - /** * @return mixed|void */ - public function invoke(); + public function invoke(mixed $handler); }