2021-03-29 03:09:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2021-08-11 01:04:57 +08:00
|
|
|
namespace Kiri\Error;
|
2021-03-29 03:09:55 +08:00
|
|
|
|
|
|
|
|
|
2021-09-06 11:28:59 +08:00
|
|
|
use Exception;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\IAspect;
|
2021-03-29 03:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class LoggerAspect
|
2021-08-11 01:04:57 +08:00
|
|
|
* @package Kiri\Error
|
2021-03-29 03:09:55 +08:00
|
|
|
*/
|
|
|
|
|
class LoggerAspect implements IAspect
|
|
|
|
|
{
|
|
|
|
|
|
2021-09-06 11:28:59 +08:00
|
|
|
private float $time;
|
2021-03-29 03:09:55 +08:00
|
|
|
|
|
|
|
|
|
2021-03-29 16:17:22 +08:00
|
|
|
/**
|
2021-07-27 17:27:56 +08:00
|
|
|
* @param mixed $handler
|
2021-08-02 18:12:32 +08:00
|
|
|
* @param array $params
|
2021-03-29 16:17:22 +08:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2021-08-02 18:12:32 +08:00
|
|
|
public function invoke(mixed $handler, array $params = []): mixed
|
2021-07-27 17:27:56 +08:00
|
|
|
{
|
2021-09-06 11:28:59 +08:00
|
|
|
return call_user_func($handler, ...$params);
|
2021-07-27 17:27:56 +08:00
|
|
|
}
|
2021-03-29 03:09:55 +08:00
|
|
|
|
2021-04-23 03:12:00 +08:00
|
|
|
|
2021-07-27 17:27:56 +08:00
|
|
|
/**
|
|
|
|
|
* @param $startTime
|
2021-09-06 11:28:59 +08:00
|
|
|
* @throws Exception
|
2021-07-27 17:27:56 +08:00
|
|
|
*/
|
2021-09-06 11:28:59 +08:00
|
|
|
private function print_runtime($startTime)
|
2021-07-27 17:27:56 +08:00
|
|
|
{
|
|
|
|
|
$runTime = round(microtime(true) - $startTime, 6);
|
2021-09-06 11:28:59 +08:00
|
|
|
echo sprintf('run %s use time %6f', request()->getUri()->__toString(), $runTime);
|
2021-07-27 17:27:56 +08:00
|
|
|
echo PHP_EOL;
|
|
|
|
|
}
|
2021-03-29 03:09:55 +08:00
|
|
|
|
|
|
|
|
|
2021-09-06 11:26:22 +08:00
|
|
|
public function before(): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement before() method.
|
2021-09-06 11:28:59 +08:00
|
|
|
$this->time = microtime(true);
|
2021-09-06 11:26:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-06 11:28:59 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-09-06 11:26:22 +08:00
|
|
|
public function after(mixed $response): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement after() method.
|
2021-09-06 11:28:59 +08:00
|
|
|
$this->print_runtime($this->time);
|
2021-09-06 11:26:22 +08:00
|
|
|
}
|
2021-03-29 03:09:55 +08:00
|
|
|
}
|