This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Delegate factories #4145
Merged
weierophinney
merged 6 commits into
zendframework:develop
from
Ocramius:feature/eager-services
Apr 15, 2013
Merged
Delegate factories #4145
weierophinney
merged 6 commits into
zendframework:develop
from
Ocramius:feature/eager-services
Apr 15, 2013
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
Actually, the factory does not produce delegates, but delegators, as explained in http://en.wikipedia.org/wiki/Delegation_pattern. I will change this after having prepared the docs PR. |
Good to go |
+1 on this! |
weierophinney
added a commit
that referenced
this pull request
Apr 15, 2013
ghost
assigned weierophinney
Apr 15, 2013
weierophinney
added a commit
to zendframework/zend-servicemanager
that referenced
this pull request
May 15, 2015
…ture/eager-services Delegate factories
weierophinney
added a commit
to zendframework/zend-servicemanager
that referenced
this pull request
May 15, 2015
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
I've been working on this concept for some time, but this is basically a cleaned up version of #2995
Why do we need this?
The idea is to be able to build delegates of any existing service. This can be used to build "decorators" for service instantiation and for proxying.
Delegation
Imagine that you have a
DbConnection
and aLoggedDbConnection
object. ALoggedDbConnection
wraps around a "real"DbConnection
instance like following, but provides additional API that sends queries to a logger:Currently, our problem is that such a wrapper requires us to completely re-define the factory that instantiates
DbConnection
. There's no way to re-use logic from such a factory. We have to rewrite it and be aware of its internal logic:Proxying
We can proxy any service (with the assumption that we know its class name or implemented interface) and make it "lazy", so that its dependencies don't get instantiated when we request them. This allows us to build lightweight containers, as explained in https://github.com/Ocramius/ProxyManager/blob/957de8648d64d2d0a3777fec793b52c198d39c82/docs/lazy-loading-value-holder.md. Also, we don't have to worry about how the original service is instantiated, since the original factory is still existing.
Example
In the following example, I've simply replaced the original service (instance of
stdClass
) with a new one that contains the original service in propertytab
:Implementation flaws
doCreate
, since I splittedcreate
to allow this kind of behaviordoCreate
is public, since I cannot use a closure and call->bindTo($this)
in PHP 5.3Documentation
A documentation PR has been provided at zendframework/zf2-documentation#825