This commit is contained in:
72
app/Listener/AutoMainFieldsListener.php
Executable file
72
app/Listener/AutoMainFieldsListener.php
Executable file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Listener;
|
||||
|
||||
use App\Context\QueueContext;
|
||||
use App\Context\UserContext;
|
||||
use Hyperf\Database\Model\Events\Creating;
|
||||
use Hyperf\Database\Model\Events\Saving;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/05/24
|
||||
* Time: 下午4:27
|
||||
* Description: 自动填充模型字段监听器,用于自动填充模型字段。
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
#[Listener]
|
||||
class AutoMainFieldsListener implements ListenerInterface
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
Creating::class,
|
||||
Saving::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function process(object $event): void
|
||||
{
|
||||
/**
|
||||
* 在模型创建或保存事件中,自动设置模型的company_id和creator_id等相关字段。
|
||||
* 这个逻辑只在用户上下文存在,且事件为Creating或Saving时触发。
|
||||
*
|
||||
* @param object $event 事件对象,预期为Creating或Saving事件之一。
|
||||
* @return void
|
||||
*/
|
||||
|
||||
$user = QueueContext::getUser() ?? UserContext::getCurrentUser();
|
||||
$company = $user['company'] ?? [];
|
||||
|
||||
$model = $event->getModel();
|
||||
// 判断事件类型是否符合条件,以及用户上下文是否存在
|
||||
if (!($event instanceof Creating || $event instanceof Saving) || !$user) {
|
||||
return;
|
||||
}
|
||||
$schema = $model->getConnection()->getSchemaBuilder();
|
||||
|
||||
// 检查模型表是否有company_id字段,若有,则设置company_id
|
||||
if ($schema->hasColumn($model->getTable(), 'company_id')) {
|
||||
$model->company_id = $model->company_id ?? $company['id'] ?? 0;
|
||||
}
|
||||
|
||||
// 检查模型表是否有creator_id和creator_name字段,若有,则设置对应的值
|
||||
if ($schema->hasColumn($model->getTable(), 'creator_id')) {
|
||||
$model->creator_id ??= $user['id'];
|
||||
$model->creator_name ??= $user['name'] ?? '';
|
||||
}
|
||||
}
|
||||
}
|
80
app/Listener/DbQueryExecutedListener.php
Normal file
80
app/Listener/DbQueryExecutedListener.php
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
74
app/Listener/QueueHandleListener.php
Normal file
74
app/Listener/QueueHandleListener.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?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\AsyncQueue\AnnotationJob;
|
||||
use Hyperf\AsyncQueue\Event\AfterHandle;
|
||||
use Hyperf\AsyncQueue\Event\BeforeHandle;
|
||||
use Hyperf\AsyncQueue\Event\Event;
|
||||
use Hyperf\AsyncQueue\Event\FailedHandle;
|
||||
use Hyperf\AsyncQueue\Event\RetryHandle;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
#[Listener]
|
||||
class QueueHandleListener implements ListenerInterface
|
||||
{
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->logger = $container->get(LoggerFactory::class)->get('queue');
|
||||
}
|
||||
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
AfterHandle::class,
|
||||
BeforeHandle::class,
|
||||
FailedHandle::class,
|
||||
RetryHandle::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function process(object $event): void
|
||||
{
|
||||
if ($event instanceof Event && $event->getMessage()->job()) {
|
||||
$job = $event->getMessage()->job();
|
||||
$jobClass = get_class($job);
|
||||
if ($job instanceof AnnotationJob) {
|
||||
$jobClass = sprintf('Job[%s@%s]', $job->class, $job->method);
|
||||
}
|
||||
$date = date('Y-m-d H:i:s');
|
||||
|
||||
switch (true) {
|
||||
case $event instanceof BeforeHandle:
|
||||
$this->logger->info(sprintf('[%s] Processing %s.', $date, $jobClass));
|
||||
break;
|
||||
case $event instanceof AfterHandle:
|
||||
$this->logger->info(sprintf('[%s] Processed %s.', $date, $jobClass));
|
||||
break;
|
||||
case $event instanceof FailedHandle:
|
||||
$this->logger->error(sprintf('[%s] Failed %s.', $date, $jobClass));
|
||||
$this->logger->error((string) $event->getThrowable());
|
||||
break;
|
||||
case $event instanceof RetryHandle:
|
||||
$this->logger->warning(sprintf('[%s] Retried %s.', $date, $jobClass));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
app/Listener/ResumeExitCoordinatorListener.php
Normal file
35
app/Listener/ResumeExitCoordinatorListener.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?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\Command\Event\AfterExecute;
|
||||
use Hyperf\Coordinator\Constants;
|
||||
use Hyperf\Coordinator\CoordinatorManager;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
|
||||
#[Listener]
|
||||
class ResumeExitCoordinatorListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
{
|
||||
return [
|
||||
AfterExecute::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function process(object $event): void
|
||||
{
|
||||
CoordinatorManager::until(Constants::WORKER_EXIT)->resume();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user