Skip to content

Commit 4566d33

Browse files
committed
minor #5284 Split advanced container configuration article (WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Split advanced container configuration article | Q | A | --- | --- | Doc fixes? | yes | New docs? | yes | Applies to | all | Fixed tickets | #4228 (partially, 2 and 3) Commits ------- e3c2603 [#4228] Move synthetic services to its own recipe 26c7813 [#4228] Moved requiring files to definitions
2 parents 5c0f8fb + e3c2603 commit 4566d33

File tree

5 files changed

+64
-96
lines changed

5 files changed

+64
-96
lines changed

components/dependency_injection/advanced.rst

Lines changed: 0 additions & 96 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

@@ -182,44 +127,3 @@ service by asking for the ``bar`` service like this::
182127
foo:
183128
class: Example\Foo
184129
bar: "@foo"
185-
186-
187-
Requiring Files
188-
---------------
189-
190-
There might be use cases when you need to include another file just before
191-
the service itself gets loaded. To do so, you can use the ``file`` directive.
192-
193-
.. configuration-block::
194-
195-
.. code-block:: yaml
196-
197-
services:
198-
foo:
199-
class: Example\Foo\Bar
200-
file: "%kernel.root_dir%/src/path/to/file/foo.php"
201-
202-
.. code-block:: xml
203-
204-
<?xml version="1.0" encoding="UTF-8" ?>
205-
<container xmlns="http://symfony.com/schema/dic/services"
206-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
207-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
208-
209-
<services>
210-
<service id="foo" class="Example\Foo\Bar">
211-
<file>%kernel.root_dir%/src/path/to/file/foo.php</file>
212-
</service>
213-
</services>
214-
</container>
215-
216-
.. code-block:: php
217-
218-
use Symfony\Component\DependencyInjection\Definition;
219-
220-
$definition = new Definition('Example\Foo\Bar');
221-
$definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php');
222-
$container->setDefinition('foo', $definition);
223-
224-
Notice that Symfony will internally call the PHP statement ``require_once``,
225-
which means that your file will be included only once per request.

components/dependency_injection/definitions.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,15 @@ You can also replace any existing method calls with an array of new ones with::
125125
the container is compiled. Once the container is compiled you cannot
126126
manipulate service definitions further. To learn more about compiling
127127
the container see :doc:`/components/dependency_injection/compilation`.
128+
129+
Requiring Files
130+
~~~~~~~~~~~~~~~
131+
132+
There might be use cases when you need to include another file just before
133+
the service itself gets loaded. To do so, you can use the
134+
:method:`Symfony\\Component\\DependencyInjection\\Definition::setFile` method::
135+
136+
$definition->setFile('/src/path/to/file/foo.php');
137+
138+
Notice that Symfony will internally call the PHP statement ``require_once``,
139+
which means that your file will be included only once per request.

components/dependency_injection/index.rst

Lines 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
Lines 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 exist
29+
during compilation (otherwise, services depending 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

Lines 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)