This commit is contained in:
2021-09-06 11:28:59 +08:00
parent 3088530d9d
commit 93068ab224
+11 -15
View File
@@ -4,6 +4,7 @@
namespace Kiri\Error; namespace Kiri\Error;
use Exception;
use Kiri\IAspect; use Kiri\IAspect;
@@ -14,8 +15,7 @@ use Kiri\IAspect;
class LoggerAspect implements IAspect class LoggerAspect implements IAspect
{ {
private string $className = ''; private float $time;
private string $methodName = '';
/** /**
@@ -25,27 +25,18 @@ class LoggerAspect implements IAspect
*/ */
public function invoke(mixed $handler, array $params = []): mixed public function invoke(mixed $handler, array $params = []): mixed
{ {
$startTime = microtime(true); return call_user_func($handler, ...$params);
$data = call_user_func($handler, ...$params);
$this->print_runtime($handler, $startTime);
return $data;
} }
/** /**
* @param $handler
* @param $startTime * @param $startTime
* @throws Exception
*/ */
private function print_runtime($handler, $startTime) private function print_runtime($startTime)
{ {
$className = $handler[0]::class;
$methodName = $handler[1];
$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 use time %6f', request()->getUri()->__toString(), $runTime);
echo PHP_EOL; echo PHP_EOL;
} }
@@ -53,11 +44,16 @@ class LoggerAspect implements IAspect
public function before(): void public function before(): void
{ {
// TODO: Implement before() method. // TODO: Implement before() method.
$this->time = microtime(true);
} }
/**
* @throws Exception
*/
public function after(mixed $response): void public function after(mixed $response): void
{ {
// TODO: Implement after() method. // TODO: Implement after() method.
$this->print_runtime($this->time);
} }
} }