定制 effectra/event-dispatcher 二次开发

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

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

effectra/event-dispatcher

最新稳定版本:v1.1.0

Composer 安装命令:

composer require effectra/event-dispatcher

包简介

The Effectra Event Dispatcher package.

README 文档

README

Effectra\EventDispatcher is a versatile and lightweight event dispatching library for PHP, designed to simplify the management of events within your application. It adheres to the PSR-14 standard, providing a consistent and extensible way to handle events, listeners, and event propagation.

Features

  • PSR-14 Compliance: Follows the PSR-14 standard for event dispatching, ensuring interoperability with other PHP packages and frameworks.
  • Stoppable Events: Supports stoppable events, allowing events to be halted during propagation if necessary.
  • Flexible Listener Providers: Easily register and manage listeners for specific events through a flexible listener provider interface.
  • Consistent Event Handling: Ensures synchronous execution of listeners in the order they are registered.
  • Simplified API: Provides a straightforward API for dispatching events, making it easy to integrate into your projects.

Installation

You can install Effectra\EventDispatcher via Composer. Run the following command in your project directory:

composer require effectra/event-dispatcher

Usage

1. Creating Events

Create your custom event classes by extending the Effectra\EventDispatcher\Event class. This base class implements the Psr\EventDispatcher\StoppableEventInterface, allowing events to be stoppable.

namespace YourNamespace\Events;

use Effectra\EventDispatcher\Event;

class CustomEvent extends Event
{
    // Your event properties and methods
}

2. Registering Listeners

Create listeners by defining callable functions or classes implementing the necessary logic. Register listeners using the Effectra\EventDispatcher\ListenerProvider class.

namespace YourNamespace;

use Effectra\EventDispatcher\ListenerProvider;
use YourNamespace\Events\CustomEvent;

// Create a listener provider
$listenerProvider = new ListenerProvider();

// Register a listener for the CustomEvent
$listenerProvider->addListener(CustomEvent::class, function (CustomEvent $event) {
    // Handle the CustomEvent
});

// Dispatching the event
$event = new CustomEvent();
$dispatcher = new EventDispatcher($listenerProvider);
$dispatcher->dispatch($event);

3. Stoppable Events

To create a stoppable event, use the stopPropagation() method within your event logic. This will prevent further listeners from being executed.

namespace YourNamespace\Events;

use Effectra\EventDispatcher\Event;

class StoppableEvent extends Event
{
    public function process(): void
    {
        // Your event processing logic

        // Stop further propagation if a condition is met
        if ($condition) {
            $this->stopPropagation();
        }
    }
}

Contributing

Contributions are welcome! Fork the repository, create a branch, make your changes, and then create a pull request. Please ensure your PR description clearly describes the changes you made.

License

This package is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-27