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

[8.x] Allow to override discover events base path #36515

Merged
merged 2 commits into from
Mar 9, 2021
Merged

[8.x] Allow to override discover events base path #36515

merged 2 commits into from
Mar 9, 2021

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Mar 9, 2021

When customizing the app's directory structure event discovery fails as it uses base_path() to remove the "real path" part from a class file and tries to guess the class name from the remaining path.

For example, if one is following "Laravel Beyond Crud" book, from Spatie, proposed file structure, and have an event placed at ./src/App/Events/UserCreated.php, the DiscoverEvents class will guess the namespace of that class as Src\App\Events\UserCreated, which PHP won't find as a class.

This PR allows a developer to override in their local EventServiceProvider the base path used for event discovery by adding a protected method they can override and provide a custom base path to be used by the DiscoverEvents class.

EDIT no tests were added as the test for DiscoverEvents is already agnostic about the base path used.

reference:

public function testEventsCanBeDiscovered()
{
class_alias(Listener::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener');
class_alias(AbstractListener::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\AbstractListener');
class_alias(ListenerInterface::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\ListenerInterface');
$events = DiscoverEvents::within(__DIR__.'/Fixtures/EventDiscovery/Listeners', getcwd());
$this->assertEquals([
EventOne::class => [
Listener::class.'@handle',
Listener::class.'@handleEventOne',
],
EventTwo::class => [
Listener::class.'@handleEventTwo',
],
], $events);
}

@rodrigopedra rodrigopedra changed the title Allow to override discover events base path [8.x] Allow to override discover events base path Mar 9, 2021
@taylorotwell taylorotwell merged commit d9a33b1 into laravel:8.x Mar 9, 2021
@rodrigopedra rodrigopedra deleted the patch-1 branch March 9, 2021 15:32
This was referenced Mar 10, 2021
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