承接 koriit/eventdispatcher 相关项目开发

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

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

koriit/eventdispatcher

Composer 安装命令:

composer require koriit/eventdispatcher

包简介

Simple event dispatcher based on PHP-DI

README 文档

README

Build Status Coverage Status Scrutinizer Code Quality StyleCI SensioLabsInsight

Latest Stable Version License

Simple event dispatcher based on PHP-DI.

This library does not aim to be general purpose library or to cover all possible use cases. What this library does aim to be is perfect choice for those who use PHP-DI and prefer to use PHP Definitions.

The goal is to create as decoupled code as possible. The code that uses the dispatcher may not know its listeners, and the other way around, the listeners may not even know that they are actually used as listeners!

Install

EventDispatcher is available via composer:

composer require koriit/eventdispatcher

Versions

This library depends on PHP-DI and is thus affected by compatibility breaks coming from it.

v2

Tested with PHP-DI ^6.0.

v1

Tested with PHP-DI ^5.4.

Usage

You are encouraged to familiarize yourself with Koriit\EventDispatcher\EventDispatcherInterface and Koriit\EventDispatcher\EventContextInterface as those two interfaces are everything you need to work with this library.

Basic example:

// configure and build your container

$dispatcher = new EventDispatcher($container);

$listener = function (LoggerInterface $logger, Request $request) {
    $logger->info($request->getMethod().' '.$request->getPathInfo());
};

$dispatcher->addListener("init", $listener, 10);

$dispatcher->dispatch("init");

Naturally since we are using PHP-DI then we would create a definition for Koriit\EventDispatcher\EventDispatcherInterface.

A listener may be anything that can be invoked by PHP-DI:

// MyClass.php
class MyClass
{
    public function logRequest(LoggerInterface $logger, Request $request)
    {
        $logger->info($request->getMethod().' '.$request->getPathInfo());
    }
}
// configure and build your container

$dispatcher = $container->get(EventDispatcherInterface::class);

$dispatcher->addListener(ApplicationLifecycle::INITIALIZING, [MyClass::class, 'logRequest'], 10);

$dispatcher->dispatch(ApplicationLifecycle::INITIALIZING);

Even interfaces:

// MyInterface.php
interface MyInterface
{
    public function logRequest(LoggerInterface $logger, Request $request);
}
// MyClass.php
class MyClass implements MyInterface
{
    public function logRequest(LoggerInterface $logger, Request $request)
    {
        $logger->info($request->getMethod().' '.$request->getPathInfo());
    }
}
// configure and build your container

$dispatcher = $container->get(EventDispatcherInterface::class);

$dispatcher->addListener(InitializationEvent::class, [MyInterface::class, 'logRequest'], 10);

$dispatcher->dispatch(InitializationEvent::class, ["event" => new InitializationEvent()]);

For above example to work you need to configure a definition for MyInterface, of course.

Warning:
Event dispatcher doesn't work well with listeners which implement fluent interface or allow for method chaining. For more information, read about stopping dispatchemnt chain below.

Adding listeners

There are 2 ways to subscribe a listener. In both cases you have to specify name of the event and calling priority. The higher the priority value the later the listener will be called. Listeners with the same priority will be called in the order they have been subscribed. You can entirely omit priority parameter as it defaults to 0.

addListener

First, by using addListener method on Koriit\EventDispatcher\EventDispatcher object.

interface EventDispatcherInterface
{
  // ..

  /**
   * Subscribes a listener to given event with specific priority.
   *
   * Listener must be invokable by PHP-DI.
   *
   * The higher the priority value the later the listener will be called.
   * Listeners with the same priority will be called in the order they have been subscribed.
   *
   * @param mixed  $eventName
   * @param mixed  $listener
   * @param int    $priority
   */
  public function addListener($eventName, $listener, $priority = 0);

  // ...
}

addListeners

Second, by using addListeners method on Koriit\EventDispatcher\EventDispatcher object.

interface EventDispatcherInterface
{
  // ..

  /**
   * Subscribes listeners in bulk.
   * 
   * Listeners array is a simple structure of 3 levels:
   * - At first level it is associative array where keys are names of registered events
   * - At second level it is indexed array where keys are priority values
   * - At third level it is simple list containing listeners subscribed to given event with given priority
   * 
   * @param array $listeners
   * 
   * @return void
   */
  public function addListeners($listeners);

  // ...
}

Listeners array is a simple structure of 3 levels:

  • At first level it is associative array where keys are names of registered events
  • At second level it is indexed array where keys are priority values
  • At third level it is simple list containing listeners subscribed to given event with given priority

Example:

// listners.php
// namespace and imports...

return [
    CLIApplicationLifecycle::INITIALIZING => [
        0 => [
            [ConfigServiceInterface::class, 'init'],
        ],
        1 => [
            [CommandsServiceInterface::class, 'load'],
        ],
    ],

    CLIApplicationLifecycle::FINALIZING => [
        100 => [
            function (LoggerInterface $logger, InputInterface $input, $exitCode) {
                $logger->info('Returning from command `'.$input.'` with exit code '.$exitCode);
            },
        ],
    ],
];
// ...

$dispatcher->addListeners(include 'listeners.php');

// ...

Dispatchment

Dispatchment is a simple process of invoking all listeners subscribed to dispatched event.

interface EventDispatcherInterface
{
    // ...

    /**
     * Dispatches an event with given name.
     *
     * @param mixed $eventName
     * @param array $parameters
     *
     * @return EventContextInterface
     */
    public function dispatch($eventName, $parameters = []);

    // ...
}
// ..

$dispatcher->dispatch(ApplicationLifecycle::INITIALIZING);

Stopping dispatchment

If any listener in the dispatchment chain returns a value that can be evaluated as true, the dispachment is stopped and all the remaining listeners are skipped. You can also achieve this by calling stop on event context.

While this design simplifies the process and does not require wiring listeners with event dispatcher, it makes it problematic to work with listeners that return values which cannot be interpreted as success or failure. This especially holds true for methods which allow for method chaining or implement fluent interface. To work around this problem you can use stop and ignoreReturnValue methods on event context, though, that requires wiring your listener with event context. Everything is a trade-off, someone once said.

Context

Event context is simple data object holding information about the dispatchment process. See Koriit\EventDispatcher\EventContextInterface for more information.

Parameters

You can pass additional parameters to be used by invoker while injecting listener arguments by name.

// ..

$dispatcher->dispatch(InitializationEvent::class, ["event" => new InitializationEvent()]);
function listener($event) {
  // $event is InitializationEvent object
}

eventName, eventContext, eventDispatcher

There are 3 parameters injected by event dispatcher itself:

  1. eventName - name of the event dispatched
  2. eventContext - reference to the context object of current dispatchment
  3. eventDispatcher - reference to the event dispatcher itself; this allows for nested dispatchments

You cannot override them as using those keys in parameters array causes exception.

koriit/eventdispatcher 适用场景与选型建议

koriit/eventdispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 669 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「event」 「dispatcher」 「eventdispatcher」 「php-di」 「koriit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 koriit/eventdispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 koriit/eventdispatcher 我们能提供哪些服务?
定制开发 / 二次开发

基于 koriit/eventdispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 669
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 8
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-11