ringierimu/event-bus

此包已被弃用且不再维护。没有建议的替代包。

将 Laravel 事件发送到事件总线

1.0.0 2023-08-15 16:58 UTC

This package is auto-updated.

Last update: 2023-10-20 21:15:59 UTC


README

Build Status StyleCI Total Downloads Latest Stable Version License

介绍

Laravel Event Bus 提供了一个简单接口,使得 Laravel 事件可以分发到 Ringier Event Bus

安装

使用 composer 安装包

composer require ringierimu/event-bus

接下来使用以下命令发布配置文件

php artisan vendor:publish --provider=Ringierimu\EventBus\EventBusServiceProvider

这将创建一个配置文件在 config/event-bus.php。您可以自由浏览此文件并根据您的应用程序需求进行更新。

使用以下变量替换更新您的 .env 文件中的正确值

RINGIER_SB_NODE_ID=123456789
RINGIER_SB_USER=event_bus_user
RINGIER_SB_PASSWORD=event_bus_password

我们鼓励您进一步查看 config/event-bus.php 文件以了解更多其他可用配置。

用法

要使 Laravel 事件可在总线上分发,您只需让您的事件类扩展 Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus 接口。然后,您需要在事件类上实现 toEventBus 方法;这将允许您配置事件如何发送到总线上,例如 payload、eventType、action 等。

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
}

最后,就像发送任何正常的 Laravel 事件一样发送您的事件。现在,您的事件将被发送到总线上。

namespace App\Http\Controllers;

use App\Events\ListingCreatedEvent;
use App\Http\Requests\StoreListingRequest;
use App\Models\Listing;
use Illuminate\Http\RedirectResponse;

class ListingController extends Controller
{
    /**
     * Store a new Listing.
     * 
     * @return RedirectResponse
     */
    public function store(StoreListingRequest $request): RedirectResponse
    {
        $listing = Listing::create($request->validated());
    
        // Event will automatically be dispatched onto the
        // bus as well.
        ListingCreatedEvent::dispatch($listing);
        
        return back();
    }
}

自定义事件类型

默认情况下,事件类型是作为事件类的名称发送的。然而,您可以通过使用 Ringierimu\EventBus\Event 类的 withEventType 方法来自定义类型名称。在您的 toEventBus 方法中执行以下操作

/**
 * Get the representation of the event for the EventBus.
 *
 * @param  Event  $event
 * @return Event
 */
public function toEventBus(Event $event): Event
{
    return $event
        ->withEventType('UserListingCreatedEvent')
        ->withPayload([
            'id' => $this->listing->id,
            'title' => $this->listing->title,
            'description' => $this->listing->description
        ]);
}

您还可以在您的 Laravel 事件类上实现一个 broadcastToEventBusAs 方法,但是请注意,withEventType 将优于 broadcastToEventBusAs

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
    
    /**
     * Get the event type name being sent to the event bus.
     *
     * @return string
     */
    public function broadcastToEventBusAs(): string
    {
        return 'UserListingCreatedEvent';
    }
}

自定义队列

默认情况下,所有发送的事件总线事件都在队列中处理。您的默认队列和连接将被用于发送任务,但是您可以通过在 .env 中添加以下内容来指定为处理事件而保留的专用队列和连接

RINGIER_SB_QUEUE=eventbus
RINGIER_SB_QUEUE_CONNECTION=redis

或者,您可以通过将 onQueueonConnection 方法添加到您的 Laravel 事件类上来按事件指定队列和连接。

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
    
    /**
     * Specify the queue name on which this event should be processed.
     *
     * @param  Event  $event
     * @return string
     */
    public function onQueue(Event $event): string
    {
        return 'eventbus';
    }
    
    /**
     * Specify the queue connection on which this event should be processed.
     *
     * @param  Event  $event
     * @return string
     */
    public function onConnection(Event $event): string
    {
        return 'redis';
    }
}

测试

phpunit