协程版仓库后端项目
Some checks failed
Build Docker / build (push) Has been cancelled

This commit is contained in:
2025-07-08 14:59:47 +08:00
commit 0b2299c427
134 changed files with 19277 additions and 0 deletions

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Listener;
use Hyperf\Collection\Arr;
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
#[Listener]
class DbQueryExecutedListener implements ListenerInterface
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(ContainerInterface $container)
{
$this->logger = $container->get(LoggerFactory::class)->get('sql', 'sql');
}
public function listen(): array
{
return [
QueryExecuted::class,
];
}
/**
* @param object $event
*/
public function process(object $event): void
{
/**
* 处理QueryExecuted事件记录查询的SQL和执行时间。
*
* @param QueryExecuted $event 该事件对象包含执行的SQL语句、绑定参数和执行时间
*/
if ($event instanceof QueryExecuted) {
$sql = $event->sql; // 获取执行的SQL语句
// 检查绑定参数是否为关联数组,如果不是,将 "?" 替换为实际的参数值
if (!Arr::isAssoc($event->bindings)) {
$position = 0; // 初始化绑定参数的搜索位置
foreach ($event->bindings as $value) { // 遍历绑定参数
$position = strpos($sql, '?', $position); // 查找下一个 "?" 位置
if ($position === false) { // 如果找不到 "?",则退出循环
break;
}
$value = "'$value'"; // 将参数值包裹在单引号中
$sql = substr_replace($sql, $value, $position, 1); // 替换 "?" 为实际参数值
$position += strlen($value); // 更新搜索位置
}
}
// 使用logger记录SQL语句和执行时间
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
}
}
}