-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Adding lazy services documentation as of symfony/symfony#7890 #2619
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ | |
configurators | ||
parentservices | ||
advanced | ||
lazy_services | ||
workflow | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
.. index:: | ||
single: Dependency Injection; Lazy Services | ||
|
||
Lazy Services | ||
============= | ||
|
||
.. versionadded:: 2.3 | ||
Lazy services were added in Symfony 2.3. | ||
|
||
Configuring lazy services | ||
------------------------- | ||
|
||
In some particular cases where a very heavy service is always requested, | ||
but not always used, you may want to mark it as ``lazy`` to delay its instantiation. | ||
|
||
In order to have services to lazily instantiate, you will first need to install | ||
the `ProxyManager bridge`_:: | ||
|
||
php composer.phar require symfony/proxy-manager-bridge:2.3.* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all commands should start with |
||
|
||
You can mark the service as ``lazy`` by manipulating its definitions: | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this empty line |
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
services: | ||
foo: | ||
class: Example\Foo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer |
||
lazy: true | ||
|
||
.. code-block:: xml | ||
|
||
<service id="foo" class="Example\Foo" lazy="true" /> | ||
|
||
.. code-block:: php | ||
|
||
$definition = new Definition('Example\Foo'); | ||
$definition->setLazy(true); | ||
$container->setDefinition('foo', $definition); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer $container->register('foo', 'Acme\Foo')
->setLazy(true); |
||
|
||
You can then require the service from the container:: | ||
|
||
$service = $container->get($serviceId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ``->get('foo');` |
||
|
||
At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same | ||
signature of the class representing the service. | ||
|
||
.. note:: | ||
|
||
If you don't install the `ProxyManager bridge`_, the container will just skip | ||
over the ``lazy`` flag and simply instantiate the service as it would normally do. | ||
|
||
The proxy gets initialized and the actual service is instantiated as soon as you interact | ||
in any way with this object. | ||
|
||
Additional resources | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
-------------------- | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this empty line |
||
You can read more about how proxies are instantiated, generated and initialized in | ||
the `documentation of ProxyManager`_. | ||
|
||
|
||
.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager | ||
.. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern | ||
.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be a single colon and
.. code-block:: bash