-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
[9.x] Chained forwarding #4
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Property assignment forwarding is removed, cause it doesn't make much sense to forward that. If you need to perform a property assignment, just create a new instance of Forwarding::enable()
->from(UserService::class)
->to(UserRepository::class);
$proxy = call(UserService::class);
$proxy->property = true; // `UserService::class`
$proxy->property; // Returns `true`
$proxy->methodMissingInServiceButExistInRepository(); // `UserRepository::class`.
$proxy->property; // `InstanceInteractionException` is thrown. |
olsza
reviewed
Jul 8, 2022
olsza
reviewed
Jul 8, 2022
This reverts commit a70e311.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
About
This PR introduces a new version of LEC package with code niceties and new features.
Instead of relying on the naming conventions, we can now explicitly define the classes we want to forward, and much to say, we can now chain it. 🔥
How it works?
We can enable the forwarding and define which class to assign in case of missing methods or properties in the base class.
Define the forwarding:
Make a proxy:
Call an existing method:
Call a non-existing method:
Call a non-existing method, but that doesn't exist also in
UserRepository::class
:So, why?
UserService
, because the method exists and it is expected.UserRepository
, because of the forwarding defined before.UserModel
, because the method also missing inUserRepository
(previous instance).Use cases?
Still, I'm too lazy to think where to put a query. I may do that in the model, but then move it to the service if additional logic arrives. Then all calls stay untouched. I can manage forwardings at any time and without relying on naming conventions. Separation of concerns matters. 🙂
Caveats
Always take attention to which call instance you are working currently when calling methods, accessing, or assigning properties. If some of them don't exist, the proxy will swap an instance and you'll lose the internal state. I'm thinking about storing a previous instance or warning a developer in such a case, we'll see if it fits reality.