yiiboot/event-dispatcher

v1.0.0 2022-11-27 12:35 UTC

README

Yii Boot 事件调度器


使用 AsEventListener PHP 属性定义事件监听器的方法。这允许在类内部配置监听器,而无需在外部文件中添加任何配置

要求

  • PHP 8.1 或更高版本。

安装

此包可以通过 composer 安装

composer require yiiboot/event-dispatcher

一般用法

config/params.php

return [
    // ...
    'yiiboot/attributed' => [
        'paths' => [
            // ...
            dirname(__DIR__) . '/src/EventListener',
        ]
    ]
];

src/EventListener/MyMultiListener.php

namespace App\EventListener;

use Yiiboot\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
#[AsEventListener(event: FooEvent::class, priority: 42)]
#[AsEventListener(method: 'onBarEvent', group: ['console', 'web'], env: 'prod')]
final class MyMultiListener
{
    public function onCustomEvent(CustomEvent $event): void
    {
        // ...
    }

    public function onFooEvent(FooEvent $event): void
    {
        // ...
    }

    public function onBarEvent(BarEvent $event): void
    {
        // ...
    }

    #[AsEventListener]
    public function onMethodEvent(BarEvent $event): void
    {
        // ...
    }
}

测试

单元测试

此包使用 PHPUnit 进行测试。要运行测试

./vendor/bin/phpunit

突变测试

此包的测试使用 Infection 突变框架和 Infection 静态分析插件 进行检查。要运行它

./vendor/bin/roave-infection-static-analysis-plugin

静态分析

代码使用 Psalm 进行静态分析。要运行静态分析

./vendor/bin/psalm

代码风格

使用 Rector 使代码库遵循某些特定规则,或使用最新版或任何特定版本的 PHP

./vendor/bin/rector

依赖

使用 ComposerRequireChecker 检测 Composer 传递依赖。

许可

Yii Boot 事件调度器是自由软件。它根据 Apache-2.0 许可证的条款发布。有关更多信息,请参阅 LICENSE

Yii Boot 维护。

支持项目

Open Collective

关注更新

Official website

受启发 && 感谢