@@ -146,9 +146,10 @@ private function parseDefinitions($content, $file)
146
146
if (!is_array ($ content ['services ' ])) {
147
147
throw new InvalidArgumentException (sprintf ('The "services" key should contain an array in %s. Check your YAML syntax. ' , $ file ));
148
148
}
149
+ $ defaults = isset ($ content ['service_defaults ' ]) ? $ content ['service_defaults ' ] : array ();
149
150
150
151
foreach ($ content ['services ' ] as $ id => $ service ) {
151
- $ this ->parseDefinition ($ id , $ service , $ file );
152
+ $ this ->parseDefinition ($ id , $ service , $ file, $ defaults );
152
153
}
153
154
}
154
155
@@ -158,10 +159,11 @@ private function parseDefinitions($content, $file)
158
159
* @param string $id
159
160
* @param array $service
160
161
* @param string $file
162
+ * @param array $defaults
161
163
*
162
164
* @throws InvalidArgumentException When tags are invalid
163
165
*/
164
- private function parseDefinition ($ id , $ service , $ file )
166
+ private function parseDefinition ($ id , $ service , $ file, array $ defaults )
165
167
{
166
168
if (is_string ($ service ) && 0 === strpos ($ service , '@ ' )) {
167
169
$ this ->container ->setAlias ($ id , substr ($ service , 1 ));
@@ -176,7 +178,7 @@ private function parseDefinition($id, $service, $file)
176
178
static ::checkDefinition ($ id , $ service , $ file );
177
179
178
180
if (isset ($ service ['alias ' ])) {
179
- $ public = ! array_key_exists ('public ' , $ service ) || (bool ) $ service ['public ' ];
181
+ $ public = array_key_exists ('public ' , $ service ) ? (bool ) $ service ['public ' ] : ! empty ( $ defaults [ ' public ' ]) ;
180
182
$ this ->container ->setAlias ($ id , new Alias ($ service ['alias ' ], $ public ));
181
183
182
184
foreach ($ service as $ key => $ value ) {
@@ -190,6 +192,7 @@ private function parseDefinition($id, $service, $file)
190
192
191
193
if (isset ($ service ['parent ' ])) {
192
194
$ definition = new ChildDefinition ($ service ['parent ' ]);
195
+ $ defaults = array ();
193
196
} else {
194
197
$ definition = new Definition ();
195
198
}
@@ -210,8 +213,9 @@ private function parseDefinition($id, $service, $file)
210
213
$ definition ->setLazy ($ service ['lazy ' ]);
211
214
}
212
215
213
- if (isset ($ service ['public ' ])) {
214
- $ definition ->setPublic ($ service ['public ' ]);
216
+ $ public = isset ($ service ['public ' ]) ? $ service ['public ' ] : (isset ($ defaults ['public ' ]) ? $ defaults ['public ' ] : null );
217
+ if (null !== $ public ) {
218
+ $ definition ->setPublic ($ public );
215
219
}
216
220
217
221
if (isset ($ service ['abstract ' ])) {
@@ -260,12 +264,13 @@ private function parseDefinition($id, $service, $file)
260
264
}
261
265
}
262
266
263
- if (isset ($ service ['tags ' ])) {
264
- if (!is_array ($ service ['tags ' ])) {
267
+ $ tags = isset ($ service ['tags ' ]) ? $ service ['tags ' ] : (isset ($ defaults ['tags ' ]) ? $ defaults ['tags ' ] : null );
268
+ if (null !== $ tags ) {
269
+ if (!is_array ($ tags )) {
265
270
throw new InvalidArgumentException (sprintf ('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax. ' , $ id , $ file ));
266
271
}
267
272
268
- foreach ($ service [ ' tags ' ] as $ tag ) {
273
+ foreach ($ tags as $ tag ) {
269
274
if (!is_array ($ tag )) {
270
275
$ tag = array ('name ' => $ tag );
271
276
}
@@ -301,11 +306,12 @@ private function parseDefinition($id, $service, $file)
301
306
$ definition ->setDecoratedService ($ service ['decorates ' ], $ renameId , $ priority );
302
307
}
303
308
304
- if (isset ($ service ['autowire ' ])) {
305
- if (is_array ($ service ['autowire ' ])) {
306
- $ definition ->setAutowiredMethods ($ service ['autowire ' ]);
309
+ $ autowire = isset ($ service ['autowire ' ]) ? $ service ['autowire ' ] : (isset ($ defaults ['autowire ' ]) ? $ defaults ['autowire ' ] : null );
310
+ if (null !== $ autowire ) {
311
+ if (is_array ($ autowire )) {
312
+ $ definition ->setAutowiredMethods ($ autowire );
307
313
} else {
308
- $ definition ->setAutowired ($ service [ ' autowire ' ] );
314
+ $ definition ->setAutowired ($ autowire );
309
315
}
310
316
}
311
317
@@ -426,7 +432,7 @@ private function validate($content, $file)
426
432
}
427
433
428
434
foreach ($ content as $ namespace => $ data ) {
429
- if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' ))) {
435
+ if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' , ' service-defaults ' ))) {
430
436
continue ;
431
437
}
432
438
0 commit comments