-
Notifications
You must be signed in to change notification settings - Fork 391
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
Fix Laravel 11 event auto-discovery by registering listeners in AuditingServiceProvider #916
Fix Laravel 11 event auto-discovery by registering listeners in AuditingServiceProvider #916
Conversation
…compatibility Eliminate AuditingEventServiceProvider which interfered with L11 event auto-discovery, EventServiceProvider::shouldDiscoverEvents()
So, must It seems to me that it would no longer be used |
exactly! Look at the diff of this PR, it's already removed. |
Just stumbled into this myself, hope the PR gets merged soon. Following your suggestion, I worked around it without creating a new service provider and just calling laravels directly, forcing the use of the real class ->withProviders([
Illuminate\Foundation\Support\Providers\EventServiceProvider::class,
]) |
I have not yet upgraded my applications to L11. Is it confirmed that this both works and is backwards compatible? |
I'm only running on L11. But yes, I have run tests on both L10 (v10.48.8) and L11 (v11.4.0) on PHP 8.3, both successfully. Afaik, extending from This PR was done together with @pascalbaljet and reviewed by him as well. |
This works for me. @onlime Out of curiosity, is there a reason, you did not use the Event facade? Compatibility with L7-9? I am not familiar with package development. |
nope, no specific reason. The |
Interestingly they've archived the lumen template repository but the framework one is still going. Judging by https://github.com/laravel/lumen-framework/blob/11.x/src/Providers/EventServiceProvider.php, I'd say |
The solution in this PR is twtg 👍 We no longer recommend to extend the default event service provider. In fact we never recommended packages to do that in the first place.
Yeah since we no longer recommend to starting new Lumen projects. We're still updating the framework one for current apps. But our general recommendation is to move away from Lumen if you can. |
Please, release it on packagist |
@parallels999 Are you planning to release this soon? Thanks for merging. I can totally live with some more weeks/months running on Oh, just read the sad news on #925 – is there anybody out there who could take over @MortenDHansen's wonderful project? |
I'm not the maintainer, I do not have the necessary permissions
It is still unknown what will happen in the future. |
Laravel 11 ships with default Event Discovery, so it automatically finds and registers your event listeners by scanning your
app/Listeners
directory. So, you no longer need to extend theEventServiceProvider::shouldDiscoverEvents()
method.But currently, Laravel Auditing's
AuditingEventServiceProvider
breaks the auto-discovery.L11 picks the first service provider and calls the
shouldDiscoverEvents()
on that one to decide whether auto-discovery should be enabled or not. And because ofAuditingEventServiceProvider
extending the default\Illuminate\Foundation\Support\Providers\EventServiceProvider
, this packages event service provider is usually the first one in the app container (Illuminate\Foundation\Application::$serviceProviders
), so that method call will always result infalse
:(
'OwenIt\Auditing\AuditingEventServiceProvider'
!=='Illuminate\Foundation\Support\Providers\EventServiceProvider'
)So, basically, this package hijacks Laravel's event service auto-discovery altogether, and in a L11 app, we could only fix/override this, by registering our own
EventServiceProvider
that overrides above method:On Illuminate\Foundation\Application#L905-L910 you see, how Laravel just grabs the first serviceProvider, and
AuditingEventServiceProvider
actually is an instanceofIlluminate\Foundation\Support\Providers\EventServiceProvider
(as it extends it):I found no other package that directly extends Laravel's
EventServiceProvider
, and I assume this is "bad practice" (which I cannot prove as I didn't find any documentation about this) with such potential negative side-effects.This PR fixes it by ditching
AuditingEventServiceProvider
and moving the package's event listener registration to theAuditingServiceProvider::boot()
method. By not using Laravel'sEvent
facade, but usingapp('events')->listen()
instead, this should also work for Lumen and eliminates the conditionalclass_alias()
quirk in the previousAuditingEventServiceProvider
.Cheers, Pipo