planmylife / notification-bundle
用于管理通知系统的捆绑包
该软件包的规范存储库似乎已丢失,因此该软件包已被冻结。
dev-master
2017-07-31 10:02 UTC
Requires
- php: ^5.4|^7.0
- jms/serializer-bundle: ^1.1
- symfony/event-dispatcher: ~3.0
This package is not auto-updated.
Last update: 2024-06-08 17:49:06 UTC
README
通知捆绑包是为planmylife.io创建的,用于管理所有通知。享受使用它吧。
安装
步骤 1:下载捆绑包
composer require planmylife/notification-bundle
此命令要求您全局安装了Composer,如Composer文档中所述。
步骤 2:启用捆绑包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new PlanMyLife\NotificationBundle\PlanMyLifeNotificationBundle(), ); } // ... }
步骤 3:配置工厂和构建器
# app/config/routing.yml pml_notification: factory: '@notification.factory' builders: std: class: stdClass builder: PlanMyLife\NotificationBundle\Builder\StdNotificationBuilder user: class: Acme\MainBundle\Entity\User builder: Acme\MainBundle\Builder\UserNotificationBuilder
步骤 4:添加您的构建器类
<?php namespace Acme\MainBundle\Builder; use Acme\MainBundle\Entity\User; use PlanMyLife\NotificationBundle\Model\Notification; class UserNotificationBuilder implements NotificationBuilderInterface { /** * @param User $target * @param mixed $type * @return Notification */ public function build($target, $type = null) { $notification = new Notification(); $notification->setTitle($target->getName()); $notification->setContent('user notification content example'); $notification->setType('[add type here]'); // log, notice, info, warning, error, critical $notification->setDate($target->getCreatedAt()); $notification->setParams([ 'email' => $target->getEmail(), # Params for mailer manager 'username' => $target->getUsername(), # Params for mailer/slack manager 'channel' => 'notification' # Params for slack manager 'template' => "Acme:Notification:$type.txt.twig" ]); return $notification; } }
附加管理器
步骤 1:定义管理器类
<?php namespace PlanMyLife\NotificationBundle\Manager; use PlanMyLife\NotificationBundle\Model\Notification; use Symfony\Component\HttpFoundation\Session\Session; class FlashBagNotificationManager implements NotificationManagerInterface { /** @var Session */ protected $session; /** * FlashBagNotificationManager constructor. * @param Session $session */ public function __construct(Session $session) { $this->session = $session; } public function manage(Notification $notification) { $this->session->getFlashBag()->add($notification->getType(), $notification->getContent()); } }
步骤 2:定义您的管理器服务
services: notification.manager.flash_bag: class: PlanMyLife\NotificationBundle\Manager\FlashBagNotificationManager arguments: - "@session"
步骤 3:定义您的监听器
<?php namespace PlanMyLife\NotificationBundle\EventListener; class FlashBagNotificationListener extends NotificationListener implements NotificationListenerInterface { public function getName() { return 'flash_bag'; } }
步骤 4:绑定您的监听器
services: notification.listener.flash_bag: class: PlanMyLife\NotificationBundle\EventListener\FlashBagNotificationListener arguments: - "%pml_notification.factory%" - "@notification.manager.flash_bag" tags: - { name: notification.event_subscriber, event: pml.notification }
步骤 5:测试您的通知
$object = new stdClass(); $object->title = 'Test title'; $object->content = 'Test Content'; $object->type = 'success'; $object->date = new \DateTime(); $object->params = []; $this->get('notification.service')->notify($object, ['flash_bag'])