Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8260f6f

Browse filesBrowse files
committed
[#4228] Move synthetic services to its own recipe
1 parent 26c7813 commit 8260f6f
Copy full SHA for 8260f6f

File tree

Expand file treeCollapse file tree

4 files changed

+52
-55
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+52
-55
lines changed

‎components/dependency_injection/advanced.rst

Copy file name to clipboardExpand all lines: components/dependency_injection/advanced.rst
-55Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,61 +73,6 @@ below) to access this service (via the alias).
7373

7474
Services are by default public.
7575

76-
Synthetic Services
77-
------------------
78-
79-
Synthetic services are services that are injected into the container instead
80-
of being created by the container.
81-
82-
For example, if you're using the :doc:`HttpKernel </components/http_kernel/introduction>`
83-
component with the DependencyInjection component, then the ``request``
84-
service is injected in the
85-
:method:`ContainerAwareHttpKernel::handle() <Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel::handle>`
86-
method when entering the request :doc:`scope </cookbook/service_container/scopes>`.
87-
The class does not exist when there is no request, so it can't be included in
88-
the container configuration. Also, the service should be different for every
89-
subrequest in the application.
90-
91-
To create a synthetic service, set ``synthetic`` to ``true``:
92-
93-
.. configuration-block::
94-
95-
.. code-block:: yaml
96-
97-
services:
98-
request:
99-
synthetic: true
100-
101-
.. code-block:: xml
102-
103-
<?xml version="1.0" encoding="UTF-8" ?>
104-
<container xmlns="http://symfony.com/schema/dic/services"
105-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
106-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
107-
108-
<services>
109-
<service id="request" synthetic="true" />
110-
</services>
111-
</container>
112-
113-
.. code-block:: php
114-
115-
use Symfony\Component\DependencyInjection\Definition;
116-
117-
$container
118-
->setDefinition('request', new Definition())
119-
->setSynthetic(true);
120-
121-
As you see, only the ``synthetic`` option is set. All other options are only used
122-
to configure how a service is created by the container. As the service isn't
123-
created by the container, these options are omitted.
124-
125-
Now, you can inject the class by using
126-
:method:`Container::set <Symfony\\Component\\DependencyInjection\\Container::set>`::
127-
128-
// ...
129-
$container->set('request', new MyRequest(...));
130-
13176
Aliasing
13277
--------
13378

‎components/dependency_injection/index.rst

Copy file name to clipboardExpand all lines: components/dependency_injection/index.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
types
99
parameters
1010
definitions
11+
synthetic_services
1112
compilation
1213
tags
1314
factories
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.. index::
2+
single: DependencyInjection; Synthetic Services
3+
4+
How to Inject Instances into the Container
5+
------------------------------------------
6+
7+
When using the container in your application, you sometimes need to inject an
8+
instance instead of configuring the container to create a new instance.
9+
10+
For instance, if you're using the :doc:`HttpKernel </components/http_kernel/introduction>`
11+
component with the DependencyInjection component, then the ``kernel``
12+
service is injected into the container from within the ``Kernel`` class::
13+
14+
// ...
15+
abstract class Kernel implements KernelInterface, TerminableInterface
16+
{
17+
// ...
18+
protected function initializeContainer()
19+
{
20+
// ...
21+
$this->container->set('kernel', $this);
22+
23+
// ...
24+
}
25+
}
26+
27+
The ``kernel`` service is called a synthetic service. This service has to be
28+
configured in the container, so the container knows the service does exists
29+
during compilation (otherwise, services dependening on this ``kernel`` service
30+
will get a "service does not exists" error).
31+
32+
In order to do so, you have to use
33+
:method:`Definition::setSynthetic() <Symfony\\Component\\DependencyInjection\\Definition::setSynthetic>`::
34+
35+
use Symfony\Component\DependencyInjectino\Definition;
36+
37+
// synthetic services don't specify a class
38+
$kernelDefinition = new Definition();
39+
$kernelDefinition->setSynthetic(true);
40+
41+
$container->setDefinition('your_service', $kernelDefinition);
42+
43+
Now, you can inject the instance in the container using
44+
:method:`Container::set() <Symfony\\Component\\DependencyInjection\\Container::set>`::
45+
46+
$yourService = new YourObject();
47+
$container->set('your_service', $yourService);
48+
49+
``$container->get('your_service')`` will now return the same instance as
50+
``$yourService``.

‎components/map.rst.inc

Copy file name to clipboardExpand all lines: components/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* :doc:`/components/dependency_injection/types`
4040
* :doc:`/components/dependency_injection/parameters`
4141
* :doc:`/components/dependency_injection/definitions`
42+
* :doc:`/components/dependency_injection/synthetic_services`
4243
* :doc:`/components/dependency_injection/compilation`
4344
* :doc:`/components/dependency_injection/tags`
4445
* :doc:`/components/dependency_injection/factories`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.