<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* provider.php
|
|
* Created by PhpStorm.
|
|
* Date: 2026/10/17
|
|
* Time: 09:40
|
|
* Author: qian.guo
|
|
* E-mail: yiyezhiqiutel@gmail.com
|
|
*/
|
|
|
|
use app\ExceptionHandle;
|
|
use app\Request;
|
|
use app\service\JwtService;
|
|
use app\service\ScoreService;
|
|
use app\service\score\ScoreEngine;
|
|
use app\service\score\ScoreStrategyFactory;
|
|
use app\service\WxService;
|
|
use think\Container;
|
|
|
|
/**
|
|
* 容器绑定
|
|
*
|
|
* 文件结构 = 官方骨架的两条约定绑定 + 本项目服务绑定。
|
|
*
|
|
* ⚠️ 前两条是官方内容,**不能删**:
|
|
* - 不绑 think\exception\Handle → app\ExceptionHandle 完全不生效,
|
|
* 异常走框架默认 HTML 报错页,前端拿到 HTML 而不是 JSON,
|
|
* 统一响应体 / 错误码 / 中文 msg 全部作废
|
|
* - 不绑 think\Request → 控制器拿到的是框架原生 Request
|
|
* 早期重写本文件时曾把这两条删掉,静态检查全绿但运行时静默失效。
|
|
* 校验器已加一道检查专门盯这两条。
|
|
*
|
|
* 业务绑定只放需要「配置注入」的服务:
|
|
* 评分策略不在容器里绑 —— 策略由 ScoreStrategyFactory 按 config/score.php
|
|
* 注册表实例化,在容器里再绑一次会让「换策略」出现两个入口。
|
|
* 模型一律用静态方法调用(规范 8.3),不注册到容器。
|
|
*/
|
|
return [
|
|
/* ---------------- 官方约定绑定(不可删) ---------------- */
|
|
|
|
'think\Request' => Request::class,
|
|
'think\exception\Handle' => ExceptionHandle::class,
|
|
|
|
/* ---------------- 评分引擎 ---------------- */
|
|
|
|
ScoreStrategyFactory::class => fn () => new ScoreStrategyFactory(config('score')),
|
|
|
|
ScoreEngine::class => fn (Container $app) => new ScoreEngine(
|
|
$app->make(ScoreStrategyFactory::class)
|
|
),
|
|
|
|
// 评分引擎的对外门面:控制器只注入它,拿不到策略细节
|
|
ScoreService::class => fn (Container $app) => new ScoreService(
|
|
$app->make(ScoreEngine::class)
|
|
),
|
|
|
|
/* ---------------- 账号体系 ---------------- */
|
|
|
|
JwtService::class => fn () => new JwtService(
|
|
(string) config('jwt.secret'),
|
|
(int) config('jwt.ttl')
|
|
),
|
|
|
|
// 第二个参数是调试标志:WxService 用它拦住「生产开着 mock」这种致命配置
|
|
WxService::class => fn () => new WxService(
|
|
(array) config('wx'),
|
|
(bool) env('app_debug', false)
|
|
),
|
|
];
|