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

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

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

a50/event-dispatcher

Composer 安装命令:

composer require a50/event-dispatcher

包简介

Event Dispatcher

README 文档

README

A50 Event Dispatcher


Latest Version on Packagist GitHub Workflow Status Analysis Total Downloads

This is PSR-14 Event Dispatcher pretty simple implementation and framework-agnostic solution.

Why another one?

Because:

  • you want to follow standard and don't worry about implementation. You can change this provider to another one anytime;
  • you don't need complicated and overhead solution;
  • you want easy configure and use event dispatcher.

Requirements

  • The minimum PHP version is 8.0.

Installation

You can install the package via composer:

composer require a50/event-dispatcher

Usage

For standalone usage example below. In your entity you need to register event:

<?php

declare(strict_types=1);

namespace YourProject\Domain;

use A50\EventDispatcher\EventRecordingCapabilities;

final class Post
{
    use EventRecordingCapabilities; // use trait to register and release events

    private function __construct(private PostId $id)
    {
    }

    public static function create(PostId $id): self
    {
        $self = new self($id);
        $self->registerThat(PostWasCreated::withId($id)); // register event

        return $self;
    }
}

Configure Psr\EventDispatcher\EventDispatcherInterface:

<?php

declare(strict_types=1);

require __DIR__ . 'vendor/autoload.php';

use A50\EventDispatcher\ImmutablePrioritizedListenerProvider;
use A50\EventDispatcher\ListenerPriority;
use A50\EventDispatcher\PrioritizedListenerProvider;
use A50\EventDispatcher\SyncEventDispatcher;

use YourProject\Domain\Post;
use YourProject\Domain\PostId;
use YourProject\Domain\PostWasCreated;
use YourProject\Application\SendEmailToModerator;

use YourProject\Domain\UserWasRegistered;
use YourProject\Application\SendWelcomeEmail;

$registry = new ImmutablePrioritizedListenerProvider([
    new PrioritizedListenerProvider(PostWasCreated::class, [
        ListenerPriority::NORMAL => new SendEmailToModerator(),
    ]),
    new PrioritizedListenerProvider(UserWasRegistered::class, [
        ListenerPriority::NORMAL => new SendWelcomeEmail(), 
    ]),
]);
$dispatcher = new SyncEventDispatcher($registry);

// And in your application use case:

$post = Post::create(
    PostId::fromString('00000000-0000-0000-0000-000000000000')
);

foreach ($post->releaseEvents() as $event) {
    $dispatcher->dispatch($event);
}

Of course that is better to use DI and you can take definitions from A50\EventDispatcher\EventDispatcherServiceProvider class. After that in your application you can easily inject Psr\EventDispatcher\EventDispatcherInterface.

Also, you can configure listeners for events pass them into config:

use A50\EventDispatcher\EventDispatcherConfig;
use YourProject\Domain\UserWasRegistered;
use YourProject\Application\SendEmailToModerator;

$config = EventDispatcherConfig::withDefaults()
    ->addEventListener(UserWasRegistered::class, SendEmailToModerator::class)
    ->listeners();

And even set priority (ListenerPriority::NORMAL by default):

use A50\EventDispatcher\EventDispatcherConfig;
use A50\EventDispatcher\ListenerPriority;
use YourProject\Application\SendEmailToModerator;
use YourProject\Domain\UserWasRegistered;

$config = EventDispatcherConfig::withDefaults()
    ->addEventListener(UserWasRegistered::class, SendEmailToModerator::class, ListenerPriority::HIGH)
    ->listeners();

Testing

make test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-07