Files
kiri-core/System/Error/LoggerAspect.php
T

57 lines
1012 B
PHP
Raw Normal View History

2021-03-29 03:09:55 +08:00
<?php
namespace Snowflake\Error;
2021-03-29 16:17:22 +08:00
use JetBrains\PhpStorm\Pure;
2021-03-29 03:09:55 +08:00
use Snowflake\IAspect;
/**
* Class LoggerAspect
* @package Snowflake\Error
*/
class LoggerAspect implements IAspect
{
private string $className = '';
private string $methodName = '';
/**
* LoggerAspect constructor.
* @param array $handler
*/
2021-03-29 16:17:22 +08:00
#[Pure] public function __construct(public array $handler)
2021-03-29 03:09:55 +08:00
{
$this->className = get_class($this->handler[0]);
$this->methodName = $this->handler[1];
}
2021-03-29 16:17:22 +08:00
/**
* @return mixed
*/
public function invoke(): mixed
2021-03-29 03:09:55 +08:00
{
$startTime = microtime(true);
$data = call_user_func($this->handler, func_get_args());
$this->print_runtime($startTime);
2021-03-29 16:17:22 +08:00
2021-03-29 03:09:55 +08:00
return $data;
}
private function print_runtime($startTime)
{
$runTime = round(microtime(true) - $startTime, 6);
echo sprintf('run %s::%s use time %6f', $this->className, $this->methodName, $runTime);
echo PHP_EOL;
}
}