Files
chatroom/app/Providers/AppServiceProvider.php

66 lines
2.2 KiB
PHP

<?php
/**
* 文件功能:应用服务提供者
*
* 负责注册和引导应用级服务:自定义 SMTP 配置动态加载、
* 婚姻系统消息事件订阅者注册等。
*/
namespace App\Providers;
use App\Listeners\SaveMarriageSystemMessage;
use App\Models\Sysparam;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// 注册婚姻系统消息订阅者(结婚/婚礼/离婚通知写入聊天历史)
Event::subscribe(SaveMarriageSystemMessage::class);
// 动态加载自定义 SMTP 配置 (如果有数据库则执行)
try {
if (Schema::hasTable('sysparam')) {
$smtpConfig = Sysparam::where('alias', 'like', 'smtp_%')->pluck('body', 'alias');
if ($smtpConfig->isNotEmpty() && $smtpConfig->get('smtp_host')) {
Config::set('mail.default', 'smtp');
Config::set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => $smtpConfig->get('smtp_host'),
'port' => $smtpConfig->get('smtp_port', 465),
'encryption' => $smtpConfig->get('smtp_encryption', 'ssl'),
'username' => $smtpConfig->get('smtp_username'),
'password' => $smtpConfig->get('smtp_password'),
'timeout' => 10,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
]);
Config::set('mail.from', [
'address' => $smtpConfig->get('smtp_from_address', $smtpConfig->get('smtp_username')),
'name' => $smtpConfig->get('smtp_from_name', '飘落流星聊天室'),
]);
}
}
} catch (\Exception $e) {
// 在安装初期表不存在时忽略,防止应用崩溃
}
}
}