定制 nimbly/announce 二次开发

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

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

nimbly/announce

最新稳定版本:2.1

Composer 安装命令:

composer require nimbly/announce

包简介

PSR-14 event dispatcher with subscriber support.

README 文档

README

Latest Stable Version GitHub Workflow Status Codecov branch License

A simple framework agnostic PSR-14 event dispatcher for your event-driven application.

Features

  • Uses PHP's #Attribute feature to register class methods as event handlers
  • Optional PSR-11 Container support
  • Full autowiring support for your subscribers and listeners - Announce will inject not just the event but any needed services or other dependencies from your container!

Installation

composer require nimbly/announce

Quick start

Create an event class

Your events can be standalone classes or they can extend the StoppableEvent abstract class. By extending the StoppableEvent abstract you gain the ability to stop event propogation if needed.

namespace App\Events;

use App\Models\User;
use Nimbly\Announce\Event;

class UserRegisteredEvent extends StoppableEvent
{
	public function __construct(public User $user)
	{
	}
}

Create a subscriber

Subscribers are classes that will handle your events. You can have as many subscribers as you would like.

To register a subscriber's method to handle a particular event or set of events, use the Nimbly\Announce\Subscribe attribute and pass in a comma separated list of event names to listen for.

namespace App\Subscribers;

use App\Events\UserRegisteredEvent;
use App\Services\EmailService;
use Nimbly\Announce\Subscribe;

class EmailSubscriber
{
	#[Subscribe(UserRegisteredEvent::class)]
	public function onUserRegistered(
		UserRegisteredEvent $event,
		EmailService $emailService): void
	{
		$emailService->send("registration_email", $event->user->email);
	}
}

Initiate Dispatcher

To register your subscriber's with the event dispatcher, pass in an array of class names or instances into the Dispatcher constructor.

You can also pass in a PSR-11 compliant container instance to be used in autowiring your subscribers as well as for event handlers on your subscribers.

$dispatcher = new Dispatcher(
	subscribers: [
		EmailSubscriber::class,
		new FooSubscriber,
	],
	container: $container
);

Dispatch event

To trigger an event, just call the dispatch method with the event instance.

$event = new UserRegisteredEvent($user);
$dispatcher->dispatch($event);

Stopping event propagation

If you need to stop event propagation during its lifetime, just call the stop() method on the event instance. The event will no longer be propagated to any further subscribed listeners. This requires the event to extend from the StoppableEvent abstract.

class UserRegisteredEvent extends StoppableEvent
{
	public function __construct(public User $user)
	{}
}
class EmailSubscriber
{
	#[Subscribe(UserRegisteredEvent::class)]
	public function onUserRegistered(
		UserRegisteredEvent $event,
		EmailService $emailService): void
	{
		$emailService->send("registration_email", $event->user->email);

		// Prevent any further handlers from processing this event
		$event->stop();
	}
}

Registering individual listeners

Alternatively, you can register individual events using the listen method.

$dispatcher = new Dispatcher;
$dispatcher->listen(
	UserRegisteredEvent::class,
	function(UserRegisteredEvent $event): void {
		// do some event stuff
	}
);

Wildcard listeners

You can register a "wild card" listener by using the * event name. This will subscribe to all events fired.

$dispatcher = new Dispatcher;
$dispatcher->listen(
	"*",
	function($event): void {
		// Respond to all events...
	}
);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固