PHP code example of tgkw-adc / helper
1. Go to this page and download the library: Download tgkw-adc/helper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tgkw-adc / helper example snippets
use function TgkwAdc\cfg;
use function TgkwAdc\redis;
use function TgkwAdc\toRmb;
use function TgkwAdc\math_add;
// 获取配置
$config = cfg('app.name', 'default');
// 获取 Redis 实例
$redis = redis('default');
// 数字转中文大写金额
echo toRmb(123.45); // 壹佰贰拾叁元肆角伍分
// 精确数学运算
echo math_add('0.1', '0.2'); // 0.30
use TgkwAdc\Helper\ApiResponseHelper;
// 成功响应
return ApiResponseHelper::success($data, '操作成功');
// 错误响应
return ApiResponseHelper::error('参数错误', $errors, null, 400);
use TgkwAdc\Helper\JwtHelper;
// 初始化
JwtHelper::init();
// 生成令牌
$token = JwtHelper::createToken(['user_id' => 123], 3600);
// 解析令牌
$payload = JwtHelper::parseToken($token);
// 从请求获取载荷
$payload = JwtHelper::getPayloadFromRequest($request);
use TgkwAdc\Helper\Log\LogHelper;
// 基本日志
LogHelper::info('用户登录', ['user_id' => 123]);
LogHelper::error('操作失败', ['error' => '数据库连接超时']);
// 业务日志
LogHelper::business('订单创建', ['order_id' => 456], 'business', 'order-create');
// 异常日志
LogHelper::exception($exception, '处理异常', ['user_id' => 123]);
use TgkwAdc\Annotation\EnumCode;
use TgkwAdc\Annotation\EnumCodePrefix;
use TgkwAdc\Trait\EnumCodeGet;
#[EnumCodePrefix(prefixCode: 1000)]
enum UserStatus: int
{
use EnumCodeGet;
#[EnumCode('正常', ['en' => 'Normal', 'zh_hk' => '正常'])]
case NORMAL = 1;
#[EnumCode('禁用', ['en' => 'Disabled', 'zh_hk' => '禁用'])]
case DISABLED = 2;
}
// 使用
$status = UserStatus::NORMAL;
echo $status->getMsg(); // 正常
echo $status->getCode(); // 100001
echo $status->getI18nMsg('en'); // Normal
use TgkwAdc\Middleware\LocaleMiddleware;
// 在 config/autoload/middlewares.php 中注册
return [
'http' => [
LocaleMiddleware::class,
],
];
use TgkwAdc\Middleware\TraceIdMiddleware;
// 自动为每个请求生成唯一追踪ID
use TgkwAdc\Resource\BaseResource;
class UserResource extends BaseResource
{
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->formatDate($this->created_at),
];
}
}
// 使用
return new UserResource($user)
->withMeta(['total' => 100])
->withStats(['views' => 50]);
// 按模块分类
LogHelper::info('订单创建', ['order_id' => 456], 'business', 'order-create');
// 按日期分类
$date = date('Y-m-d');
LogHelper::info('每日统计', ['count' => 100], 'statistics', 'default', "daily-{$date}");
// 按用户分类
LogHelper::info('用户操作', ['action' => 'login'], 'user', 'default', "user-{$userId}");
use TgkwAdc\Constants\LocaleConstants;
// 支持的语言
$locales = LocaleConstants::getSupportedLocaleCodes();
// ['zh' => 'zh_CN', 'en' => 'en_US', 'zh_hk' => 'zh_HK', 'zh_tw' => 'zh_TW']
// 默认语言
$default = LocaleConstants::getDefaultLocale(); // zh_CN
use TgkwAdc\Helper\Intl\I18nHelper;
// 获取当前语言
$locale = I18nHelper::getNowLang();
// 获取翻译文本
$text = I18nHelper::getText('welcome.message', ['name' => 'John']);
// config/autoload/jwt.php
return [
'secret' => env('JWT_SECRET', 'your-secret-key'),
'alg' => env('JWT_ALG', 'HS256'),
];
// config/autoload/logger.php
return [
'default' => [
'handler' => [
'class' => Monolog\Handler\RotatingFileHandler::class,
'constructor' => [
'filename' => BASE_PATH . '/runtime/logs/app.log',
'level' => Monolog\Level::Info,
'maxFiles' => 30,
],
],
'formatter' => [
'class' => TgkwAdc\Helper\Log\CustomJsonFormatter::class,
],
'processors' => [
TgkwAdc\Helper\Log\AppendRequestIdProcessor::class,
],
],
];
bash
cp vendor/tgkw-adc/helper/src/Helper/Log/logger.example.php config/autoload/logger.php
bash
vendor/bin/php-cs-fixer fix src