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

[11.x] Add wildcard directory discovery to the EventServiceProvider #53932

Merged

Conversation

jared-cannon
Copy link
Contributor

(Most of this post is copied from my Ideas discussion here: #53893 (comment))

Overview

As someone who uses the "Features/" project structure for larger applications, a way to automatically scan each new feature's Listeners directory is a nice, albeit simple, QOL improvement. My suggested change lives in the EventServiceProvider->discoverEvents() function:

Implementation

/**
 * 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())
            );
        }, []);
}

Result

This would allow a user to add the following line to their withEvents() function:

->withEvents(discover: [
    __DIR__.'/../app/Features/*/Listeners',
])

Tests

I could not find an existing test class for the EventServiceProvider, but I can create one if necessary.

@jared-cannon jared-cannon changed the title Add wildcard directory discovery to the EventServiceProvider [11.x] Add wildcard directory discovery to the EventServiceProvider Dec 16, 2024
@taylorotwell taylorotwell merged commit fd07e20 into laravel:11.x Dec 17, 2024
40 checks passed
@jared-cannon jared-cannon deleted the add-wildcard-event-discovery branch December 17, 2024 20:30
dallyger added a commit to dallyger/laravel-docs that referenced this pull request Feb 13, 2025
Add documentation for the feature introduced in
<laravel/framework#53932>.
dallyger added a commit to dallyger/laravel-docs that referenced this pull request Feb 13, 2025
Add documentation for the feature introduced in
<laravel/framework#53932>.
dallyger added a commit to dallyger/laravel-docs that referenced this pull request Feb 13, 2025
Add documentation for the feature introduced in
<laravel/framework#53932>.
taylorotwell added a commit to laravel/docs that referenced this pull request Feb 14, 2025
* feat: wildcards in event discovery paths

Add documentation for the feature introduced in
<laravel/framework#53932>.

* Update events.md

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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.

None yet

2 participants