@@ -7,19 +7,41 @@ Lazy Services
|
7 | 7 | .. versionadded:: 2.3
|
8 | 8 | Lazy services were added in Symfony 2.3.
|
9 | 9 |
|
10 |
| -Configuring lazy services |
11 |
| -------------------------- |
| 10 | +Why Lazy Services? |
| 11 | +------------------ |
12 | 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. |
| 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``. |
15 | 19 |
|
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 |
17 | 29 | the `ProxyManager bridge`_:
|
18 | 30 |
|
19 | 31 | .. code-block:: bash
|
| 32 | +
|
20 | 33 | $ php composer.phar require symfony/proxy-manager-bridge:2.3.*
|
21 | 34 |
|
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: |
23 | 45 |
|
24 | 46 | .. configuration-block::
|
25 | 47 |
|
@@ -44,24 +66,27 @@ You can then require the service from the container::
|
44 | 66 |
|
45 | 67 | $service = $container->get('foo');
|
46 | 68 |
|
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. |
49 | 73 |
|
50 | 74 | .. note::
|
51 | 75 |
|
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. |
54 | 79 |
|
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. |
57 | 82 |
|
58 | 83 | Additional Resources
|
59 | 84 | --------------------
|
60 | 85 |
|
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`_. |
63 | 88 |
|
64 | 89 |
|
65 |
| -.. _`ProxyManager bridge`: https://.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager |
| 90 | +.. _`ProxyManager bridge`: https://.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager |
66 | 91 | .. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern
|
67 | 92 | .. _`documentation of ProxyManager`: https://.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md
|
0 commit comments