承接 php-pico/event 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

php-pico/event

Composer 安装命令:

composer require php-pico/event

包简介

A PSR-14 Event Dispather package.

README 文档

README

A PSR-14 Event Dispatcher package.

Installation

composer require php-pico/event

Requires PHP ^8.5.

Usage

Registering listeners

use PhpPico\Event\ListenerProvider;

$listenerProvider = new ListenerProvider();

$listenerProvider->listen(UserRegistered::class, function (UserRegistered $event): void {
    // handle the event
});

Listeners also receive events for classes implementing the registered interface, or extending the registered class:

$listenerProvider->listen(Throwable::class, function (Throwable $event): void {
    // invoked for any Throwable, including subclasses
});

Dispatching events

use PhpPico\Event\EventDispatcher;

$dispatcher = new EventDispatcher($listenerProvider);

$dispatcher->dispatch(new UserRegistered($user));

Listeners are invoked in the order they were registered.

Stoppable events

Events implementing Psr\EventDispatcher\StoppableEventInterface can halt propagation to remaining listeners:

use Psr\EventDispatcher\StoppableEventInterface;

final class UserRegistered implements StoppableEventInterface
{
    private bool $stopped = false;

    public function stop(): void
    {
        $this->stopped = true;
    }

    public function isPropagationStopped(): bool
    {
        return $this->stopped;
    }
}

Once stop() is called from within a listener, no further listeners are invoked for that dispatch.

Extend PhpPico\Event\AbstractStoppableEvent to get this behavior for free:

use PhpPico\Event\AbstractStoppableEvent;

final class UserRegistered extends AbstractStoppableEvent
{
    public function __construct(
        public readonly User $user,
    ) {}
}

AbstractStoppableEvent already implements stop() and isPropagationStopped(), so the event class only needs to declare its own data.

Testing

composer test

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固