shippinno / notification
该软件包最新版本(v3.0.0)没有可用的许可信息。
v3.0.0
2023-08-31 06:25 UTC
Requires
- php: ^8.2
- ext-json: *
- adbario/php-dot-notation: ^2.1
- alek13/slack: ^2.2
- doctrine/orm: ^2.5
- mathiasverraes/classfunctions: ^1.1
- shippinno/email: ^1.0.0
- shippinno/template: ^1.0
- symfony/cache: ^6.3
- tanigami/doctrine-json-unescaped-type: ^1.0
- tanigami/specification: ^1.3
- tanigami/value-objects: ^0.4.6
Requires (Dev)
- fakerphp/faker: ^1.9.1
- mockery/mockery: ^1.2
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.3
- dev-master
- v3.0.0
- v2.1.0
- v2.0.0
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.1.0
- v0.0.1
- dev-feature/php-version-up
- dev-feature/symfony-mailer
- dev-develop
- dev-feature/remove-all-method
This package is auto-updated.
Last update: 2024-09-09 07:44:39 UTC
README
安装
$ composer require shippinno/notification
基本用法
创建并发送通知
创建一个包含Destination
的Notification
并通过Gateway
发送。
use Shippinno\Email\SwiftMailer\SwiftMailerSendEmail; use Shippinno\Notification\Domain\Model\Notification; use Shippinno\Notification\Domain\Model\NotificationNotSentException; use Shippinno\Notification\Infrastructure\Domain\Model\EmailGateway; $notification = new Notification( new EmailDestination( [new EmailAddress('to@example.com')], ), new Subject('Hello'), new Body('This is a notification.'), ); $gateway = new EmailGateway( new SwiftMailerSendEmail(...), new EmailAddress('from@example.com') ); try { $gateway->send($notification); } catch (NotificationNotSentException $e) { // ... }
Gateway
必须与Destination
兼容(检查Destination::sendsToDestination(Destination $destination)
)。在上面的例子中,我们假设EmailGateway
接受EmailDestination
的通知。
持久化通知
使用NotificationRepository
将通知持久化到您的数据库。
如果您使用DoctrineNotificationRepository
并将$isPrecocious
属性设置为true
,您不需要执行EntityManager::flush()
。
$repository = new DoctrineNotificationRepository($em, $class, true); // $isPrecocious === true $repository->add($notification); // Already flushed.
您可以检索新鲜(未发送或失败的)通知以发送它们。
$notifications = $repository->freshNotifications();
与持久化通知一起工作,您可能希望在尝试发送后将其标记为已发送或失败。
如果您的DoctrineNotificationRepository
是预先的,调用persist()
将立即刷新。
try { $gateway->send($notification); $notification->markSent(); } catch (NotificationNotSentException $e) { $notification->markFailed($e->__toString()); // mark it failed with the reason } finally { $repository->persist($notification); }
高级用法
使用模板
假设您有Liquid模板,如下所示:
$ tree -d /templates /templates |-- hello__subject.liquid `-- hello__body.liquid $ $ cat /templates/hello.subject.liquid Hello, {{ you }} !! $ $ cat /templates/hello.body.liquid Good bye, {{ her }} :)
然后您可以使用这些模板通过TemplateNotificationFactory
创建通知。
use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Shippinno\Template\Liquid; use Shippinno\Notification\Domain\Model\TemplateNotificationFactory; $template = new Liquid(new Filesystem(new Local('/templates'))); $factory = new TemplateNotificationFactory($template); $notification = $factory->create( 'hello', // template name ['you' => 'Shippinno', 'her' => 'Jessica']), // variables for the template new EmailDestination([new EmailAddress('to@example.com')]) ); $notification->subject()->subject(); // => 'Hello Shippinno !!' $notification->body()->body(); // => 'Good bye Jessica :)'
有关模板如何工作的更多详细信息,请参阅shippinno/template。
网关路由
SendNotification
服务将通知路由到并通过GatewayRegistry
指定的网关发送。
use Shippinno\Notification\Domain\Model\SendNotification; use Shippinno\Notification\Domain\Model\GatewayRegistry; $gatewayRegistry = new GatewayRegistry; $gatewayRegistry->set('EmailDestination', new EmailGateway(...)); $gatewayRegistry->set('SlackChannelDestination', new SlackGateway(...)); $emailNotification = new Notification(new EmailDestination(...), ...); $slackChannelNotification = new Notification(new SlackChannelDestination(...), ...); $sendNotifiation = new SendNotification($gatewayRegistry); $sendNotification->execute($emailNotification); // will send an email $sendNotification->execute($slackChannelNotification); // will send a message to the Slack channel