Skip to content

[DependencyInjection] Use lazy-loading ghost object proxies out of the box #16995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions service_container/lazy_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ until you interact with the proxy in some way.
In PHP versions prior to 8.0 lazy services do not support parameters with
default values for built-in PHP classes (e.g. ``PDO``).

Installation
------------
.. versionadded:: 6.2

In order to use the lazy service instantiation, you will need to install the
``symfony/proxy-manager-bridge`` package:

.. code-block:: terminal

$ composer require symfony/proxy-manager-bridge
Starting from Symfony 6.2, you don't have to install any package (e.g.
``symfony/proxy-manager-bridge``) in order to use the lazy service instantiation.

Configuration
-------------
Expand Down Expand Up @@ -81,32 +76,16 @@ You can mark the service as ``lazy`` by manipulating its definition:
$services->set(AppExtension::class)->lazy();
};

Once you inject the service into another service, a lazy ghost object with the
same signature of the class representing the service should be injected. A lazy
`ghost object`_ is an object that is created empty and that is able to initialize
itself when being accessed for the first time). The same happens when calling
``Container::get()`` directly.

Once you inject the service into another service, a virtual `proxy`_ with the
same signature of the class representing the service should be injected. The
same happens when calling ``Container::get()`` directly.

The actual class will be instantiated as soon as you try to interact with the
service (e.g. call one of its methods).

To check if your proxy works you can check the interface of the received object::
To check if your lazy service works you can check the interface of the received object::

dump(class_implements($service));
// the output should include "ProxyManager\Proxy\LazyLoadingInterface"

.. note::

If you don't install the `ProxyManager bridge`_ , the container will skip
over the ``lazy`` flag and directly instantiate the service as it would
normally do.

Additional Resources
--------------------

You can read more about how proxies are instantiated, generated and initialized
in the `documentation of ProxyManager`_.
// the output should include "Symfony\Component\VarExporter\LazyGhostObjectInterface"

.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager
.. _`proxy`: https://en.wikipedia.org/wiki/Proxy_pattern
.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md
.. _`ghost object`: https://en.wikipedia.org/wiki/Lazy_loading#Ghost
.. _`final`: https://www.php.net/manual/en/language.oop5.final.php