Skip to content

Commit a2bc822

Browse files
committed
Merge pull request #2619 from Ocramius/feature/proxy-manager-bridge
Adding lazy services documentation as of symfony/symfony#7890
2 parents a981ae7 + 931091d commit a2bc822

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

components/dependency_injection/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
configurators
1515
parentservices
1616
advanced
17+
lazy_services
1718
workflow
1819

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. index::
2+
single: Dependency Injection; Lazy Services
3+
4+
Lazy Services
5+
=============
6+
7+
.. versionadded:: 2.3
8+
Lazy services were added in Symfony 2.3.
9+
10+
Configuring lazy services
11+
-------------------------
12+
13+
In some particular cases where a very heavy service is always requested,
14+
but not always used, you may want to mark it as ``lazy`` to delay its instantiation.
15+
16+
In order to have services to lazily instantiate, you will first need to install
17+
the `ProxyManager bridge`_:
18+
19+
.. code-block:: bash
20+
$ php composer.phar require symfony/proxy-manager-bridge:2.3.*
21+
22+
You can mark the service as ``lazy`` by manipulating its definitions:
23+
24+
.. configuration-block::
25+
26+
.. code-block:: yaml
27+
28+
services:
29+
foo:
30+
class: Acme\Foo
31+
lazy: true
32+
33+
.. code-block:: xml
34+
35+
<service id="foo" class="Acme\Foo" lazy="true" />
36+
37+
.. code-block:: php
38+
39+
$definition = new Definition('Acme\Foo');
40+
$definition->setLazy(true);
41+
$container->setDefinition('foo', $definition);
42+
43+
You can then require the service from the container::
44+
45+
$service = $container->get('foo');
46+
47+
At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same
48+
signature of the class representing the service.
49+
50+
.. note::
51+
52+
If you don't install the `ProxyManager bridge`_, the container will just skip
53+
over the ``lazy`` flag and simply instantiate the service as it would normally do.
54+
55+
The proxy gets initialized and the actual service is instantiated as soon as you interact
56+
in any way with this object.
57+
58+
Additional Resources
59+
--------------------
60+
61+
You can read more about how proxies are instantiated, generated and initialized in
62+
the `documentation of ProxyManager`_.
63+
64+
65+
.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager
66+
.. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern
67+
.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md

components/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* :doc:`/components/dependency_injection/configurators`
4040
* :doc:`/components/dependency_injection/parentservices`
4141
* :doc:`/components/dependency_injection/advanced`
42+
* :doc:`/components/dependency_injection/lazy_services`
4243
* :doc:`/components/dependency_injection/workflow`
4344

4445
* **DOM Crawler**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy