软Servlet / 通知
Requires
- php: >=5.3.0
- illuminate/support: 4.1.*
This package is not auto-updated.
Last update: 2024-09-24 06:11:22 UTC
README
该包为Web应用提供了一个可扩展的通知系统结构和数据库设计。它提供了一种简单且可扩展的方式来管理Web应用的通知。
该包的目标是提供一种快速且可扩展的方式来生成如下类型的通知:
用户Foo在生日相册中添加了一张新照片。
安装
目前只提供了Laravel 4框架的安装指南。
-
todo - 在packagist上添加包
-
运行数据库迁移
php artisan migrate --bench=softservlet/notification
- 将通知服务提供者添加到`app/config/app.php`
'Softservlet\Notification\Laravel\Providers\NotificationServiceProvider'
- 在您的应用程序中定义Notificable对象
该包为您提供了选择谁将接收通知的机会。您需要实现Softservlet\Notification命名空间中的NotificableInterface。
方法getId()必须返回为每个用户指定的唯一ID。在Laravel 4中定义IoC绑定时,请写:
App::bind('Softservlet\Notification\NotificableInterface', 'YourApp\User');
有关Laravel文档中IoC绑定的更多信息。
使用方法
通知由通知对象和动作对象创建。一个通知至少属于一个用户。
创建通知
在创建通知之前,首先需要理解通知对象和通知动作术语。
让我们分析以下通知:
Foo用户收到了Summer照片的点赞。
这可以是一个渲染的通知(我们稍后会看到如何渲染通知)。
我们可以将这个句子抽象化并识别出三个主要对象
- Foo用户 - 实现NotificableInterface
- Photo - 名称是Summer的照片
- Like - 点赞对象
编写这段代码的好地方是在您的应用程序的NotificationController中。我将跳过这一步。
//create a user which implements NotificableInterface $user = User::find(5); //define the Photo object $photo = Photo::find(12); //get the Photo object with id 12 //get the like object of this photo $like = Photo::like()->first(); //a basic Eloquent usage //let's create a notification of this objects $object = App::make('Softservlet\Notification\Notification\NotificationEntityInterface', array($photo, 12)); $actor = App::make('Softservlet\Notification\Notification\NotificationEntityInterface', array($like, $like->getId()));
从现在起,我们可以创建一个通知并将其附加到$user
//create an instance of notification repository $notificationRepository = App::make('Softservlet\Notification\Repositories\NotificationRepositoryInterface'); //create the notification $notification = $notificationRepository->create($object, $actor); //attach this notification to the user $notificationRepository->attach($user, $notification);
此时,通知应该存在于您的数据库中,并且用户应该会收到通知。
向用户列表发送通知
通知用户数组的简单方法是用Softservlet\Notication\Notifier对象。
$notifier = App::make('Softservlet\Notification\Notifier\NotifierInterface', $notificableArray);
解析通知 - 映射器
我们已经看到了如何创建通知,现在让我们来看看如何处理它并生成预期的输出。
该包需要知道当您有特定类型的通知对象和特定通知动作时会发生什么。
这里我们引入了一个新的关键词 - 映射器。映射器是一个数组,看起来像
$mapper = array ( array ( 'object' => 'App\Photo', 'actor' => 'App\Like', 'single' => function($notification) { //a dummy repository of photos $photoRepository = new photoRepository(); //here we get the photo based on the ID that we've passed //as parameter to NotificationEntity $photo = $photoRepository->find($notification->getObject()->getId()); //a dummy likes repository $likeRepository = new likesRepository; //get the like object $like = $likeRepository->find($notification->getActor()->getId()); return $photo->author()->username .' has received a new like from '. $like->author()->username; } ) );
###映射器标准Todo
###完整的工作流程
//let's create a notification of this objects //in this example we want to notify a User that a Photo has been Created //create the object we want to nofify - in our case the Photo $object = App::make('Softservlet\Notification\Notification\NotificationEntityInterface', array('Photo')); //create an actor of the notification, in our case will be Create verb $actor = App::make('Softservlet\Notification\Notification\NotificationEntityInterface', array('Created', 15)); //we need to notify someone about this notification //so we create a instance of an object which implements NotificableInterface $notificable = App::make('Softservlet\Notification\NotificableInterface'); $notificable = $notificable->find(1); //?????? //create an instance of notification repository $notificationRepository = App::make('Softservlet\Notification\Repositories\NotificationRepositoryInterface'); //create the notification - each notification is made by an object //and an actor $notification = $notificationRepository->create($object, $actor); //once a notification is created, you can attach it to multiple users $notificationRepository->attach($notificable, $notification); //mapper array - the standard is defined in documentation $mapper = array ( array ( 'object' => 'Photo', 'actor' => 'Created', 'single' => function($notification) { return 'xyzABCD'; }, 'group' => function($notifications, $username) { return $username->email.' has ' . count($notifications) . ' new notifications to a photo'; } ) ); //resolve this notification - call the specify callback defined in mapper //we need to call this to $resolver = App::make('Softservlet\Notification\Resolver\Resolver', array($mapper)); //get all notifications for $notificable object //which we've been instantiated before $repo = App::make('Softservlet\Notification\Repositories\NotificableRepositoryInterface', $notificable); $notifications = $repo->get(); //get all notifications from $notificable user object //resolve all notifications for $notificable object //result will be an array of rendered notifications $result = $resolver->resolve($notifications, $notificable);