nikolaposa/notifier
Composer 安装命令:
composer require nikolaposa/notifier
包简介
Extensible library for building notifications and sending them via different delivery channels.
关键字:
README 文档
README
Extensible library for building notifications and sending them via different delivery channels.
Installation
The preferred method of installation is via Composer. Run the following command to install
the latest version of a package and add it to your project's composer.json:
composer require nikolaposa/notifier
Theory of operation
Notifications are informative messages that are sent through different channels (i.e. email, SMS, mobile push) to notify users about certain events in the application. Notification is a higher-level abstraction, a concept that encapsulates a subject to be notified to the recipient, regardless of delivery channels through which that information can be communicated. From an architectural standpoint, notification is a domain concern.
In order to minimize the coupling of your domain with the infrastructure for sending notifications, Notifier library was based on on unobtrusive interfaces that should be implemented by your objects in order to plug them into the workflow of the library. Those are:
Notification- marks the object as a notification that can be used with the Notifier library,Recipient- represents the recipient of the notification which provides contact (i.e. email address, phone number) for a certain channel; typically implemented by a User domain object.
For each channel through which Notification is supposed to be sent, Notification class should implement channel-specific
interface, making the Notification suitable for sending via a specific channel. These interfaces declare message
building methods, for example EmailNotification::toEmailMessage(), that convert the notification to a message sent by
that particular channel. Channel-specific Notification interfaces extend the Notification interface itself, so you do
not need to implement it explicitly.
Channel component captures implementation details of how a Notification is sent via certain delivery channels. Specific channel implementation typically consists of:
- channel-specific Notification interface,
- Message class - transport-level message to which Notification gets converted,
Channelimplementation responsible for the very act of sending the Notification.
Out of the box, this library features facilities for sending notifications via email and SMS. The highly extensible design allows for implementing custom delivery channels.
Finally, Notifier service is a facade that manages the entire process of sending a Notification to a list of
Recipients via supported channels. It is the only service of this library that the calling code is supposed to interact
with directly.
Usage
Creating Notifications
namespace App\Model; use Notifier\Channel\Email\EmailMessage; use Notifier\Channel\Sms\SmsMessage; use Notifier\Channel\Email\EmailNotification; use Notifier\Channel\Sms\SmsNotification; use Notifier\Recipient\Recipient; class TodoExpiredNotification implements EmailNotification, SmsNotification { /** @var Todo */ protected $todo; public function __construct(Todo $todo) { $this->todo = $todo; } public function toEmailMessage(Recipient $recipient): EmailMessage { return (new EmailMessage()) ->from('noreply@example.com') ->subject('Todo expired') ->htmlBody( 'Dear ' . $recipient->getRecipientName() . ',' . '<br><br>' . 'Your todo: <b>' . $this->todo->getText() . '</b> has expired.' ); } public function toSmsMessage(Recipient $recipient): SmsMessage { return (new SmsMessage()) ->text('Todo: ' . $this->todo->getText() . ' has expired'); } }
Implementing Recipient
namespace App\Model; use Notifier\Recipient\Recipient; class User implements Recipient { /** @var string */ protected $name; /** @var array */ protected $contacts; public function __construct(string $name, array $contacts) { $this->name = $name; $this->contacts = $contacts; } public function getName(): string { return $this->name; } public function getRecipientContact(string $channel, Notification $notification): ?string { return $this->contacts[$channel] ?? null; } public function getRecipientName(): string { return $this->name; } }
Sending Notifications
use Notifier\Channel\Channels; use Notifier\Channel\Email\EmailChannel; use Notifier\Channel\Email\SimpleMailer; use Notifier\Channel\Sms\SmsChannel; use Notifier\Channel\Sms\TwilioTexter; use Notifier\Notifier; use Notifier\Recipient\Recipients; $notifier = new Notifier(new Channels( new EmailChannel(new SimpleMailer()), new SmsChannel(new TwilioTexter('auth_id', 'auth_token')) )); $notifier->send( new TodoExpiredNotification($todo), new Recipients($user1, $user2, $user3) );
Credits
License
Released under MIT License - see the License File for details.
nikolaposa/notifier 适用场景与选型建议
nikolaposa/notifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.58k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2020 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「email」 「sms」 「notification」 「notifier」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nikolaposa/notifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nikolaposa/notifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nikolaposa/notifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
Apple Push Notification & Feedback Provider
Throttle notifications on a per-channel basis
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Simple javascript toast notifications
统计信息
- 总下载量: 1.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-28