Skip to content

Commit 4557fee

Browse files
committed
[symfony#2619] Tweaks for new proxy/lazy services entry
(cherry picked from commit d7ea3a5)
1 parent bcc2fdd commit 4557fee

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

components/dependency_injection/lazy_services.rst

+40-15
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,41 @@ Lazy Services
77
.. versionadded:: 2.3
88
Lazy services were added in Symfony 2.3.
99

10-
Configuring lazy services
11-
-------------------------
10+
Why Lazy Services?
11+
------------------
1212

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.
13+
In some cases, you may want to inject a service that is a bit heavy to instantiate,
14+
but is not always used inside your object. For example, imagine you have
15+
a ``NewsletterManager`` and you inject a ``mailer`` service into it. Only
16+
a few methods on your ``NewsletterManager`` actually use the ``mailer``,
17+
but even when you don't need it, a ``mailer`` service is always instantiated
18+
in order to construct your ``NewsletterManager``.
1519

16-
In order to have services to lazily instantiate, you will first need to install
20+
Configuring lazy services is one answer to this. With a lazy service, a "proxy"
21+
of the ``mailer`` service is actually injected. It looks and acts just like
22+
the ``mailer``, except that the ``mailer`` isn't actually instantiated until
23+
you interact with the proxy in some way.
24+
25+
Installation
26+
------------
27+
28+
In order to use the lazy service instantiation, you will first need to install
1729
the `ProxyManager bridge`_:
1830

1931
.. code-block:: bash
32+
2033
$ php composer.phar require symfony/proxy-manager-bridge:2.3.*
2134
22-
You can mark the service as ``lazy`` by manipulating its definitions:
35+
.. note::
36+
37+
If you're using the full-stack framework, this package is not included
38+
and needs to be added to ``composer.json`` and installed (which is what
39+
the above command does).
40+
41+
Configuration
42+
-------------
43+
44+
You can mark the service as ``lazy`` by manipulating its definition:
2345

2446
.. configuration-block::
2547

@@ -44,24 +66,27 @@ You can then require the service from the container::
4466

4567
$service = $container->get('foo');
4668

47-
At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same
48-
signature of the class representing the service.
69+
At this point the retrieved ``$service`` should be a virtual `proxy`_ with
70+
the same signature of the class representing the service. You can also inject
71+
the service just like normal into other services. The object that's actually
72+
injected will be the proxy.
4973

5074
.. note::
5175

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.
76+
If you don't install the `ProxyManager bridge`_, the container will just
77+
skip over the ``lazy`` flag and simply instantiate the service as it would
78+
normally do.
5479

55-
The proxy gets initialized and the actual service is instantiated as soon as you interact
56-
in any way with this object.
80+
The proxy gets initialized and the actual service is instantiated as soon
81+
as you interact in any way with this object.
5782

5883
Additional Resources
5984
--------------------
6085

61-
You can read more about how proxies are instantiated, generated and initialized in
62-
the `documentation of ProxyManager`_.
86+
You can read more about how proxies are instantiated, generated and initialized
87+
in the `documentation of ProxyManager`_.
6388

6489

65-
.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager
90+
.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager
6691
.. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern
6792
.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md

0 commit comments

Comments
 (0)