Skip to content

[13.x] Allow auto discovered listeners to opt out of discovery#60209

Open
jackbayliss wants to merge 3 commits into
laravel:13.xfrom
jackbayliss:13.x-should-be-discovered
Open

[13.x] Allow auto discovered listeners to opt out of discovery#60209
jackbayliss wants to merge 3 commits into
laravel:13.xfrom
jackbayliss:13.x-should-be-discovered

Conversation

@jackbayliss
Copy link
Copy Markdown
Contributor

@jackbayliss jackbayliss commented May 20, 2026

Good day!

This PR adds a ShouldBeDiscovered interface that lets listeners decide themselves if they should be registered during discovery... used an interface so I'm not causing any B/C.

I want auto discovery, cause it's easy, but I want to purposefully not register some events in certain environments, I can do it in the handle, but it felt repetitive so thought this could be nice... as avoids discovery completely

This is especially helpful for listeners that implement ShouldQueue, as it's not completely obvious the logic in the handle will still get queued.

Code example:

Details

Idea being it just stops it being discovered at all.., no need to think about shouldQueue, we just dont want it for this env..

  use Illuminate\Contracts\Events\ShouldBeDiscovered;
  use Illuminate\Contracts\Queue\ShouldQueue;

  class CustomerRegisteredListener implements ShouldBeDiscovered, ShouldQueue
  {
      public static function shouldBeDiscovered(): bool
      {
          return app()->environment('production');
      }

      // Before the check would be in the handle, but still sent it to the queue.
      public function handle(CustomerRegistered $event): void
      {
          Http::post(config('services.salesforce.webhook'), [
              'email' => $event->customer->email,
          ]);
      }
  }

Why not just use a subscriber? ... Cause generally you use it to subscribe for multiple events, if you only have one, it feels (at least to me) a bit overcomplicated. I wanna let the auto discover handle it.

Also don't really wanna Event:forget as that too feels a bit.. meh

I just want a way to say "Dont listen to this.. due to XYZ" - ie a environment, feature etc. This provides a lightweight alternative essentially. The method is purposefully static to not rely on state.

Note- you can still manually Event::listen, but this is about automagic discovery! This also works with optimize etc

Open to renaming, a better way, or throwing this away I am but one man from England with ideas!

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review May 20, 2026 23:21
@jackbayliss jackbayliss changed the title [13.x] Allow listeners to opt out of discovery [13.x] Allow auto discovered listeners to opt out of discovery May 20, 2026
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.

1 participant