mildabre/event-dispatcher 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mildabre/event-dispatcher

最新稳定版本:v0.1.1

Composer 安装命令:

composer require mildabre/event-dispatcher

包简介

Simple event dispatcher for Nette based applications.

README 文档

README

License: MIT PHP Version

Simple, fresh, and modern event dispatcher with attribute-based event listener registration for Nette Framework applications.

Features

  • Attribute-based registration - Use PHP 8 attributes to mark events and listeners
  • Simple API - Minimal configuration, maximum productivity
  • Nette DI integration - Seamless integration with Nette Dependency Injection
  • Lightweight - No bloat, just what you need
  • Event Listener Discovery - Automatic listener registration

Installation

Install via Composer:

composer require mildabre/event-dispatcher

Configuration

Register the extension in your common.neon:

extensions:
    events: Mildabre\EventDispatcher\DI\EventDispatcherExtension

Optional dispatch disabling:

events:
    enabled: false       # default: true

Define an Event

Create an event as value object (final readonly class) with class and mark it with the #[Event] attribute.

<?php

use Mildabre\EventDispatcher\Attributes\Event;

#[Event]
final readonly class UserRegistered
{
    public function __construct(
        public string $email,
        public \DateTimeImmutable $registeredAt,
    ) {}
}

Create an Event Listener

Create a listener class with a handle() method that accepts your event:

<?php

use Mildabre\ServiceDiscovery\Attributes\EventListener;

#[EventListener]
class SendWelcomeEmail
{
    public function __construct(
        private EmailService $emailService,
    ) {}

    public function handle(UserRegistered $event): void
    {
        $this->emailService->sendWelcome($event->email);
    }
}

Dispatch Events

Inject the EventDispatcher and dispatch your events:

<?php

use Mildabre\EventDispatcher\EventDispatcher;

class UserService
{
    public function __construct(
        private EventDispatcher $eventDispatcher,
    ) {}

    public function register(string $email, string $password): void
    {
        // ... registration logic ...
        
        $this->eventDispatcher->dispatch(
            new UserRegistered($email, new \DateTimeImmutable())
        );
    }
}

How It Works

  1. Event Classes must be annotated with #[Event] attribute
  2. Listener Classes must be tagged with #[EventListener] attribute (from mildabre/service-discovery)
  3. Listener Methods must be named handle() and accept exactly one parameter of the event class type
  4. Auto-registration happens automatically via Nette DI - listeners are discovered and registered at compile time

Architecture

The event dispatcher follows a simple but powerful architecture:

Event → EventDispatcher → Listeners
  ↓            ↓              ↓
#[Event]   Auto-wired   #[EventListener]
              via           with handle()
           Nette DI           method

Requirements

  • PHP >= 8.1
  • nette/di ^3.1
  • nette/schema ^1.2
  • mildabre/service-discovery ^0.1

License

This project is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固