master

laravel/framework

Last updated at: 29/12/2023 09:27

Dispatchable.php

TLDR

This file contains a trait called "Dispatchable" within the Illuminate\Foundation\Events namespace. It provides methods for dispatching and broadcasting events.

Methods

dispatch

This method dispatches an event with the given arguments by calling the event function. It returns the result of the event call.

dispatchIf

This method dispatches an event with the given arguments if the provided boolean value is true. It uses the event function to dispatch the event. If the boolean value is false, the event is not dispatched.

dispatchUnless

This method dispatches an event with the given arguments unless the provided boolean value is true. It uses the event function to dispatch the event. If the boolean value is true, the event is not dispatched.

broadcast

This method broadcasts the event with the given arguments by calling the broadcast function. It returns a PendingBroadcast instance.

<?php

namespace Illuminate\Foundation\Events;

trait Dispatchable
{
    /**
     * Dispatch the event with the given arguments.
     *
     * @return mixed
     */
    public static function dispatch()
    {
        return event(new static(...func_get_args()));
    }

    /**
     * Dispatch the event with the given arguments if the given truth test passes.
     *
     * @param  bool  $boolean
     * @param  mixed  ...$arguments
     * @return mixed
     */
    public static function dispatchIf($boolean, ...$arguments)
    {
        if ($boolean) {
            return event(new static(...$arguments));
        }
    }

    /**
     * Dispatch the event with the given arguments unless the given truth test passes.
     *
     * @param  bool  $boolean
     * @param  mixed  ...$arguments
     * @return mixed
     */
    public static function dispatchUnless($boolean, ...$arguments)
    {
        if (! $boolean) {
            return event(new static(...$arguments));
        }
    }

    /**
     * Broadcast the event with the given arguments.
     *
     * @return \Illuminate\Broadcasting\PendingBroadcast
     */
    public static function broadcast()
    {
        return broadcast(new static(...func_get_args()));
    }
}