Skip to content

Commit 4147ad4

Browse files
committed
Merge pull request symfony#3232 from symfony/expression-framework-container
[symfony#3022] Adding service container expression language details
2 parents 78baee9 + 22c9404 commit 4147ad4

File tree

2 files changed

+144
-34
lines changed

2 files changed

+144
-34
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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
18+
http://symfony.com/schema/dic/services/services-1.0.xsd"
19+
>
20+
21+
<services>
22+
<service id="my_mailer" class="Acme\HelloBundle\Mailer">
23+
<argument>sendmail</argument>
24+
</service>
25+
</services>
26+
</container>
27+
28+
.. code-block:: php
29+
30+
// app/config/config.php
31+
use Symfony\Component\DependencyInjection\Definition;
32+
33+
$container->setDefinition('my_mailer', new Definition(
34+
'Acme\HelloBundle\Mailer',
35+
array('sendmail')
36+
));

book/service_container.rst

Lines changed: 108 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-
array('sendmail')
139-
));
106+
.. include includes/_service_container_my_mailer.rst.inc
140107
141108
.. note::
142109

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

0 commit comments

Comments
 (0)