demroos / notification-bundle
Synfony 通知控制组件
Requires
- php: ^8.0
- ext-ctype: *
- ext-iconv: *
- ext-json: *
- doctrine/cache: ^1.10.2
- jms/serializer-bundle: ^2.4 | ^3.3
- symfony/config: ^4.2 || ^5.0
- symfony/dependency-injection: ^4.2 || ^5.0
- symfony/flex: ^1.0
- symfony/framework-bundle: ^4.2 || ^5.0
- symfony/validator: ^4.2 || ^5.0
- symfony/yaml: ^4.2 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^9.3.8
This package is auto-updated.
Last update: 2024-09-21 21:14:36 UTC
README
注册实体和接收通知
注意:默认情况下,传入的通知会记录到“notifications”通道
要与通知接收器管理器一起工作,您需要一个messenger组件
composer require symfony/messenger
在文件 config/packages/messenger.yaml
中配置 Symfony 的 messenger
配置 NotificationManager:添加到配置 notification.yml
notification: entities: - { name: entity_name, class: App\NotificationClass }
更改 NotificationSenderInterface 实现类
notification: sender: 'Class implement NotificationSenderInterface'
为您的传入通知创建一个控制器类。该控制器将处理所有传入通知,并使用上述配置分发相应的消息。
<? /** * Class NotificationController * @package App\Controller\Api * @Route("/notification") */ class NotificationController extends ApiController { /** * @Route("/endpoint", name="api_notification_endpoint", methods={"POST"}) * @param Request $request * @param NotificationReceiverInterface $notificationManager * @return JsonResponse */ public function handler(Request $request, NotificationReceiverInterface $notificationManager) { $notification = $notificationManager->handleRequest($request); $this->dispatchMessage($notification); return $this->responseBuilder->setData([])->getResponse(); } }
然后您需要处理控制器之前分发的消息。对于每个实体,将有一个不同的消息处理器。例如
<? class UserNotificationHandler implements MessageHandlerInterface { /** * @var UserRepository */ private $userRepository; /** * @var ObjectManager */ private $entityManager; /** * UserNotificationHandler constructor. * @param UserRepository $userRepository * @param ObjectManager $entityManager */ public function __construct(UserRepository $userRepository, ObjectManager $entityManager) { $this->userRepository = $userRepository; $this->entityManager = $entityManager; } public function __invoke(UserNotification $notification) { $user = $this->userRepository->findOneBy(['oauthId' => $notification->id]); if (!$user instanceof User) { $user = new User(); $user->setOauthId($notification->id); } $user ->setFirstName($notification->firstName) ->setLastName($notification->lastName) ->setRoles($notification->roles); $this->entityManager->persist($user); $this->entityManager->flush(); } }
发送通知
注意:默认情况下,传出通知会记录到“outgoing_notifications”通道
NotificationSender 是一个服务,它帮助您将“notification”消息发送到 RabbitMQ。它使用提供的对象(实体)和 JMS Array Transformer 创建一个包含特定字段(在这种情况下,实体和有效载荷字段)的格式化消息。必须将 Serializer\Groups 添加到必须出现在通知中的实体字段中。组命名必须遵循以下模式:{{entityName}}.notification,例如 @JMS\Groups(groups={"user.notification"})
示例消息
{ "entity": "order", "payload": { "number": "TT1", "status": "ORDER_REGISTERED", ... } }
为了使用 NotificationSender,您需要安装 rabbit 组件
composer require php-amqplib/rabbitmq-bundle
确保您有 config/packages/old_sound_rabbit_mq.yaml
文件,并包含以下内容
old_sound_rabbit_mq: connections: default: url: '%env(RABBITMQ_URL)%' lazy: true producers: # use 'old_sound_rabbit_mq.task_producer' service to send data. order: connection: default exchange_options: { name: 'order', type: headers }
!!!实体名称和生产者名称应相同!!!。如果您想发送 OrderNotification,您应该使用上面的配置文件示例作为生产者,并将实体名称设置为 'order'。
要发送通知,请调用
$notificationSender ->setEntityName('order') ->send($order, $id);