From 4d50f34084ae8f4d775389a536d501530e2d2128 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 28 Nov 2013 14:03:53 -0600 Subject: [PATCH 1/2] [#3022] Adding service container expression language details --- .../_service_container_my_mailer.rst.inc | 34 +++++ book/service_container.rst | 131 +++++++++++++----- 2 files changed, 131 insertions(+), 34 deletions(-) create mode 100644 book/includes/_service_container_my_mailer.rst.inc diff --git a/book/includes/_service_container_my_mailer.rst.inc b/book/includes/_service_container_my_mailer.rst.inc new file mode 100644 index 00000000000..601d188a007 --- /dev/null +++ b/book/includes/_service_container_my_mailer.rst.inc @@ -0,0 +1,34 @@ +.. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + services: + my_mailer: + class: Acme\HelloBundle\Mailer + arguments: [sendmail] + + .. code-block:: xml + + + + + + + + sendmail + + + + + .. code-block:: php + + // app/config/config.php + use Symfony\Component\DependencyInjection\Definition; + + $container->setDefinition('my_mailer', new Definition( + 'Acme\HelloBundle\Mailer', + array('sendmail') + )); \ No newline at end of file diff --git a/book/service_container.rst b/book/service_container.rst index 84ef36c0c01..6f609e175c1 100644 --- a/book/service_container.rst +++ b/book/service_container.rst @@ -103,40 +103,7 @@ for you. In order for this to work, you must *teach* the container how to create the ``Mailer`` service. This is done via configuration, which can be specified in YAML, XML or PHP: -.. configuration-block:: - - .. code-block:: yaml - - # app/config/config.yml - services: - my_mailer: - class: Acme\HelloBundle\Mailer - arguments: [sendmail] - - .. code-block:: xml - - - - - - - - sendmail - - - - - .. code-block:: php - - // app/config/config.php - use Symfony\Component\DependencyInjection\Definition; - - $container->setDefinition('my_mailer', new Definition( - 'Acme\HelloBundle\Mailer', - array('sendmail') - )); +.. include includes/_service_container_my_mailer.rst.inc .. note:: @@ -660,6 +627,102 @@ service needs the ``my_mailer`` service in order to function. When you define this dependency in the service container, the container takes care of all the work of instantiating the classes. +Using the Expression Language +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The service container also supports an "expression" that allows you to inject +very specific values into a service. + +For example, suppose you have a third service (not shown here), called ``mailer_configuration``, +which has a ``getMailerMethod`` method on it, which will return a string +like ``sendmail`` based on some configuration. Remember that the first argument +to the ``my_mailer`` service is the simple string ``sendmail``: + +.. include includes/_service_container_my_mailer.rst.inc + +But instead of hardcoding this, how could we get this value from the ``getMailerMethod`` +of the new ``mailer_configuration`` service? One way is to use an expression: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + services: + my_mailer: + class: Acme\HelloBundle\Mailer + arguments: ["@=service('mailer_configuration').getMailerMethod()"] + + .. code-block:: xml + + + + + + + + service('mailer_configuration').getMailerMethod() + + + + + .. code-block:: php + + // app/config/config.php + use Symfony\Component\DependencyInjection\Definition; + + $container->setDefinition('my_mailer', new Definition( + 'Acme\HelloBundle\Mailer', + array(new Expression("service('mailer_configuration').getMailerMethod()")) + )); + +To learn more about the expression language syntax, see :doc:`/components/expression_language/syntax`. + +In this context, you have access to 2 functions: + +* ``service`` - returns a given service (see the example above); +* ``parameter`` - returns a specific parameter value (syntax is just like ``service``) + +You also have access to the :class:`Symfony\\Component\\DependencyInjection\\ContainerBuilder` +via a ``container`` variable. Here's another example: + +.. configuration-block:: + + .. code-block:: yaml + + services: + my_mailer: + class: Acme\HelloBundle\Mailer + arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"] + + .. code-block:: xml + + + + + + + @=container.hasParameter('some_param') ? parameter('some_param') : 'default_value' + + + + + .. code-block:: php + + use Symfony\Component\DependencyInjection\Definition; + + $container->setDefinition('my_mailer', new Definition( + 'Acme\HelloBundle\Mailer', + array(new Expression("@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'")) + )); + +Expressions can be used in ``parameters``, ``arguments``, ``properties``, +as arguments with ``configurator`` and as arguments to ``calls`` (method calls). + Optional Dependencies: Setter Injection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 22c940431fea57d7fafe4809c212638330175789 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 29 Nov 2013 16:42:34 -0500 Subject: [PATCH 2/2] [#3232][#3022] Making many small tweaks thanks to @WouterJ and @xabbuh --- .../_service_container_my_mailer.rst.inc | 4 ++- book/service_container.rst | 27 +++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/book/includes/_service_container_my_mailer.rst.inc b/book/includes/_service_container_my_mailer.rst.inc index 601d188a007..efc2eaeac26 100644 --- a/book/includes/_service_container_my_mailer.rst.inc +++ b/book/includes/_service_container_my_mailer.rst.inc @@ -14,7 +14,9 @@ + xsi:schemaLocation="http://symfony.com/schema/dic/services + http://symfony.com/schema/dic/services/services-1.0.xsd" + > diff --git a/book/service_container.rst b/book/service_container.rst index 6f609e175c1..ff355e44f8a 100644 --- a/book/service_container.rst +++ b/book/service_container.rst @@ -630,17 +630,20 @@ the work of instantiating the classes. Using the Expression Language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. versionadded:: 2.4 + The Expression Language functionality was introduced in Symfony 2.4. + The service container also supports an "expression" that allows you to inject very specific values into a service. For example, suppose you have a third service (not shown here), called ``mailer_configuration``, -which has a ``getMailerMethod`` method on it, which will return a string +which has a ``getMailerMethod()`` method on it, which will return a string like ``sendmail`` based on some configuration. Remember that the first argument to the ``my_mailer`` service is the simple string ``sendmail``: .. include includes/_service_container_my_mailer.rst.inc -But instead of hardcoding this, how could we get this value from the ``getMailerMethod`` +But instead of hardcoding this, how could we get this value from the ``getMailerMethod()`` of the new ``mailer_configuration`` service? One way is to use an expression: .. configuration-block:: @@ -659,7 +662,9 @@ of the new ``mailer_configuration`` service? One way is to use an expression: + xsi:schemaLocation="http://symfony.com/schema/dic/services + http://symfony.com/schema/dic/services/services-1.0.xsd" + > @@ -672,10 +677,11 @@ of the new ``mailer_configuration`` service? One way is to use an expression: // app/config/config.php use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\ExpressionLanguage\Expression; $container->setDefinition('my_mailer', new Definition( 'Acme\HelloBundle\Mailer', - array(new Expression("service('mailer_configuration').getMailerMethod()")) + array(new Expression('service("mailer_configuration").getMailerMethod()')) )); To learn more about the expression language syntax, see :doc:`/components/expression_language/syntax`. @@ -694,15 +700,17 @@ via a ``container`` variable. Here's another example: services: my_mailer: - class: Acme\HelloBundle\Mailer - arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"] + class: Acme\HelloBundle\Mailer + arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"] .. code-block:: xml + xsi:schemaLocation="http://symfony.com/schema/dic/services + http://symfony.com/schema/dic/services/services-1.0.xsd" + > @@ -714,10 +722,13 @@ via a ``container`` variable. Here's another example: .. code-block:: php use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\ExpressionLanguage\Expression; $container->setDefinition('my_mailer', new Definition( 'Acme\HelloBundle\Mailer', - array(new Expression("@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'")) + array(new Expression( + "@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'" + )) )); Expressions can be used in ``parameters``, ``arguments``, ``properties``,