Skip to content

Commit 710d28d

Browse files
committed
Merge branch 'moving_di_stuff_to_components' of git://github.com/richardmiller/symfony-docs into richardmiller-moving_di_stuff_to_components
Conflicts: book/service_container.rst
2 parents 9ed2ebb + 1904ac8 commit 710d28d

File tree

3 files changed

+128
-133
lines changed

3 files changed

+128
-133
lines changed

book/service_container.rst

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -876,142 +876,10 @@ the framework.
876876
the ``swiftmailer`` key invokes the service extension from the
877877
``SwiftmailerBundle``, which registers the ``mailer`` service.
878878

879-
.. index::
880-
single: Service Container; Advanced configuration
881-
882-
Advanced Container Configuration
883-
--------------------------------
884-
885-
As we've seen, defining services inside the container is easy, generally
886-
involving a ``service`` configuration key and a few parameters. However,
887-
the container has several other tools available that help to *tag* services
888-
for special functionality, create more complex services, and perform operations
889-
after the container is built.
890-
891-
Marking Services as public / private
892-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
893-
894-
When defining services, you'll usually want to be able to access these definitions
895-
within your application code. These services are called ``public``. For example,
896-
the ``doctrine`` service registered with the container when using the DoctrineBundle
897-
is a public service as you can access it via::
898-
899-
$doctrine = $container->get('doctrine');
900-
901-
However, there are use-cases when you don't want a service to be public. This
902-
is common when a service is only defined because it could be used as an
903-
argument for another service.
904-
905-
.. note::
906-
907-
If you use a private service as an argument to more than one other service,
908-
this will result in two different instances being used as the instantiation
909-
of the private service is done inline (e.g. ``new PrivateFooBar()``).
910-
911-
Simply said: A service will be private when you do not want to access it
912-
directly from your code.
913-
914-
Here is an example:
915-
916-
.. configuration-block::
917-
918-
.. code-block:: yaml
919-
920-
services:
921-
foo:
922-
class: Acme\HelloBundle\Foo
923-
public: false
924-
925-
.. code-block:: xml
926-
927-
<service id="foo" class="Acme\HelloBundle\Foo" public="false" />
928-
929-
.. code-block:: php
930-
931-
$definition = new Definition('Acme\HelloBundle\Foo');
932-
$definition->setPublic(false);
933-
$container->setDefinition('foo', $definition);
934-
935-
Now that the service is private, you *cannot* call::
936-
937-
$container->get('foo');
938-
939-
However, if a service has been marked as private, you can still alias it (see
940-
below) to access this service (via the alias).
941-
942-
.. note::
943-
944-
Services are by default public.
945-
946-
Aliasing
947-
~~~~~~~~
948-
949-
When using core or third party bundles within your application, you may want
950-
to use shortcuts to access some services. You can do so by aliasing them and,
951-
furthermore, you can even alias non-public services.
952-
953-
.. configuration-block::
954-
955-
.. code-block:: yaml
956-
957-
services:
958-
foo:
959-
class: Acme\HelloBundle\Foo
960-
bar:
961-
alias: foo
962-
963-
.. code-block:: xml
964-
965-
<service id="foo" class="Acme\HelloBundle\Foo"/>
966-
967-
<service id="bar" alias="foo" />
968-
969-
.. code-block:: php
970-
971-
$definition = new Definition('Acme\HelloBundle\Foo');
972-
$container->setDefinition('foo', $definition);
973-
974-
$containerBuilder->setAlias('bar', 'foo');
975-
976-
This means that when using the container directly, you can access the ``foo``
977-
service by asking for the ``bar`` service like this::
978-
979-
$container->get('bar'); // Would return the foo service
980-
981-
Requiring files
982-
~~~~~~~~~~~~~~~
983-
984-
There might be use cases when you need to include another file just before
985-
the service itself gets loaded. To do so, you can use the ``file`` directive.
986-
987-
.. configuration-block::
988-
989-
.. code-block:: yaml
990-
991-
services:
992-
foo:
993-
class: Acme\HelloBundle\Foo\Bar
994-
file: %kernel.root_dir%/src/path/to/file/foo.php
995-
996-
.. code-block:: xml
997-
998-
<service id="foo" class="Acme\HelloBundle\Foo\Bar">
999-
<file>%kernel.root_dir%/src/path/to/file/foo.php</file>
1000-
</service>
1001-
1002-
.. code-block:: php
1003-
1004-
$definition = new Definition('Acme\HelloBundle\Foo\Bar');
1005-
$definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php');
1006-
$container->setDefinition('foo', $definition);
1007-
1008-
Notice that symfony will internally call the PHP function require_once
1009-
which means that your file will be included only once per request.
1010-
1011879
.. _book-service-container-tags:
1012880

1013881
Tags (``tags``)
1014-
~~~~~~~~~~~~~~~
882+
---------------
1015883

1016884
In the same way that a blog post on the Web might be tagged with things such
1017885
as "Symfony" or "PHP", services configured in your container can also be
@@ -1079,5 +947,6 @@ Learn more
1079947
* :doc:`/cookbook/controller/service`
1080948
* :doc:`/cookbook/service_container/scopes`
1081949
* :doc:`/cookbook/service_container/compiler_passes`
950+
* :doc:`/components/dependency_injection/advanced`
1082951

1083952
.. _`service-oriented architecture`: http://wikipedia.org/wiki/Service-oriented_architecture
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
.. index::
2+
single: Dependency Injection; Advanced configuration
3+
4+
Advanced Container Configuration
5+
================================
6+
7+
Marking Services as public / private
8+
------------------------------------
9+
10+
When defining services, you'll usually want to be able to access these definitions
11+
within your application code. These services are called ``public``. For example,
12+
the ``doctrine`` service registered with the container when using the DoctrineBundle
13+
is a public service as you can access it via::
14+
15+
$doctrine = $container->get('doctrine');
16+
17+
However, there are use-cases when you don't want a service to be public. This
18+
is common when a service is only defined because it could be used as an
19+
argument for another service.
20+
21+
.. note::
22+
23+
If you use a private service as an argument to more than one other service,
24+
this will result in two different instances being used as the instantiation
25+
of the private service is done inline (e.g. ``new PrivateFooBar()``).
26+
27+
Simply said: A service will be private when you do not want to access it
28+
directly from your code.
29+
30+
Here is an example:
31+
32+
.. configuration-block::
33+
34+
.. code-block:: yaml
35+
36+
services:
37+
foo:
38+
class: Example\Foo
39+
public: false
40+
41+
.. code-block:: xml
42+
43+
<service id="foo" class="Example\Foo" public="false" />
44+
45+
.. code-block:: php
46+
47+
$definition = new Definition('Example\Foo');
48+
$definition->setPublic(false);
49+
$container->setDefinition('foo', $definition);
50+
51+
Now that the service is private, you *cannot* call::
52+
53+
$container->get('foo');
54+
55+
However, if a service has been marked as private, you can still alias it (see
56+
below) to access this service (via the alias).
57+
58+
.. note::
59+
60+
Services are by default public.
61+
62+
Aliasing
63+
--------
64+
65+
You may sometimes want to use shortcuts to access some services. You can
66+
do so by aliasing them and, furthermore, you can even alias non-public
67+
services.
68+
69+
.. configuration-block::
70+
71+
.. code-block:: yaml
72+
73+
services:
74+
foo:
75+
class: Example\Foo
76+
bar:
77+
alias: foo
78+
79+
.. code-block:: xml
80+
81+
<service id="foo" class="Example\Foo"/>
82+
83+
<service id="bar" alias="foo" />
84+
85+
.. code-block:: php
86+
87+
$definition = new Definition('Example\Foo');
88+
$container->setDefinition('foo', $definition);
89+
90+
$containerBuilder->setAlias('bar', 'foo');
91+
92+
This means that when using the container directly, you can access the ``foo``
93+
service by asking for the ``bar`` service like this::
94+
95+
$container->get('bar'); // Would return the foo service
96+
97+
Requiring files
98+
---------------
99+
100+
There might be use cases when you need to include another file just before
101+
the service itself gets loaded. To do so, you can use the ``file`` directive.
102+
103+
.. configuration-block::
104+
105+
.. code-block:: yaml
106+
107+
services:
108+
foo:
109+
class: Example\Foo\Bar
110+
file: %kernel.root_dir%/src/path/to/file/foo.php
111+
112+
.. code-block:: xml
113+
114+
<service id="foo" class="Example\Foo\Bar">
115+
<file>%kernel.root_dir%/src/path/to/file/foo.php</file>
116+
</service>
117+
118+
.. code-block:: php
119+
120+
$definition = new Definition('Example\Foo\Bar');
121+
$definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php');
122+
$container->setDefinition('foo', $definition);
123+
124+
Notice that symfony will internally call the PHP function require_once
125+
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
@@ -10,4 +10,5 @@
1010
tags
1111
factories
1212
parentservices
13+
advanced
1314

0 commit comments

Comments
 (0)