@@ -26,23 +26,20 @@ Installation
26
26
------------
27
27
28
28
In order to use the lazy service instantiation, you will first need to install
29
- the `ProxyManager bridge `_ :
29
+ the `` ocramius/proxy-manager `` package :
30
30
31
31
.. code-block :: bash
32
32
33
- $ composer require symfony /proxy-manager-bridge: ~ 2.3
33
+ $ composer require ocramius /proxy-manager
34
34
35
35
.. note ::
36
36
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 `_
39
39
40
40
.. code-block :: bash
41
-
42
- $ composer require ocramius/proxy-manager
43
41
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
46
43
47
44
Configuration
48
45
-------------
@@ -54,9 +51,9 @@ You can mark the service as ``lazy`` by manipulating its definition:
54
51
.. code-block :: yaml
55
52
56
53
services :
57
- foo :
58
- class : Acme\Foo
59
- lazy : true
54
+ app.twig_extension :
55
+ class : AppBundle\Twig\AppExtension
56
+ lazy : true
60
57
61
58
.. code-block :: xml
62
59
@@ -66,53 +63,44 @@ You can mark the service as ``lazy`` by manipulating its definition:
66
63
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
67
64
68
65
<services >
69
- <service id =" foo " class =" Acme\Foo " lazy =" true" />
66
+ <service id =" app.twig_extension " class =" AppBundle\Twig\AppExtension " lazy =" true" />
70
67
</services >
71
68
</container >
72
69
73
70
.. code-block :: php
74
71
75
72
use Symfony\Component\DependencyInjection\Definition;
76
73
77
- $definition = new Definition('Acme\Foo ');
74
+ $definition = new Definition('AppBundle\Twig\AppExtension ');
78
75
$definition->setLazy(true);
79
- $container->setDefinition('foo', $definition);
80
76
81
- You can then require the service from the container::
77
+ $ container->setDefinition('app.twig_extension', $definition);
82
78
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.
84
82
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).
89
85
90
86
To check if your proxy works you can simply check the interface of the
91
- received object.
87
+ received object::
92
88
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"
99
91
100
92
.. note ::
101
93
102
94
If you don't install the `ProxyManager bridge `_ and the
103
95
`ocramius/proxy-manager `_, the container will just skip over the ``lazy ``
104
96
flag and simply instantiate the service as it would normally do.
105
97
106
- The proxy gets initialized and the actual service is instantiated as soon
107
- as you interact in any way with this object.
108
-
109
98
Additional Resources
110
99
--------------------
111
100
112
101
You can read more about how proxies are instantiated, generated and initialized
113
102
in the `documentation of ProxyManager `_.
114
103
115
-
116
104
.. _`ProxyManager bridge` : https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager
117
105
.. _`proxy` : https://en.wikipedia.org/wiki/Proxy_pattern
118
106
.. _`documentation of ProxyManager` : https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md
0 commit comments