owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
2.94k stars 382 forks source link

AuditingEventServiceProvider disables Laravel's Event Auto-Discovery #923

Closed ManuelLeiner closed 2 months ago

ManuelLeiner commented 2 months ago
Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 11.4.0
Package version 13.6.5
PHP version 8.3.6

Actual Behaviour

I've already filed an extensive bug report in the laravel/framework repository, which has been rejected. I am not yet 100% convinced that this not at least partly a framework problem.

Once the auditing package is installed, Laravel's event auto-discovery no longer works. This means that events and listeners from my application will not be detected. I (want to) rely on auto-discovery.

I am pretty sure this is caused by the AuditingEventServiceProvider inheriting from the framework's base EventServiceProvider.

I have found that the framework's EventServiceProvider is no longer registered once another service provider inheriting from that base class is registered. This is the case for your service provider. I am still trying to figure out how the framework works and registers those service providers.

As the auditing service provider is registered instead the framework's service provider, this check in shouldDiscoverEvents will return false.

Expected Behaviour

Using your package, Laravel's EventServiceProvider is still registered and event auto-discovery works as expected.

Steps to Reproduce

  1. Fresh installation: https://laravel.com/docs/11.x/installation#sail-on-macos
  2. Follow setup
  3. ./vendor/bin/sail art make:event TestEvent
  4. ./vendor/bin/sail art make:listener TestListener
  5. Type hint the event (3) in the listener (4)
  6. ./vendor/bin/sail art event:list
  7. ./vendor/bin/sail composer require owen-it/laravel-auditing
  8. ./vendor/bin/sail art event:list
  9. ./vendor/bin/sail composer remove owen-it/laravel-auditing
  10. ./vendor/bin/sail art event:list

Check the output of (8) and (10) and you will see that at (8) the event and listener are discovered while at (10) they no longer are.

Please have a look at the code in the original bug report.

Possible Solutions

If this package would rely on Manual Registering Events, i.e. not inheriting and not using the $listen array anymore, but instead using the facade in any of the boot functions, I think the problem could be solved.

On the other hand, I still think this is a bug in the framework.

parallels999 commented 2 months ago
ManuelLeiner commented 2 months ago

Omg sorry .. this one time I do not check for open PRs.