Add wildcard support to event discovery #53893
-
OverviewAs someone who uses the "Features/" project structure for my larger applications, a way to automatically scan each new feature's Listeners directory is a nice, albeit simple, QOL improvement. My suggested change would live in the Code change/**
* Discover the events and listeners for the application.
*
* @return array
*/
public function discoverEvents(): array
{
return (new Collection($this->discoverEventsWithin()))
->flatMap(function ($directory) {
// Use glob to expand wildcard patterns into directory paths
return glob($directory, GLOB_ONLYDIR);
})
->reject(function ($directory) {
return ! is_dir($directory);
})
->reduce(function ($discovered, $directory) {
return array_merge_recursive(
$discovered,
DiscoverEvents::within($directory, $this->eventDiscoveryBasePath())
);
}, []);
} ResultThis would allow a user to add the following line to their ->withEvents(discover: [
__DIR__.'/../app/Features/*/Listeners',
]) AddendumThis is my first proposed change for Laravel. After reading the contributing documentation, I wasn't sure whether I needed to open an issue before submitting a PR - I'd appreciate any guidance on this. I'm happy to create a PR with this change along with an update to the documentation describing this functionality. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Personally I think that Laravel should provide much better support for large scale applications in which large sections need to be structured as modules or features. In essence it should be possible by either Edit: Also to avoid unnecessary scans of these directories on every Laravel invocations - and with e.g. Livewire these can be very frequent very small invocations - every type of automatic search like this should be cached. (I would even go so far as to say that wherever possible a low cost hash of the source should be created and the cache should be refreshed automatically when the hash changes.) Providing recursion is disallowed, I see no performance problems - scanning I had experience looking at some packages that attempt to add this "module" functionality, and one that implicitly used a recursive directory structure The important thing for me is that this needs to be a) part of Laravel core, and b) performs well, and c) flexible, and d) easy to code. |
Beta Was this translation helpful? Give feedback.
-
Resolved with PR #53932 (comment) |
Beta Was this translation helpful? Give feedback.
-
@jared-cannon Jared - your PR was merged - CONGRATULATIONS!!!!! |
Beta Was this translation helpful? Give feedback.
Resolved with PR #53932 (comment)