How to work with Service Definition Objects

How to work with Service Definition Objects

Service definitions are the instructions describing how the container should build a service. They are not the actual services used by your applications. The container will create the actual class instances based on the configuration in the definition.

Normally, you would use YAML, XML or PHP to describe the service definitions. But if you’re doing advanced things with the service container, like working with a Compiler Pass or creating a Dependency Injection Extension, you may need to work directly with the Definition objects that define how a service will be instantiated.

Getting and Setting Service Definitions

There are some helpful methods for working with the service definitions:

  1. use Symfony\Component\DependencyInjection\Definition;
  2. // finds out if there is an "app.mailer" definition
  3. $container->hasDefinition('app.mailer');
  4. // finds out if there is an "app.mailer" definition or alias
  5. $container->has('app.mailer');
  6. // gets the "app.user_config_manager" definition
  7. $definition = $container->getDefinition('app.user_config_manager');
  8. // gets the definition with the "app.user_config_manager" ID or alias
  9. $definition = $container->findDefinition('app.user_config_manager');
  10. // adds a new "app.number_generator" definition
  11. $definition = new Definition(\App\NumberGenerator::class);
  12. $container->setDefinition('app.number_generator', $definition);
  13. // shortcut for the previous method
  14. $container->register('app.number_generator', \App\NumberGenerator::class);

Working with a Definition

Creating a New Definition

In addition to manipulating and retrieving existing definitions, you can also define new service definitions with the Symfony\Component\DependencyInjection\Definition class.

Class

The first optional argument of the Definition class is the fully qualified class name of the object returned when the service is fetched from the container:

  1. use App\Config\CustomConfigManager;
  2. use App\Config\UserConfigManager;
  3. use Symfony\Component\DependencyInjection\Definition;
  4. $definition = new Definition(UserConfigManager::class);
  5. // override the class
  6. $definition->setClass(CustomConfigManager::class);
  7. // get the class configured for this definition
  8. $class = $definition->getClass();

Constructor Arguments

The second optional argument of the Definition class is an array with the arguments passed to the constructor of the object returned when the service is fetched from the container:

  1. use App\Config\DoctrineConfigManager;
  2. use Symfony\Component\DependencyInjection\Definition;
  3. use Symfony\Component\DependencyInjection\Reference;
  4. $definition = new Definition(DoctrineConfigManager::class, [
  5. new Reference('doctrine'), // a reference to another service
  6. '%app.config_table_name%', // will be resolved to the value of a container parameter
  7. ]);
  8. // gets all arguments configured for this definition
  9. $constructorArguments = $definition->getArguments();
  10. // gets a specific argument
  11. $firstArgument = $definition->getArgument(0);
  12. // adds a new named argument
  13. // '$argumentName' = the name of the argument in the constructor, including the '$' symbol
  14. $definition = $definition->setArgument('$argumentName', $argumentValue);
  15. // adds a new argument
  16. $definition->addArgument($argumentValue);
  17. // replaces argument on a specific index (0 = first argument)
  18. $definition->replaceArgument($index, $argument);
  19. // replaces all previously configured arguments with the passed array
  20. $definition->setArguments($arguments);

Caution

Don’t use get() to get a service that you want to inject as constructor argument, the service is not yet available. Instead, use a Reference instance as shown above.

Method Calls

If the service you are working with uses setter injection then you can manipulate any method calls in the definitions as well:

  1. // gets all configured method calls
  2. $methodCalls = $definition->getMethodCalls();
  3. // configures a new method call
  4. $definition->addMethodCall('setLogger', [new Reference('logger')]);
  5. // configures an immutable-setter
  6. $definition->addMethodCall('withLogger', [new Reference('logger')], true);
  7. // replaces all previously configured method calls with the passed array
  8. $definition->setMethodCalls($methodCalls);

Tip

There are more examples of specific ways of working with definitions in the PHP code blocks of the Service Container articles such as Using a Factory to Create Services and How to Manage Common Dependencies with Parent Services.

Note

The methods here that change service definitions can only be used before the container is compiled. Once the container is compiled you cannot manipulate service definitions further. To learn more about compiling the container, see Compiling the Container.

Requiring Files

There might be use cases when you need to include another file just before the service itself gets loaded. To do so, you can use the [setFile()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/DependencyInjection/Definition.php "Symfony\Component\DependencyInjection\Definition::setFile()") method:

  1. $definition->setFile('/src/path/to/file/foo.php');

Notice that Symfony will internally call the PHP statement require_once, which means that your file will be included only once per request.

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