-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Self-registering event listeners #19917
Self-registering event listeners #19917
Conversation
@@ -54,6 +54,12 @@ public function register() | |||
*/ | |||
public function listens() | |||
{ | |||
return $this->listen; | |||
return array_merge_recursive(collect($this->listeners)->flatMap(function ($listener) { | |||
return collect($listener::$hears)->map(function ($event) use ($listener) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe $listener::$hears ?? []
?
Event generation adjusted to handle this properly. |
could this have gone to 5.4? doesn't seem like we're breaking anything since we're just merging the two events, right? |
use |
What about |
people, let's not bike shed this. the name is fine. |
I disagree; naming things is hard and worth a conversation. Might be better for me to just open a PR with the suggestion though instead of discussing it here. |
i agree wholeheartedly that naming things is important. problem here is you're going from an OK name to another OK name. so all of these suggestions are completely subjective. if you can provide some objective evidence why |
Good point, I should've justified it instead of just tossing out a name. The reason I prefer Great new feature regardless, though! One less level of indirection when tracing code. |
I like With
With
|
I think hears sound most out of line of all the following. I like Other than that, its a nice pr. Thnx ! |
If we're gonna bikeshed about this, I'd like to throw my suggestion into the ring.
Thank you. |
Still better than hears :P ... I'm afraid a simple suggestion is leading this pr towards becoming a troll thread... Sry about that 😬 |
This is still just an early idea, but was talking through it with Taylor and thought it'd be a good idea to open a PR for discussion.
The general idea is to allow you to specify which events your listeners listen to in the listener itself instead of creating a mapping in your
EventServiceProvider
. Then all you'd need to do is throw your listeners in an array in the service provider to register them.Here's what your
EventServiceProvider
might look like:And here's what a listener would look like:
We'd want to support backwards compatibility with the old approach, which this implementation does as far as I have tested.
Things to explore/figure out: