Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add Macroable trait to the Dispatcher #31317

Merged
merged 1 commit into from
Feb 3, 2020
Merged

[6.x] Add Macroable trait to the Dispatcher #31317

merged 1 commit into from
Feb 3, 2020

Conversation

timrspratt
Copy link
Contributor

The reason behind this, is I've recently needed a way of getting all events. Rather than extending the register my own Dispatcher in place of the Illuminate one, I think this would be a cleaner approach.

@timrspratt timrspratt changed the title Make the Dispatcher macroable [6.x] Add Macroable trait to the Dispatcher Feb 1, 2020
@GrahamCampbell
Copy link
Member

Why do you specifically need the methods to be on this class?

@timrspratt
Copy link
Contributor Author

timrspratt commented Feb 1, 2020

@GrahamCampbell I'm currently doing the following to get all events that are of a certain type (where the listener event is of a class):

class Dispatcher extends \Illuminate\Events\Dispatcher
{
    public function getManagedEvents(): Collection
    {
        return collect($this->listeners)->filter(static function ($_, $event) {
            return is_subclass_of($event, ManagedEvent::class);
        });
    }
}

I thought making the Dispatcher extendible will allow other developers to be flexible, but another option would be to modify getListeners (or create a new public method), so that it can accept a closure as well as the event name.

public function getListeners($eventName)
{
    if (is_callable($eventName)) {
        $listeners = array_filter($this->listeners, $eventName);
    } else {
        $listeners = $this->listeners[$eventName] ?? [];
    }

    ...
}

My concern with the second solution is that the wildcard and interface listeners may pollute the list once filtered, for what I need anyway. Essentially I need a way of getting all events that are registered in the dispatcher that extend a specific class.

@taylorotwell taylorotwell merged commit caf2f2e into laravel:6.x Feb 3, 2020
@timrspratt timrspratt deleted the patch-1 branch February 3, 2020 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants