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

[7.x] Improve event subscribers #33191

Merged
merged 1 commit into from
Jun 12, 2020
Merged

Conversation

imanghafoori1
Copy link
Contributor

@imanghafoori1 imanghafoori1 commented Jun 11, 2020

This allows the event subscribers to return an array like the $listen property on the EventsServiceProvider
In fact, it introduces some syntactic sugar, by removing $events->listen(

also improves the doc-block of the public interface.

The example in laravel documentation:

    public function subscribe($events)
    {
        $events->listen(
            'App\Events\UserLoggedIn',
            'App\Listeners\UserEventListener@onUserLogin'
        );

        $events->listen(
            'App\Events\UserLoggedOut',
            'App\Listeners\UserEventListener@onUserLogout'
        );
    }

Can be refactored to:

    public function subscribe()
    {
         return [
            'App\Events\UserLoggedIn' => ['App\Listeners\UserEventListener@onUserLogin'],
            'App\Events\UserLoggedOut' =>  ['App\Listeners\UserEventListener@onUserLogout']
         ];
    }

This is mostly backward compatible except in case someone returns a sequential array from the subscriber for no reason.
(It is possible to make is fully backward compatible by checking the array is assoc but it has performance hit, in the boot phase.)


if (is_array($events)) {
foreach ($events as $event => $listeners) {
foreach (array_unique($listeners) as $listener) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 for the array_unique

Copy link
Contributor Author

@imanghafoori1 imanghafoori1 Jun 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach ($events as $event => $listeners) {

this is copied from the above link, I kept it an exact copy, in case you want to extract the logic into a single method, to be reused.
If it is to be removed, better to be removed from both places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in the service provider for a different reason. It is not needed here. I will remove it.

@imanghafoori1 imanghafoori1 force-pushed the subscribers branch 3 times, most recently from 47fb6cf to f9109fb Compare June 12, 2020 09:39
@taylorotwell taylorotwell merged commit 929d326 into laravel:7.x Jun 12, 2020
@imanghafoori1 imanghafoori1 deleted the subscribers branch June 12, 2020 14:05
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.

4 participants