定制 mitch/event-dispatcher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mitch/event-dispatcher

Composer 安装命令:

composer require mitch/event-dispatcher

包简介

Event Dispatcher with a focus on Domain Events

README 文档

README

This repository has been moved to Big Name. There won't be any support here. Visit Big Name's repository for the newest updates.

An Event Dispatcher built with a focus on Domain Events.

Installation

Begin by installing the package through Composer. Edit your project's composer.json file to require mitch/event-dispatcher.

"require": {
  "mitch/event-dispatcher": "0.1.x"
}

Next use Composer to update your project from the the Terminal:

php composer.phar update

Once the package has been installed you'll need to add the service provider. Open your app/config/app.php configuration file, and add a new item to the providers array.

'Mitch\EventDispatcher\Laravel\EventDispatcherServiceProvider'

How It Works

Event

In your domain you'll create an event, for let's say when a new user has been added. Lets call this event UserCreatedEvent. This event will hold the necessary information for the listener to fulfill it's job. You have complete freedom about which arguments it takes, since you'll be the one passing them in. In some ways this event is a Date Transfer Object (DTO).

For example:

<?php namespace Domain\Accounts\Events;

use Mitch\EventDispatcher\Event;

class UserCreatedEvent implements Event
{
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function getName()
    {
        return 'UserCreated';
    }
}

Listener

An event without a listener does no good for us, so lets create an email listener MailNewlyCreatedUserListener for the event UserCreatedEvent.

<?php namespace Domain\Accounts\EventListeners;

use Mitch\EventDispatcher\Listener;

class MailNewlyCreatedUserListener implements Listener
{
    private $mailer

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function handle(Event $event)
    {
        // Do something with the event
    }
}

Same rule with the listeners as the events, you have complete freedom with the arguments. When an event is dispatched the handle method on the correct listeners will be called.

Listening

Now we got the building blocks ready lets start listening for some new users, shall we. For the sake of this example, the code is kept as simple as possible.

use Mitch\EventDispatcher\Dispatcher;
use Domain\Accounts\Events\UserCreatedEvent;
use Domain\Accounts\EventListeners\MailNewlyCreatedUserListener;

// Listening for event
$mailer = // Some mail package...
$listener = new MailNewlyCreatedUserListener($mailer);

$dispatcher = new Dispatcher;
$dispatcher->addListener('UserCreated', $listener);

// Dispatching event
$user = // A wild user appeared..
$event = new UserCreatedEvent($user);

$dispatcher->dispatch($event);

Dispatching multiple

For extra hipster points you can dispatch multiple events in 1 call.

use Mitch\EventDispatcher\Dispatcher;
use Domain\Accounts\UserAddedEvent;
use Domain\Achievements\UserGotAchievementEvent;

$user = ...;
$achievement = ...;
$events = [
    new UserAddedEvent($user),
    new UserGotAchievementEvent($user, $achievement)
];

$dispatcher = new Dispatcher;
$dispatcher->dispatch($events);

That's it!

Later tater

统计信息

  • 总下载量: 254
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固