定制 rekalogika/domain-event 二次开发

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

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

rekalogika/domain-event

Composer 安装命令:

composer require rekalogika/domain-event

包简介

Domain Event Implementation for Symfony and Doctrine

README 文档

README

An implementation of domain event pattern for Symfony & Doctrine.

Full documentation is available at rekalogika.dev/domain-event.

What is a Domain Event?

A domain event is simply a regular event like you would normally use with Symfony's EventDispatcher. The difference is that a domain event represents something that has happened in your domain. It has a name that is meaningful to the underlying business that the domain represents. A domain event is usually dispatched by your entities, as opposed to being dispatched from your controllers or other services.

Why Use Domain Events?

A domain event represents a business event that has happened. It is a good way to model the business requirements that say "when something happens, do this".

A domain event is raised by the part of your code where the event is actually happening. Different part of your application might call the same method on an entity. In some cases, the method is called indirectly, and the caller has no idea that it is being called. By using domain events, the event will be dispatched in all the cases. No need to make sure to dispatch the event from all the different places where the method is called.

The application layer (controllers, services) can tell an entity to do something, but it cannot reliably know if the action is actually performed, or if an additional action is performed. A controller or a service can ask $bookshelf->removeBook($book), but only the $bookshelf knows if the book was actually removed. And if the event actually happened, the entity can tell the world about it by recording a BookRemoved event.

Some problems might tempt you to inject a service into your entity. With domain events, you can avoid that. You can make your entity dispatch an event, and set up a listener to react to that event. The relevant services can then correctly act on your entity, instead of the other way around.

Synopsis

//
// The event
//

final readonly class PostPublished
{
    public function __construct(public string $postId) {}
}

//
// The entity
//

use Rekalogika\Contracts\DomainEvent\DomainEventEmitterInterface;
use Rekalogika\Contracts\DomainEvent\DomainEventEmitterTrait;

class Post implements DomainEventEmitterInterface
{
    use DomainEventEmitterTrait;
    
    // ...

    public function setStatus(string $status): void
    {
        $originalStatus = $this->status;
        $this->status = $status;

        // records the published event if the new status is published and it
        // is different from the original status

        if ($status === 'published' && $originalStatus !== $status) {
            $this->recordEvent(new PostPublished($this->id));
        }
    }

    // ...
}

//
// The listener
//

use Psr\Log\LoggerInterface;
use Rekalogika\Contracts\DomainEvent\Attribute\AsPostFlushDomainEventListener;

class PostEventListener
{
    public function __construct(private LoggerInterface $logger) {}

    // will be called after the post is published and the entity manager is
    // flushed
    
    #[AsPostFlushDomainEventListener]
    public function onPostPublished(PostPublished $event) {
        $postId = $event->postId;

        $this->logger->info("Post $postId has been published.");
    }
}

//
// The caller
//

use Doctrine\ORM\EntityManagerInterface;

/** @var Post $post */
/** @var EntityManagerInterface $entityManager */

$post->setStatus('published');
$entityManager->flush();

// the event will be dispatched after the flush above, afterwards the listener
// above will be called, sending a message to the logger

Features

  • Works out of the box. No configuration is required for basic features.
  • Simple, unopinionated architecture. Uses plain event objects, and doesn't require much from your domain entities.
  • Uses standard Symfony's EventDispatcher, with the same dispatching semantics & listener registrations.
  • Transaction support.
  • Works with multiple entity managers.
  • Multiple events considered identical are dispatched only once.
  • Four listening strategies: immediate, pre-flush, post-flush, and event bus.
  • Uses Symfony Messenger as the event bus implementation.
  • Utilizes the transactional outbox pattern when publishing events to the event bus to guarantee consistency and delivery.
  • Utilizes Symfony Scheduler to relay undelivered events to the event bus.
  • Does not require you to change how you work with entities.
  • Should work everywhere without any change: in controllers, message handlers, command line, etc.
  • Separated contracts & framework. Useful for enforcing architectural boundaries. Your domain doesn't have to depend on the framework.
  • Symfony Profiler integration. Debug your events in the profiler's events panel.

To Do

  • Support for Doctrine MongoDB ODM.
  • Support event inheritance.
  • Deprecate __remove() and use a method tagged with an attribute instead.

Installation

Ensure that Symfony Flex is enabled (it is enabled by default). Open a command console, enter your project directory and execute:

composer require rekalogika/domain-event

Documentation

rekalogika.dev/domain-event

License

MIT

Contributing

The rekalogika/domain-event repository is a read-only repo split from the main repo. Issues and pull requests should be submitted to the rekalogika/domain-event-src monorepo.

rekalogika/domain-event 适用场景与选型建议

rekalogika/domain-event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.37k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 rekalogika/domain-event 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-01