# notify **Repository Path**: fiberphp/notify ## Basic Information - **Project Name**: notify - **Description**: 📧 FiberPHP 通知组件 —— 支持邮件、短信、钉钉、飞书等多渠道消息推送,统一 API,开箱即用。 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-23 - **Last Updated**: 2026-11-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FiberPHP Notify 多通道通知系统,零框架依赖的纯库实现。内置邮件(PHP `mail()`)、日志、Slack(Incoming Webhook)三种通道,通过 `Notifiable` / `Notification` 抽象声明投递目标与内容,可按通知自身声明的通道分发。 ## 环境要求 - PHP >= 8.5 - Slack 通道可选依赖 `fiberphp/http-client`(未安装时静默跳过) - 日志通道可选依赖宿主 `logger()` 助手(纯库环境无则静默丢弃) ## 安装 ```bash composer require fiberphp/notify ``` ## 快速开始 ```php use FiberPHP\Notify\NotifyManager; use FiberPHP\Notify\Notifiable; use FiberPHP\Notify\Notification; // 可通知对象:返回各通道路由信息 class User implements Notifiable { public function routeNotificationFor(string $channel): mixed { return match ($channel) { 'email' => 'tom@example.com', 'slack' => 'https://hooks.slack.com/services/xxx', default => null, }; } } // 通知:声明投递通道与数据 class OrderShipped extends Notification { public function via(Notifiable $n): array { return ['mail', 'slack']; } public function toArray(Notifiable $n): array { return ['subject' => '订单已发货', 'message' => '您的订单 #A2026 已发出']; } } // 构造注入管理器(框架项目传 (array) config('notify')) $manager = new NotifyManager([ 'default' => 'mail', 'channels' => [ 'mail' => ['driver' => 'email', 'from' => 'noreply@example.com'], 'slack' => ['driver' => 'slack', 'webhook_url' => 'https://hooks.slack.com/...'], ], ]); // 按通知声明的通道分发 $manager->send(new User(), new OrderShipped()); // 显式指定通道 $manager->channel('mail')->send(new User(), new OrderShipped()); ``` ## 配置说明 配置结构(同 `config/notify.php`): ```php return [ 'default' => 'mail', 'channels' => [ 'mail' => ['driver' => 'email', 'from' => '...', 'to' => '...'], 'log' => ['driver' => 'log', 'channel' => 'default', 'level' => 'info'], 'slack' => ['driver' => 'slack', 'webhook_url' => '...', 'timeout' => 10], ], ]; ``` | 通道 | 驱动 | 关键配置 | |-------|---------|----------------------------------| | email | `email` | `from` / `to` / `headers` | | log | `log` | `channel`(日志通道名)/ `level` | | slack | `slack` | `webhook_url` / `timeout` | ## 特性 | 能力 | 说明 | |------------|------------------------------------------------------------------------| | 多通道 | email / log / slack 三种内置驱动,按 `driver` 字段构建 | | 惰性构建 | 通道实例首次使用时构建并缓存复用 | | 按通知分发 | `send()` 按 `Notification::via()` 声明的通道逐一投递 | | 方法转发 | `__call` 转发到默认通道,保持 `$manager->send(...)` 简写语义 | | 通道覆盖 | `toLog` / `toEmail` / `toSlack` 默认回退 `toArray()`,子类可按通道定制 | ## License MIT License (c) 2026 庞斌,详见 [LICENSE](LICENSE)。