lithemod/events 问题修复 & 功能扩展

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

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

lithemod/events

Composer 安装命令:

composer require lithemod/events

包简介

Lithe Events: A powerful and lightweight event management system for PHP projects, designed to simplify communication between components intuitively and efficiently. Easily create, register, and emit events, making your code more modular and reactive. Transform your applications into dynamic experie

README 文档

README

Lithe Events is a lightweight event handling library designed for PHP applications. It provides a straightforward way to create and manage events, supporting a decoupled architecture that enhances the maintainability and flexibility of your applications.

Table of Contents

Installation

To install lithemod/events, use Composer. Run the following command in your project directory:

composer require lithemod/events

This command will download the package and update your composer.json file accordingly.

Usage

Using the EventDispatcher Class

The EventDispatcher class serves as the core for managing events and listeners. Here's how to use it effectively:

Creating an Event

To create an event, instantiate the Event class:

use Lithe\Events\Event;

$event = new Event('event.name', ['key' => 'value']);
  • Parameters:
    • event.name: A string that identifies the event.
    • ['key' => 'value']: An optional associative array to store additional event data.

Registering Listeners

You can create an instance of EventDispatcher and register a listener for an event using the on method. A listener is a callable executed when the event is emitted.

use Lithe\Events\EventDispatcher;

$dispatcher = new EventDispatcher();

$listener = function ($data) {
    echo "Event data: " . json_encode($data);
};

// Register the listener
$dispatcher->on('event.name', $listener);

Emitting Events

To emit an event and trigger all registered listeners, use the emit method:

$event = new Event('event.name', ['key' => 'value']);
$dispatcher->emit($event);

Removing Listeners

If you need to remove a listener from an event, use the off method:

$dispatcher->off('event.name', $listener);

Using Orbis Event Functions

In addition to using the EventDispatcher, you can utilize helper functions provided by the Orbis namespace for convenient event management.

Registering Listeners

To register a listener for an event using Orbis, use the on function:

use Lithe\Orbis\Events;

$listener = function ($data) {
    echo "Event data: " . json_encode($data);
};

// Register the listener
Events\on('event.name', $listener);

Emitting Events

To emit an event using Orbis, call the emit function:

use Lithe\Orbis\Events;
use Lithe\Events\Event;

$event = new Event('event.name', ['key' => 'value']);
Events\emit($event);

Removing Listeners

To remove a registered listener with Orbis, use the off function:

Events\off('event.name', $listener);

Example

Here's a complete example of how to use lithemod/events with the Orbis event functions:

use Lithe\Events\Event;
use Lithe\Orbis\Events;

// Create a listener
$listener = function ($data) {
    echo "Event received with data: " . json_encode($data) . "\n";
};

// Register the listener
Events\on('my.event', $listener);

// Emit the event
$data = ['msg' => 'Hello, world!'];
Events\emit(new Event('my.event', $data));

// Remove the listener
Events\off('my.event', $listener);

License

This project is licensed under the MIT License.

lithemod/events 适用场景与选型建议

lithemod/events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 lithemod/events 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-20