承接 heybigname/event-dispatcher 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

heybigname/event-dispatcher

Composer 安装命令:

composer require heybigname/event-dispatcher

包简介

Event Dispatcher with a focus on Domain Events

README 文档

README

Latest Stable Version License Build Status Coverage Status Total Downloads

An Event Dispatcher built with a focus on Domain Events.

Table of Contents

Installation

Run the command below to install via Composer

composer require heybigname/event-dispatcher

Integrations

Although this package is completely framework agnostic, it does have integrations with framework(s). Currently only Laravel 4.x is included, but if requested others can be added.

Laravel

To install into a Laravel project, first do the composer install then add the following class to your config/app.php service providers list.

'BigName\EventDispatcher\Integrations\Laravel\ServiceProvider',

That's all it takes for the Laravel integration. Now it's possible to inject or make a Dispatcher like this:

use BigName\EventDispatcher\Dispatcher;

class RegisterMemberHandler implements Handler
{
    private $dispatcher;

    public function __construct(Dispatcher $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }
}
$dispatcher = App::make('BigName\EventDispatcher\Dispatcher');

How It Works

Event

In your domain you'll create an event, for let's say when a new member has been registered. Lets call this event MemberWasRegistered. 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 Data Transfer Object (DTO).

For example:

<?php namespace Domain\Accounts\Events;

use BigName\EventDispatcher\Event;

class MemberWasRegistered implements Event
{
    private $member;

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

    public function getMember()
    {
        return $this->member;
    }

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

Listener

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

<?php namespace Domain\Accounts\EventListeners;

use BigName\EventDispatcher\Listener;
use BigName\EventDispatcher\Event;

class WelcomeNewlyRegisteredMemberListener implements Listener
{
    private $mailer

    public function __construct($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 members, shall we. For the sake of this example, the code is kept as simple as possible.

use BigName\EventDispatcher\Dispatcher;
use Domain\Accounts\Events\MemberWasRegistered;
use Domain\Accounts\EventListeners\WelcomeNewlyRegisteredMemberListener;

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

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

// Dispatching event
$member = // A wild member appeared..
$event = new MemberWasRegistered($member);

$dispatcher->dispatch($event);

Lazy Listening

It's possible to have your listeners lazy loaded. Merely pass a string with full namespace of the class. Currently this only works with the Laravel (Illuminate) Container. Other containers can be supported, if requested.

use Illuminate\Container\Container;
use BigName\EventDispatcher\Dispatcher;
use BigName\EventDispatcher\Containers\LaravelContainer;

$container = new LaravelContainer(new Container);
$dispatcher = new Dispatcher($container);

$dispatcher->addLazyListener('MemberWasRegistered', 'Domain\Accounts\EventListeners\WelcomeNewlyRegisteredMemberListener');

This will construct and instantiate the listener(s) on dispatch() or getLazyListeners(). Lazy loading listeners can help mitigate the overhead if you have all your listeners instantiated on bootstrap.

Dispatching Multiple Events

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

use BigName\EventDispatcher\Dispatcher;
use Domain\Accounts\MemberWasRegistered;
use Domain\Achievements\MemberEarnedAchievement;

$member = ...;
$achievement = ...;
$events = [
    new MemberWasRegistered($member),
    new MemberEarnedAchievement($member, $achievement)
];

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

Maintainers

This package is maintained by Mitchell van Wijngaarden of Big Name

License

This package is licensed under the MIT license.

统计信息

  • 总下载量: 13.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 59
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 59
  • Watchers: 12
  • Forks: 3
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固