Files
hyperf_data/extend/Aether/PHP/Hyperf/AetherService.php
Aether 0920cef866 init
2025-09-18 10:46:54 +08:00

49 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace Aether;
use Aether\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Hyperf\DbConnection\Db;
use Psr\Log\LoggerInterface;
use Throwable;
abstract class AetherService
{
#[Inject]
protected LoggerFactory $loggerFactory;
/**
* 获取当前服务日志器
*/
protected function logger(): LoggerInterface
{
// return $this->loggerFactory->get(substr(strrchr(static::class, '\\'), 1));
// 提取类名如ExamTypeService作为日志通道名
$className = substr(strrchr(static::class, '\\'), 1);
return $this->loggerFactory->get($className);
}
/**
* 事务处理
* @throws Throwable
*/
protected function transaction(callable $callback)
{
return Db::transaction($callback);
}
/**
* 检查资源是否存在(不存在则抛出异常)
* @throws BusinessException
*/
protected function checkResourceExists(?object $resource, string $message = '资源不存在'): void
{
if (empty($resource)) {
throw new BusinessException($message, BusinessException::RESOURCE_NOT_FOUND);
}
}
}