8000 [#3022] Adding service container expression language details · cordoval/symfony-docs@4d50f34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d50f34

Browse files
committed
[symfony#3022] Adding service container expression language details
1 parent 78baee9 commit 4d50f34

File tree

2 files changed

+131
-34
lines changed

2 files changed

+131
-34
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. configuration-block::
2+
3+
.. code-block:: yaml
4+
5+
# app/config/config.yml
6+
services:
7+
my_mailer:
8+
class: Acme\HelloBundle\Mailer
9+
arguments: [sendmail]
10+
11+
.. code-block:: xml
12+
13+
<!-- app/config/config.xml -->
14+
<?xml version="1.0" encoding="UTF-8" ?>
15+
<container xmlns="http://symfony.com/schema/dic/services"
16+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
18+
19+
<services>
20+
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
21+
<argument>sendmail</argument>
22+
</service>
23+
</services>
24+
</container>
25+
26+
.. code-block:: php
27+
28+
// app/config/config.php
29+
use Symfony\Component\DependencyInjection\Definition;
30+
31+
$container->setDefinition('my_mailer', new Definition(
32+
'Acme\HelloBundle\Mailer',
33+
array('sendmail')
34+
));

book/service_container.rst

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,7 @@ for you. In order for this to work, you must *teach* the container how to
103103
create the ``Mailer`` service. This is done via configuration, which can
104104
be specified in YAML, XML or PHP:
105105

106-
.. configuration-block::
107-
108-
.. code-block:: yaml
109-
110-
# app/config/config.yml
111-
services:
112-
my_mailer:
113-
class: Acme\HelloBundle\Mailer
114-
arguments: [sendmail]
115-
116-
.. code-block:: xml
117-
118-
<!-- app/config/config.xml -->
119-
<?xml version="1.0" encoding="UTF-8" ?>
120-
<container xmlns="http://symfony.com/schema/dic/services"
121-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
122-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
123-
124-
<services>
125-
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
126-
<argument>sendmail</argument>
127-
</service>
128-
</services>
129-
</container>
130-
131-
.. code-block:: php
132-
133-
// app/config/config.php
134-
use Symfony\Component\DependencyInjection\Definition;
135-
136-
$container->setDefinition('my_mailer', new Definition(
137-
'Acme\HelloBundle\Mailer',
138-
8000 array('sendmail')
139-
));
106+
.. include includes/_service_container_my_mailer.rst.inc
140107
141108
.. note::
142109

@@ -660,6 +627,102 @@ service needs the ``my_mailer`` service in order to function. When you define
660627
this dependency in the service container, the container takes care of all
661628
the work of instantiating the classes.
662629

630+
Using the Expression Language
631+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
632+
633+
The service container also supports an "expression" that allows you to inject
634+
very specific values into a service.
635+
636+
For example, suppose you have a third service (not shown here), called ``mailer_configuration``,
637+
which has a ``getMailerMethod`` method on it, which will return a string
638+
like ``sendmail`` based on some configuration. Remember that the first argument
639+
to the ``my_mailer`` service is the simple string ``sendmail``:
640+
641+
.. include includes/_service_container_my_mailer.rst.inc
642+
643+
But instead of hardcoding this, how could we get this value from the ``getMailerMethod``
644+
of the new ``mailer_configuration`` service? One way is to use an expression:
645+
646+
.. configuration-block::
647+
648+
.. code-block:: yaml
649+
650+
# app/config/config.yml
651+
services:
652+
my_mailer:
653+
class: Acme\HelloBundle\Mailer
654+
arguments: ["@=service('mailer_configuration').getMailerMethod()"]
655+
656+
.. code-block:: xml
657+
658+
<!-- app/config/config.xml -->
659+
<?xml version="1.0" encoding="UTF-8" ?>
660+
<container xmlns="http://symfony.com/schema/dic/services"
661+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
662+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
663+
664+
<services>
665+
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
666+
<argument type="expression">service('mailer_configuration').getMailerMethod()</argument>
667+
</service>
668+
</services>
669+
</container>
670+
671+
.. code-block:: php
672+
673+
// app/config/config.php
674+
use Symfony\Component\DependencyInjection\Definition;
675+
676+
$container->setDefinition('my_mailer', new Definition(
677+
'Acme\HelloBundle\Mailer',
678+
array(new Expression("service('mailer_configuration').getMailerMethod()"))
679+
));
680+
681+
To learn more about the expression language syntax, see :doc:`/components/expression_language/syntax`.
682+
683+
In this context, you have access to 2 functions:
684+
685+
* ``service`` - returns a given service (see the example above);
686+
* ``parameter`` - returns a specific parameter value (syntax is just like ``service``)
687+
688+
You also have access to the :class:`Symfony\\Component\\DependencyInjection\\ContainerBuilder`
689+
via a ``container`` variable. Here's another example:
690+
691+
.. configuration-block::
692+
693+
.. code-block:: yaml
694+
695+
services:
696+
my_mailer:
697+
class: Acme\HelloBundle\Mailer
698+
arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
699+
700+
.. code-block:: xml
701+
702+
<?xml version="1.0" encoding="UTF-8" ?>
703+
<container xmlns="http://symfony.com/schema/dic/services"
704+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
705+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
706+
707+
<services>
708+
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
709+
<argument type="expression">@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'</argument>
710+
</service>
711+
</services>
712+
</container>
713+
714+
.. code-block:: php
715+
716+
use Symfony\Component\DependencyInjection\Definition;
717+
718+
$container->setDefinition('my_mailer', new Definition(
719+
'Acme\HelloBundle\Mailer',
720+
array(new Expression("@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"))
721+
));
722+
723+
Expressions can be used in ``parameters``, ``arguments``, ``properties``,
724+
as arguments with ``configurator`` and as arguments to ``calls`` (method calls).
725+
663726
Optional Dependencies: Setter Injection
664727
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665728

0 commit comments

Comments
 (0)
0