laravel-activitylog
laravel-activitylog copied to clipboard
Scoped binding loses `resolveUsing` callback
Describe the bug Originally asked @AlexVanderbist about this here.
It's possible to specify CauserResolver::resolveUsing(...) when the app boots, but scoped bindings are stored in an array on the app instance and cleared out after each job. This results in the "causer" not being resolved because resolveUsing is no longer set.
The scoped bindings are reset here and all it does is remove the scoped instance, so when you request the same scoped binding again, it just creates a new instance. But there's no way to tap into that new instance to set resolveUsing again. (At least not that I've seen.)
For more context, we are using this feature to determine the causer in jobs. We pass some info about the user with the job, using a feature inspired by https://github.com/spatie/laravel-interacts-with-payload. So, resolveUsing is set in the worker instance app boot, but then reset after each job, losing our causer resolver that gets the causer info from the job payload using a facade of our own.
To Reproduce
On app boot, call CauserResolver::resolveUsing(() => 'ANYTHING').
In a job, call CauserResolver::resolve() and the value is null.
Expected behavior
I'd expect to receive ANYTHING in the job.
Versions (please complete the following information)
- PHP: 8.1
- Database: mysql 5.7
- Laravel: 9
- Package: laravel-activitylog
BTW, effectively solved this with Queue::before(() => CauserResolver::resolveUsing(() => 'ANYTHING')), but this caused a lot of time debugging.
I don't think it's intuitive that the resolver is reset at any point after booting.
Hi again @joelvh, thanks for diving into this and reporting this issue. This was originally changed to make it easier to implement this package in a multi-tenancy application. Using the scoped binding a CauserResolver for tenant A would never be used in a later job for tenant B. However, all things considered this isn't very intuitive for 99% of use-cases for this package.
If you send a PR that reverts this change back to a regular singleton, I'll happily merge it and tag a new version.
Dear contributor,
because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.