This commit is contained in:
2021-07-27 17:27:56 +08:00
parent 6bb24e5b42
commit 74532d2509
3 changed files with 30 additions and 40 deletions
+7 -6
View File
@@ -12,6 +12,7 @@ use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionClass;
use ReflectionException; use ReflectionException;
use Snowflake\Aop; use Snowflake\Aop;
use Snowflake\Core\Json; use Snowflake\Core\Json;
@@ -141,18 +142,18 @@ class Node extends HttpService
/** /**
* @param $aop * @param ReflectionClass $reflect
* @param $application * @param $application
* @return Closure * @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']; $callback = [$reflect->getMethod('invoke'), 'invokeArgs'];
$instance = $reflect->newInstance($application->handler); $instance = Snowflake::getDi()->get($reflect->getName());
return static function () use ($callback, $application, $instance, $reflect) { 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 private function normalHandler($application): Closure
{ {
return static function () use ($application) { return static function () use ($application) {
return call_user_func($application->handler, ...func_get_args()); return call_user_func($application->handler);
}; };
} }
+22 -27
View File
@@ -4,7 +4,6 @@
namespace Snowflake\Error; namespace Snowflake\Error;
use JetBrains\PhpStorm\Pure;
use Snowflake\IAspect; use Snowflake\IAspect;
@@ -15,43 +14,39 @@ use Snowflake\IAspect;
class LoggerAspect implements IAspect class LoggerAspect implements IAspect
{ {
private string $className = ''; private string $className = '';
private string $methodName = ''; private string $methodName = '';
/**
* LoggerAspect constructor.
* @param array $handler
*/
#[Pure] public function __construct(public array $handler)
{
}
/** /**
* @param mixed $handler
* @return mixed * @return mixed
*/ */
public function invoke(): mixed public function invoke(mixed $handler): mixed
{ {
$startTime = microtime(true); $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) /**
{ * @param $handler
$className = $this->handler[0]::class; * @param $startTime
$methodName = $this->handler[1]; */
private function print_runtime($handler, $startTime)
{
$className = $handler::class;
$methodName = $handler;
$runTime = round(microtime(true) - $startTime, 6); $runTime = round(microtime(true) - $startTime, 6);
echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime); echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime);
echo PHP_EOL; echo PHP_EOL;
} }
} }
+1 -7
View File
@@ -8,16 +8,10 @@ interface IAspect
{ {
/**
* IAspect constructor.
* @param array $handler
*/
public function __construct(array $handler);
/** /**
* @return mixed|void * @return mixed|void
*/ */
public function invoke(); public function invoke(mixed $handler);
} }