From 50905ff253e5e1164b377ad5f285d1c439e7bdb7 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Tue, 7 May 2013 22:32:26 +0200 Subject: [PATCH 1/2] Adding lazy services documentation as of symfony/symfony#7890 --- components/dependency_injection/index.rst | 1 + .../dependency_injection/lazy_services.rst | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 components/dependency_injection/lazy_services.rst diff --git a/components/dependency_injection/index.rst b/components/dependency_injection/index.rst index e1d6b0eab8d..b3960eb8e00 100644 --- a/components/dependency_injection/index.rst +++ b/components/dependency_injection/index.rst @@ -14,5 +14,6 @@ configurators parentservices advanced + lazy_services workflow diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst new file mode 100644 index 00000000000..ca97b5cad24 --- /dev/null +++ b/components/dependency_injection/lazy_services.rst @@ -0,0 +1,68 @@ +.. index:: + single: Dependency Injection; Lazy Services + +Lazy Services +============= + +.. versionadded:: 2.3 + Lazy services were added in Symfony 2.3. + +Configuring lazy services +------------------------- + +In some particular cases where a very heavy service is always requested, +but not always used, you may want to mark it as ``lazy`` to delay its instantiation. + +In order to have services to lazily instantiate, you will first need to install +the `ProxyManager bridge`_:: + + php composer.phar require symfony/proxy-manager-bridge:2.3.* + +You can mark the service as ``lazy`` by manipulating its definitions: + + +.. configuration-block:: + + .. code-block:: yaml + + services: + foo: + class: Example\Foo + lazy: true + + .. code-block:: xml + + + + .. code-block:: php + + $definition = new Definition('Example\Foo'); + $definition->setLazy(true); + $container->setDefinition('foo', $definition); + +You can then require the service from the container:: + + $service = $container->get($serviceId); + +At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same +signature of the class representing the service. + +.. note:: + + If you don't install the `ProxyManager bridge`_, the container will just skip + over the ``lazy`` flag and simply instantiate the service as it would normally do. + +The proxy gets initialized and the actual service is instantiated as soon as you interact +in any way with this object. + +Additional resources +-------------------- + + +You can read more about how proxies are instantiated, generated and initialized in +the `documentation of ProxyManager`_. + + +.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager +.. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern +.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md \ No newline at end of file From 931091d8e3aa0b9d4f1393d313388d5ee1a5d47d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 9 May 2013 20:18:51 +0200 Subject: [PATCH 2/2] Applying changes suggested by @WouterJ, adding lazy_services to components map --- .../dependency_injection/lazy_services.rst | 17 ++++++++--------- components/map.rst.inc | 1 + 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst index ca97b5cad24..599d503f7e6 100644 --- a/components/dependency_injection/lazy_services.rst +++ b/components/dependency_injection/lazy_services.rst @@ -14,35 +14,35 @@ In some particular cases where a very heavy service is always requested, but not always used, you may want to mark it as ``lazy`` to delay its instantiation. In order to have services to lazily instantiate, you will first need to install -the `ProxyManager bridge`_:: +the `ProxyManager bridge`_: - php composer.phar require symfony/proxy-manager-bridge:2.3.* +.. code-block:: bash + $ php composer.phar require symfony/proxy-manager-bridge:2.3.* You can mark the service as ``lazy`` by manipulating its definitions: - .. configuration-block:: .. code-block:: yaml services: foo: - class: Example\Foo + class: Acme\Foo lazy: true .. code-block:: xml - + .. code-block:: php - $definition = new Definition('Example\Foo'); + $definition = new Definition('Acme\Foo'); $definition->setLazy(true); $container->setDefinition('foo', $definition); You can then require the service from the container:: - $service = $container->get($serviceId); + $service = $container->get('foo'); At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same signature of the class representing the service. @@ -55,10 +55,9 @@ signature of the class representing the service. The proxy gets initialized and the actual service is instantiated as soon as you interact in any way with this object. -Additional resources +Additional Resources -------------------- - You can read more about how proxies are instantiated, generated and initialized in the `documentation of ProxyManager`_. diff --git a/components/map.rst.inc b/components/map.rst.inc index e29df506e55..d360c65a706 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -39,6 +39,7 @@ * :doc:`/components/dependency_injection/configurators` * :doc:`/components/dependency_injection/parentservices` * :doc:`/components/dependency_injection/advanced` + * :doc:`/components/dependency_injection/lazy_services` * :doc:`/components/dependency_injection/workflow` * **DOM Crawler** 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