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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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

useApp\Config\UserConfigManager;useApp\Config\CustomConfigManager;useSymfony\Component\DependencyInjection\Definition;$definition=newDefinition(UserConfigManager::class);// override the class$definition->setClass(CustomConfigManager::class);// get the class configured for this definition$class=$definition->getClass();

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

useApp\Config\DoctrineConfigManager;useSymfony\Component\DependencyInjection\Definition;useSymfony\Component\DependencyInjection\Reference;$definition=newDefinition(DoctrineConfigManager::class,[newReference('doctrine'),// a reference to another service'%app.config_table_name%',// will be resolved to the value of a container parameter]);// gets all arguments configured for this definition$constructorArguments=$definition->getArguments();// gets a specific argument$firstArgument=$definition->getArgument(0);// adds a new argument$definition->addArgument($argument);// replaces argument on a specific index (0 = first argument)$definition->replaceArgument($index,$argument);// replace all previously configured arguments with the passed array$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.

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.