PHP code example of ringierimu / event-bus

1. Go to this page and download the library: Download ringierimu/event-bus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ringierimu / event-bus example snippets


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
            ]);
    }
}

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();
    }
}

/**
 * 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
        ]);
}

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';
    }
}

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';
    }
}
bash
php artisan vendor:publish --provider=Ringierimu\EventBus\EventBusServiceProvider