How to Inject Values Based on Complex Expressions

How to Inject Values Based on Complex Expressions

The service container also supports an “expression” that allows you to inject very specific values into a service.

For example, suppose you have a service (not shown here), called App\Mail\MailerConfiguration, which has a getMailerMethod() method on it. This returns a string - like sendmail based on some configuration.

Suppose that you want to pass the result of this method as a constructor argument to another service: App\Mailer. One way to do this is with an expression:

  • YAML

    1. # config/services.yaml
    2. services:
    3. # ...
    4. App\Mail\MailerConfiguration: ~
    5. App\Mailer:
    6. # the '@=' prefix is required when using expressions for arguments in YAML files
    7. arguments: ['@=service("App\\Mail\\MailerConfiguration").getMailerMethod()']
    8. # when using double-quoted strings, the backslash needs to be escaped twice (see https://yaml.org/spec/1.2/spec.html#id2787109)
    9. # arguments: ["@=service('App\\\\Mail\\\\MailerConfiguration').getMailerMethod()"]
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <!-- ... -->
    9. <service id="App\Mail\MailerConfiguration"></service>
    10. <service id="App\Mailer">
    11. <argument type="expression">service('App\\Mail\\MailerConfiguration').getMailerMethod()</argument>
    12. </service>
    13. </services>
    14. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Mail\MailerConfiguration;
    4. use App\Mailer;
    5. return function(ContainerConfigurator $configurator) {
    6. // ...
    7. $services->set(MailerConfiguration::class);
    8. $services->set(Mailer::class)
    9. ->args([expr("service('App\\Mail\\MailerConfiguration').getMailerMethod()")]);
    10. };

To learn more about the expression language syntax, see The Expression 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 like service).

You also have access to the Symfony\Component\DependencyInjection\Container via a container variable. Here’s another example:

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\Mailer:
    4. # the '@=' prefix is required when using expressions for arguments in YAML files
    5. arguments: ["@=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <service id="App\Mailer">
    9. <argument type="expression">container.hasParameter('some_param') ? parameter('some_param') : 'default_value'</argument>
    10. </service>
    11. </services>
    12. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Mailer;
    4. return function(ContainerConfigurator $configurator) {
    5. $services = $configurator->services();
    6. $services->set(Mailer::class)
    7. ->args([expr("container.hasParameter('some_param') ? parameter('some_param') : 'default_value'")]);
    8. };

Expressions can be used in arguments, properties, as arguments with configurator and as arguments to calls (method calls).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.