Skip to content

Commit b2f3276

Browse files
committed
Move the Lazy services article to topic
1 parent 8ab3595 commit b2f3276

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
/components/dependency_injection/introduction /components/dependency_injection
253253
/components/dependency_injection/index /components/dependency_injection
254254
/components/dependency_injection/factories /service_container/factories
255+
/components/dependency_injection/lazy_services /service_container/lazy_services
255256
/components/event_dispatcher/introduction /components/event_dispatcher
256257
/components/expression_language/introduction /components/expression_language
257258
/components/expression_language/index /components/expression_language

components/dependency_injection/lazy_services.rst renamed to service_container/lazy_services.rst

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,20 @@ Installation
2626
------------
2727

2828
In order to use the lazy service instantiation, you will first need to install
29-
the `ProxyManager bridge`_:
29+
the ``ocramius/proxy-manager`` package:
3030

3131
.. code-block:: bash
3232
33-
$ composer require symfony/proxy-manager-bridge:~2.3
33+
$ composer require ocramius/proxy-manager
3434
3535
.. note::
3636

37-
If you're using the full-stack framework, the proxy manager bridge is
38-
already included but the actual proxy manager still needs to be installed:
37+
If you're not using the full-stack framework, you also have to install the
38+
`ProxyManager bridge`_
3939

4040
.. code-block:: bash
41-
42-
$ composer require ocramius/proxy-manager
4341
44-
Afterwards compile your container and check to make sure that you get
45-
a proxy for your lazy services.
42+
$ composer require symfony/proxy-manager-bridge:~2.3
4643
4744
Configuration
4845
-------------
@@ -54,9 +51,9 @@ You can mark the service as ``lazy`` by manipulating its definition:
5451
.. code-block:: yaml
5552
5653
services:
57-
foo:
58-
class: Acme\Foo
59-
lazy: true
54+
app.twig_extension:
55+
class: AppBundle\Twig\AppExtension
56+
lazy: true
6057
6158
.. code-block:: xml
6259
@@ -66,53 +63,44 @@ You can mark the service as ``lazy`` by manipulating its definition:
6663
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6764
6865
<services>
69-
<service id="foo" class="Acme\Foo" lazy="true" />
66+
<service id="app.twig_extension" class="AppBundle\Twig\AppExtension" lazy="true" />
7067
</services>
7168
</container>
7269
7370
.. code-block:: php
7471
7572
use Symfony\Component\DependencyInjection\Definition;
7673
77-
$definition = new Definition('Acme\Foo');
74+
$definition = new Definition('AppBundle\Twig\AppExtension');
7875
$definition->setLazy(true);
79-
$container->setDefinition('foo', $definition);
8076
81-
You can then require the service from the container::
77+
$container->setDefinition('app.twig_extension', $definition);
8278
83-
$service = $container->get('foo');
79+
Once you inject the service into another service, a virtual `proxy`_ with the
80+
same signature of the class representing the service should be injected. The
81+
same happends when calling ``Container::get()`` directly.
8482

85-
At this point the retrieved ``$service`` should be a virtual `proxy`_ with
86-
the same signature of the class representing the service. You can also inject
87-
the service just like normal into other services. The object that's actually
88-
injected will be the proxy.
83+
The actual class will be instantiated as soon as you try to interact with the
84+
service (e.g. call one of its methods).
8985

9086
To check if your proxy works you can simply check the interface of the
91-
received object.
87+
received object::
9288

93-
.. code-block:: php
94-
95-
var_dump(class_implements($service));
96-
97-
If the class implements the ``ProxyManager\Proxy\LazyLoadingInterface``
98-
your lazy loaded services are working.
89+
dump(class_implements($service));
90+
// the output should include "ProxyManager\Proxy\LazyLoadingInterface"
9991

10092
.. note::
10193

10294
If you don't install the `ProxyManager bridge`_ and the
10395
`ocramius/proxy-manager`_, the container will just skip over the ``lazy``
10496
flag and simply instantiate the service as it would normally do.
10597

106-
The proxy gets initialized and the actual service is instantiated as soon
107-
as you interact in any way with this object.
108-
10998
Additional Resources
11099
--------------------
111100

112101
You can read more about how proxies are instantiated, generated and initialized
113102
in the `documentation of ProxyManager`_.
114103

115-
116104
.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager
117105
.. _`proxy`: https://en.wikipedia.org/wiki/Proxy_pattern
118106
.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md

0 commit comments

Comments
 (0)