omar-makled / aqarmap-notification
Requires
- php: >=5.5.9
- guzzlehttp/guzzle: ~6.0
- nexmo/client: @beta
- php-amqplib/php-amqplib: > 2.6.1
- symfony/symfony: ^3.0
Requires (Dev)
- mockery/mockery: ^0.9.9
- phpunit/phpunit: ^6.2
- symfony/phpunit-bridge: ^3.0
README
介绍
我们提供了一个很好的方法来解耦通知和通道的各个方面,因为每个都是独立的类,提供了非常低的耦合。
我们支持通过多种交付通道发送通知,包括邮件、数据库、短信等...
安装
Aqarmap Notification 是一个 symfony 框架包,首先您需要创建一个新的 symfony 应用程序
symfony new my-app
通过 Composer
要获取最新版本,只需使用 composer 添加框架包
composer require omar-makled/aqarmap-notification
注意您可能需要在 composer.json
中设置 "minimum-stability": "dev"
"minimum-stability": "dev",
"prefer-stable": true
通过克隆仓库
或者,您也可以通过克隆 src
目录中的仓库来安装通知 git clone git@github.com:OmarMakled/aqarmap-notification.git NotificationBundle
下载完成后,您应该更新 composer autoload
并运行 composer dump
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Aqarmap\\NotificationBundle\\": "src/NotificationBundle/src"
},
安装完成后,您需要在 app/AppKernel.php
中注册 new Aqarmap\NotificationBundle\NotificationBundle
。您还需要运行 bin/console doctrine:schema:update --dump-sql
以创建通知表 qaramap_notification
创建通知
每个通知都由一个单独的类表示(通常存储在 Bundle/Notifications
目录中),这是一个方便的地方来注册应用程序中的所有通知。
bin/console aqarmap:notification-create
此命令将帮助您在 AppBundle/Notifications
目录中创建一个新的通知类。
Welcome to the Aqarmap notification generator
Bundle name (e.g. AppBundle): AppBundle
Notification name (e.g InvoicePaid): NewListing
Please select notfication config [default ClassConfig]
[0] ClassConfig
[1] YmlConfig
> ClassConfig
Please select channel [default all]
[0] Database
[1] Mail
[2] SMS
>
Do you want to queue channel [Yes]?
Please select queue channel (defaults to all)
[0] Database
[1] Mail
[2] SMS
>
Do you want to constructor injection with `TwigEngine` [recommended Yes]?
Summary before generation
You are going to generate a NewListing command inside AppBundle bundle.
Do you confirm generation [Yes]?
created ./src/AppBundle/Notifications/NewListing.php
Generated the NewListing command in AppBundle
Everything is OK! Now get to work :).
通知配置
每个通知可以在 class
或 yml
文件中配置,具体取决于您的需求。
例如,在 NewListing.php
中,我们定义了 channel
和 queue
,请注意,设置 public $config = \Aqarmap\NotificationBundle\Config\ClassConfig::class
很重要,它告诉 NotificationManager
关于配置类型
/**
* Point to config type
*
* @var \Aqarmap\NotificationBundle\Config\ConfigInterface
*/
public $config = \Aqarmap\NotificationBundle\Config\ClassConfig::class;
/**
* List of channels
*
* @var array
*/
public $channel = ["Database","Mail","SMS"];
/**
* List of queue channels
*
* @var array
*/
public $queue = [];
有时您可能需要将通知配置在 config.yml
中以用于发布目的,这里我们指向 YmlConfig
告诉管理者在 config.yml
(存储在 Bundle/Notifications/config.yml
并自动生成)中查找配置
NewListing.php
/**
* Point to config type
*
* @var \Aqarmap\NotificationBundle\Config\ConfigInterface
*/
public $config = \Aqarmap\NotificationBundle\Config\YmlConfig::class;
config.yml
Notifications:
NewListing:
class: Notifications\NewListing
channel: ['sms', 'database', 'mail']
queue: ['sms', 'database', 'mail']
发送通知
在控制器内
通知管理器在服务容器内定义,因此您可以通过 $this->get('noification_manager)
获取新实例
注意如果您回答“是”,则 Do you want to constructor injection with
TwigEngine[推荐:是]?
这对于渲染电子邮件骨架可能很有用。如果是这样,您必须传递依赖项,它不会自动注入,您可以使用服务容器的功能。
Controller.php
$manager = $this->get('notification_manager');
$manager->send($users, new NewListing($this->get('templating'));
NewListing.php
/**
* Define the message via Mail channel
*
* @return string
*/
public function byMail()
{
return $this->twigEngine->render(
__DIR__.'/views/newlisting.php.twig'
);
}
使用事件订阅者
事件订阅者是可能在其自身内部订阅多个事件的类 创建事件订阅者,通常事件订阅者应该通过 services.yml
注册。
services.yml
notification_subscriber:
class: AppBundle\NotificationSubscriber
arguments: ["@notification_manager"]
tags:
- {name: kernel.event_subscriber}
NotificationSubscriber.php
class NotificationSubscriber implements EventSubscriberInterface
{
/**
* Notification manager instance.
*
* @var \Aqarmap\NotificationBundle\NotificationManager
*/
public $manager;
/**
* Create a notification subscriber instance.
*
* @param \Aqarmap\NotificationBundle\NotificationManager $manager
*/
public function __construct(NotificationManager $manager)
{
$this->manager = $manager;
}
/**
* Register the listeners for the subscriber.
*/
public static function getSubScribedEvents()
{
return [
'add.listing' => 'SendNewListingNotification'
];
}
/**
* Send new listing notification
*
* @param \AppBundle\Events\AddListingEvent $event
*/
public function SendNewListingNotification(AddListingEvent $event)
{
$this->manager->send($event->users, new NewListingNotification($event->listing));
$event->stopPropagation();
}
}
使用事件监听器
由于单个事件可以有多个不相互依赖的监听器。例如,您可能希望每次添加新的列表时都向您的用户发送短信通知。而不是将代码耦合在一个类(如 EventSubscriber
或 EventListner
)中,这是最常见的方式。
通知事件
事件提供了一个简单的观察者实现,允许您订阅并监听应用程序中发生的各种事件。
当发送通知时,通知系统通过 Aqarmap\NotificationBundle\Events\NotificationSent
事件。它包含 NotificationInterface
和 channel
。您可以在应用程序中为该事件注册监听器。
队列通知
发送通知可能需要时间,您可以通过定义 queue
属性来设置特定的通道以异步运行。
测试用例
Aqarmap 通知构建时考虑了测试,并附带了测试用例。
待办事项
- 还有很多事情要……