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

[9.x] Allow maintenance mode events to be listened to in closure based listeners #44417

Merged
merged 1 commit into from
Oct 3, 2022

Conversation

lioneaglesolutions
Copy link
Contributor

I'm not sure if this is a bug as such but I ran into this today.

I was trying to listen to MaintenanceModeDisabled::class and broadcast this (which I don't think works in maintenance mode anyway) but nonetheless, was faced with this error when registering my listener as usual, with the event type-hinted in the handler so I could then dispatch my own event to broadcast it;

class MaintenanceModeDisabledListener
{
    public function handle(\Illuminate\Foundation\Events\MaintenanceModeDisabled $event)
    {
        MyCustomMaintenanceModeDisabledEvent::dispatch();
    }
}

This was met with an error;
Too few arguments to function ....\MaintenanceModeDisabledListener::handle(), 0 passed in .../vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php on line 441 and exactly 1 expected

Upon further experimenting I realised that the following listener would also fail and mean you couldn't listen to these events using closures;

Event::listen(function (MaintenanceModeDisabled $event) {
    // custom logic here
});

This behaviour is confirmed with the following failing tests;

public function testDisabledEventCanBeListenedTo()
{
    file_put_contents(
        storage_path('framework/down'),
        json_encode([
            'retry' => 60,
            'refresh' => 60,
        ])
    );

    Event::listen(function (MaintenanceModeDisabled $event) {
        $this->assertTrue(true);
    });

    $this->artisan(UpCommand::class);
}

public function testEnabledEventCanBeListenedTo()
{
    Event::listen(function (MaintenanceModeEnabled $event) {
        $this->assertTrue(true);
    });

    $this->artisan(DownCommand::class);
}

By instantiating the events for dispatch, the events can be listened to using closures. I also couldn't really find anywhere else in the framework where ->dispatch($eventClassNameAsString) was used so hoped that this would be the solution. I wasn't sure if any tests were relevant or required.

@taylorotwell taylorotwell merged commit 5f89ab0 into laravel:9.x Oct 3, 2022
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.

2 participants