ashleydawson/domain-event-dispatcher-bundle

Symfony 扩展包,用于添加领域事件分发器(单例)

v1.0.2 2017-01-01 17:02 UTC

This package is auto-updated.

Last update: 2024-09-14 03:56:11 UTC


README

领域事件分发器(单例)引入Symfony项目。有关完整使用说明,请参阅库附带的完整文档。

安装

使用Composer安装该包

$ composer require ashleydawson/domain-event-dispatcher-bundle

然后,在 app/AppKernel.php 文件中注册该包

$bundles = [
    // ...
    new AshleyDawson\DomainEventDispatcherBundle\AshleyDawsonDomainEventDispatcherBundle(),
];

配置

延迟事件被配置为从 Symfony 的 kernel.terminate 内核事件中分发。要更改此设置,请将以下内容添加到您的 app/config/config.yml 文件中

ashley_dawson_domain_event_dispatcher:
    dispatch_deferred_events_from_kernel_event: kernel.terminate
    dispatch_deferred_events_from_kernel_event_priority: 0

用法

请参阅完整文档以深入了解如何使用领域事件分发器。以下是一个简单示例

创建一个事件

<?php

namespace AppBundle\DomainEvent;

class MyDomainEvent
{
    private $myEntityId;
    
    public function __construct($myEntityId)
    {
        $this->myEntityId = $myEntityId;
    }
    
    public function getMyEntityId()
    {
        return $this->myEntityId;
    }
}

创建一个监听器

<?php

namespace AppBundle\DomainEventListener;

use AppBundle\DomainEvent\MyDomainEvent;

class MyDomainEventListener
{
    public function __invoke(MyDomainEvent $event)
    {
        // Do something with the event...
    }
}

使用带有标签 ashley_dawson.domain_event_listenerSymfony 依赖注入容器 将监听器添加到事件分发器

# app/config/services.yml

services:
    app.my_domain_event_listener:
        class: AppBundle\DomainEventListener\MyDomainEventListener
        tags:
            - { name: ashley_dawson.domain_event_listener }

从您的模型中分发一个事件

<?php

namespace AppBundle\Entity;

use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use AppBundle\DomainEvent\MyDomainEvent;

class MyEntity
{
    private $id;
    
    public function mySpecialCommand()
    {
        DomainEventDispatcher::getInstance()->dispatch(
            new MyDomainEvent($this->id)
        );
    }
}

Symfony 性能分析器

在请求期间已延迟/分发的事件的映射可以在 Symfony 性能分析器中找到。只需单击领域事件图标,即可显示包含映射的配置屏幕。

工具栏信息

Toolbar

完整性能分析器屏幕

Profiler Screen