rekalogika/domain-event-outbox

基于 rekalogika/domain-event 实现的事务型出盒模式

支持包维护!
priyadi

安装: 499

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

2.5.0 2024-09-16 15:46 UTC

This package is auto-updated.

Last update: 2024-09-16 15:47:38 UTC


README

rekalogika/domain-event 包的基础上实现了事务型出盒模式。

完整文档可在 rekalogika.dev/domain-event 查找。

概要

//
// The event
//

final readonly class PostChanged
{
    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 setTitle(string $title): void
    {
        $this->title = $title;
        // highlight-next-line
        $this->recordEvent(new PostChanged($this->id));
    }

    // ...
}

//
// The listener
//

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

class PostEventListener
{
    public function __construct(private LoggerInterface $logger) {}
    
    // highlight-next-line
    #[AsPublishedDomainEventListener]
    public function onPostChanged(PostChanged $event) {
        $postId = $event->postId;

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

//
// The caller
//

use Doctrine\ORM\EntityManagerInterface;

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

$post->setTitle('New title');
$entityManager->flush();

// During the flush above, the event will be recorded in the outbox table in the
// database. Then the message relay service is executed, and will publish the
// events on the event bus. When the event bus announces the event, the listener
// will be executed.

文档

rekalogika.dev/domain-event.

许可证

MIT

贡献

rekalogika/domain-event-outbox 仓库是从主仓库拆分出的只读仓库。问题和拉取请求应提交到 rekalogika/domain-event-src monorepo。