4
4
Working with Container Service Definitions
5
5
==========================================
6
6
7
+ Service definitions are the instructions describing how the container should
8
+ build a service. They are not the actual services used by your applications
9
+
7
10
Getting and Setting Service Definitions
8
11
---------------------------------------
9
12
@@ -32,34 +35,68 @@ with these methods and make changes to it these will be reflected in the
32
35
container. If, however, you are creating a new definition then you can add
33
36
it to the container using::
34
37
35
- $container->setDefinition($id, $definition);
38
+ use Symfony\Component\DependencyInjection\Definition;
39
+
40
+ $definition = new Definition('Acme\Service\MyService');
41
+ $container->setDefinition('acme.my_service', $definition);
42
+
43
+ .. tip ::
44
+
45
+ Registering service definitions is so common that the container provides a
46
+ shortcut method called ``register() ``::
47
+
48
+ $container->register('acme.my_service', 'Acme\Service\MyService');
36
49
37
50
Working with a Definition
38
51
-------------------------
39
52
40
53
Creating a New Definition
41
54
~~~~~~~~~~~~~~~~~~~~~~~~~
42
55
43
- If you need to create a new definition rather than manipulate one retrieved
44
- from the container then the definition class is :class: `Symfony\\ Component\\ DependencyInjection\\ Definition `.
56
+ In addition to manipulating and retrieving existing definitions, you can also
57
+ define new service definitions with the :class: `Symfony\\ Component\\ DependencyInjection\\ Definition `
58
+ class.
45
59
46
60
Class
47
61
~~~~~
48
62
49
- First up is the class of a definition, this is the class of the object returned
50
- when the service is requested from the container.
63
+ The first optional argument of the `` Definition `` class is the fully qualified
64
+ class name of the object returned when the service is fetched from the container::
51
65
52
- To find out what class is set for a definition::
66
+ use Symfony\Component\DependencyInjection\Definition;
67
+
68
+ $definition = new Definition('Acme\Service\MyService');
69
+
70
+ If the class is unknown when instantiating the ``Definition `` class, use the
71
+ ``setClass() `` method to set it later::
53
72
54
- $definition->getClass( );
73
+ $definition->setClass('Acme\Service\MyService' );
55
74
56
- and to set a different class ::
75
+ To find out what class is set for a definition ::
57
76
58
- $definition->setClass($class); // Fully qualified class name as string
77
+ $class = $definition->getClass();
78
+ // $class = 'Acme\Service\MyService'
59
79
60
80
Constructor Arguments
61
81
~~~~~~~~~~~~~~~~~~~~~
62
82
83
+ The second optional argument of the ``Definition `` class is an array with the
84
+ arguments passed to the constructor of the object returned when the service is
85
+ fetched from the container::
86
+
87
+ use Symfony\Component\DependencyInjection\Definition;
88
+
89
+ $definition = new Definition(
90
+ 'Acme\Service\MyService',
91
+ array('argument1' => 'value1', 'argument2' => 'value2')
92
+ );
93
+
94
+ If the arguments are unknown when instantiating the ``Definition `` class or if
95
+ you want to add new arguments, use the ``addArgument() `` method, which adds them
96
+ at the end of the arguments array::
97
+
98
+ $definition->addArgument($argument);
99
+
63
100
To get an array of the constructor arguments for a definition you can use::
64
101
65
102
$definition->getArguments();
@@ -69,12 +106,16 @@ or to get a single argument by its position::
69
106
$definition->getArgument($index);
70
107
// e.g. $definition->getArgument(0) for the first argument
71
108
72
- You can add a new argument to the end of the arguments array using::
109
+ The argument can be a string, an array or a service parameter by using the
110
+ ``%parameter_name% `` syntax::
73
111
74
- $definition->addArgument($argument );
112
+ $definition->addArgument('%kernel_debug%' );
75
113
76
- The argument can be a string, an array, a service parameter by using ``%parameter_name% ``
77
- or a service id by using::
114
+ If the argument is another service, don't use the ``get() `` method to fetch it,
115
+ because it won't be available when defining services. Instead, use the
116
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Reference ` class to get a
117
+ reference to the service which will be available once the service container is
118
+ fully built::
78
119
79
120
use Symfony\Component\DependencyInjection\Reference;
80
121
@@ -139,4 +180,3 @@ the service itself gets loaded. To do so, you can use the
139
180
140
181
Notice that Symfony will internally call the PHP statement ``require_once ``,
141
182
which means that your file will be included only once per request.
142
-
0 commit comments