定制 webware/mezzio-eventdispatcher 二次开发

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

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

webware/mezzio-eventdispatcher

Composer 安装命令:

composer require webware/mezzio-eventdispatcher

包简介

League EventDispatcher

README 文档

README

PHP Version License

A PSR-14 Event Dispatcher library for Mezzio/Laminas applications, providing seamless integration between League Event and Laminas ServiceManager.

Features

  • PSR-14 Compliant - Full PSR-14 Event Dispatcher implementation
  • Laminas Integration - Native ServiceManager/Dependency Injection support
  • Configuration-Based - Register listeners and subscribers via configuration arrays
  • Immutable Events - Type-safe, immutable event objects with fluent API
  • Priority Support - Control listener execution order with priority levels
  • Type Safe - PHP 8.2+ with strict types, readonly classes, and comprehensive PHPStan Level 10 coverage
  • Fully Tested - 91 tests with comprehensive unit and integration coverage

Requirements

  • PHP 8.2, 8.3, 8.4, or 8.5
  • league/event ^3.0
  • PSR-11 Container implementation (e.g., Laminas ServiceManager)

Installation

composer require webware/mezzio-eventdispatcher

Quick Start

1. Register the ConfigProvider

Add the configuration provider to your application:

// config/config.php
use Webware\Event\ConfigProvider;

$aggregator = new ConfigAggregator([
    ConfigProvider::class,
    // ... other config providers
]);

return $aggregator->getMergedConfig();

2. Create an Event

use Webware\Event\ImmutableEvent;

// Immutable event (recommended for most use cases)
$event = new ImmutableEvent('user.created', $user, ['timestamp' => time()]);

// OR use mutable event when needed
use Webware\Event\MutableEvent;
$event = new MutableEvent('user.created', $user, ['timestamp' => time()]);

3. Create a Listener

use Webware\Event\EventInterface;
use Webware\Event\ListenerInterface;

class UserCreatedListener implements ListenerInterface
{
    public function __invoke(EventInterface $event): void
    {
        $user = $event->getTarget();
        $params = $event->getParams();

        // Handle the event...
    }
}

4. Register Listener in Configuration

// config/autoload/events.global.php
use Webware\Event\ConfigKey;
use Webware\Event\ListenerPriority;

return [
    ConfigKey::Listeners->value => [
        [
            'event' => 'user.created',
            'listener' => UserCreatedListener::class,
            'priority' => ListenerPriority::Normal->value,
        ],
    ],
    'dependencies' => [
        'factories' => [
            UserCreatedListener::class => YourListenerFactory::class,
        ],
    ],
];

5. Dispatch Events

use Psr\EventDispatcher\EventDispatcherInterface;
use Webware\Event\ImmutableEvent;

class UserService
{
    public function __construct(
        private EventDispatcherInterface $dispatcher
    ) {}

    public function createUser(array $data): User
    {
        $user = new User($data);

        // Dispatch immutable event
        $event = new ImmutableEvent('user.created', $user);
        $this->dispatcher->dispatch($event);

        return $user;
    }
}

Documentation

Comprehensive documentation is available in the docs/ directory:

Example

A complete example demonstrating all features:

use Psr\EventDispatcher\EventDispatcherInterface;
use Webware\Event\ImmutableEvent;

// In your service
class OrderService
{
    public function __construct(
        private EventDispatcherInterface $dispatcher
    ) {}

    public function placeOrder(Order $order): void
    {
        // Process order...
        $order->setStatus('completed');

        // Dispatch immutable event with context
        $event = new ImmutableEvent(
            name: 'order.completed',
            target: $order,
            params: [
                'user_id' => $order->getUserId(),
                'total' => $order->getTotal(),
                'timestamp' => time(),
            ]
        );

        $this->dispatcher->dispatch($event);
    }
}

// Listener 1: Send confirmation email
class OrderEmailListener implements ListenerInterface
{
    public function __invoke(EventInterface $event): void
    {
        $order = $event->getTarget();
        $this->emailService->sendOrderConfirmation($order);
    }
}

// Listener 2: Update inventory
class OrderInventoryListener implements ListenerInterface
{
    public function __invoke(EventInterface $event): void
    {
        $order = $event->getTarget();
        $this->inventoryService->decrementStock($order);
    }
}

// Configuration
return [
    ConfigKey::Listeners->value => [
        [
            'event' => 'order.completed',
            'listener' => OrderEmailListener::class,
            'priority' => ListenerPriority::Normal->value,
        ],
        [
            'event' => 'order.completed',
            'listener' => OrderInventoryListener::class,
            'priority' => ListenerPriority::High->value, // Runs first
        ],
    ],
];

Testing

This library includes comprehensive test suites:

# Run unit tests
composer test

# Run integration tests
composer test-integration

# Run all tests with coverage
composer test-coverage

# Run all quality checks (CS, PHPStan, tests)
composer check

Code Quality

  • PHPStan Level 10 - Maximum static analysis strictness
  • Laminas Coding Standard - PSR-12 compliant with additional quality rules
  • 100% Type Coverage - Strict types and comprehensive type hints
  • Comprehensive Tests - 91 tests covering all functionality

Contributing

Contributions are welcome! Please ensure:

  1. All tests pass: composer check
  2. Code follows Laminas Coding Standard: composer cs-check
  3. PHPStan Level 10 passes: composer static-analysis
  4. New features include tests and documentation

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

Credits

Support

webware/mezzio-eventdispatcher 适用场景与选型建议

webware/mezzio-eventdispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 194 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 webware/mezzio-eventdispatcher 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-10-18