@@ -146,9 +146,20 @@ 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 ();
150
+ $ defaultKeys = array ('public ' , 'tags ' , 'autowire ' );
151
+
152
+ if (!is_array ($ defaults )) {
153
+ throw new InvalidArgumentException (sprintf ('Service defaults must be an array, "%s" given in "%s". ' , gettype ($ defaults ), $ file ));
154
+ }
155
+ foreach ($ defaults as $ key => $ default ) {
156
+ if (!in_array ($ key , $ defaultKeys )) {
157
+ throw new InvalidArgumentException (sprintf ('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s". ' , $ key , $ file , implode ('", " ' , $ defaultKeys )));
158
+ }
159
+ }
149
160
150
161
foreach ($ content ['services ' ] as $ id => $ service ) {
151
- $ this ->parseDefinition ($ id , $ service , $ file );
162
+ $ this ->parseDefinition ($ id , $ service , $ file, $ defaults );
152
163
}
153
164
}
154
165
@@ -158,10 +169,11 @@ private function parseDefinitions($content, $file)
158
169
* @param string $id
159
170
* @param array $service
160
171
* @param string $file
172
+ * @param array $defaults
161
173
*
162
174
* @throws InvalidArgumentException When tags are invalid
163
175
*/
164
- private function parseDefinition ($ id , $ service , $ file )
176
+ private function parseDefinition ($ id , $ service , $ file, array $ defaults )
165
177
{
166
178
if (is_string ($ service ) && 0 === strpos ($ service , '@ ' )) {
167
179
$ this ->container ->setAlias ($ id , substr ($ service , 1 ));
@@ -176,7 +188,7 @@ private function parseDefinition($id, $service, $file)
176
188
static ::checkDefinition ($ id , $ service , $ file );
177
189
178
190
if (isset ($ service ['alias ' ])) {
179
- $ public = ! array_key_exists ('public ' , $ service ) || (bool ) $ service ['public ' ];
191
+ $ public = array_key_exists ('public ' , $ service ) ? (bool ) $ service ['public ' ] : ! empty ( $ defaults [ ' public ' ]) ;
180
192
$ this ->container ->setAlias ($ id , new Alias ($ service ['alias ' ], $ public ));
181
193
182
194
foreach ($ service as $ key => $ value ) {
@@ -190,6 +202,7 @@ private function parseDefinition($id, $service, $file)
190
202
191
203
if (isset ($ service ['parent ' ])) {
192
204
$ definition = new ChildDefinition ($ service ['parent ' ]);
205
+ $ defaults = array ();
193
206
} else {
194
207
$ definition = new Definition ();
195
208
}
@@ -210,8 +223,9 @@ private function parseDefinition($id, $service, $file)
210
223
$ definition ->setLazy ($ service ['lazy ' ]);
211
224
}
212
225
213
- if (isset ($ service ['public ' ])) {
214
- $ definition ->setPublic ($ service ['public ' ]);
226
+ $ public = isset ($ service ['public ' ]) ? $ service ['public ' ] : (isset ($ defaults ['public ' ]) ? $ defaults ['public ' ] : null );
227
+ if (null !== $ public ) {
228
+ $ definition ->setPublic ($ public );
215
229
}
216
230
217
231
if (isset ($ service ['abstract ' ])) {
@@ -260,12 +274,13 @@ private function parseDefinition($id, $service, $file)
260
274
}
261
275
}
262
276
263
- if (isset ($ service ['tags ' ])) {
264
- if (!is_array ($ service ['tags ' ])) {
277
+ $ tags = isset ($ service ['tags ' ]) ? $ service ['tags ' ] : (isset ($ defaults ['tags ' ]) ? $ defaults ['tags ' ] : null );
278
+ if (null !== $ tags ) {
279
+ if (!is_array ($ tags )) {
265
280
throw new InvalidArgumentException (sprintf ('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax. ' , $ id , $ file ));
266
281
}
267
282
268
- foreach ($ service [ ' tags ' ] as $ tag ) {
283
+ foreach ($ tags as $ tag ) {
269
284
if (!is_array ($ tag )) {
270
285
$ tag = array ('name ' => $ tag );
271
286
}
@@ -301,11 +316,12 @@ private function parseDefinition($id, $service, $file)
301
316
$ definition ->setDecoratedService ($ service ['decorates ' ], $ renameId , $ priority );
302
317
}
303
318
304
- if (isset ($ service ['autowire ' ])) {
305
- if (is_array ($ service ['autowire ' ])) {
306
- $ definition ->setAutowiredMethods ($ service ['autowire ' ]);
319
+ $ autowire = isset ($ service ['autowire ' ]) ? $ service ['autowire ' ] : (isset ($ defaults ['autowire ' ]) ? $ defaults ['autowire ' ] : null );
320
+ if (null !== $ autowire ) {
321
+ if (is_array ($ autowire )) {
322
+ $ definition ->setAutowiredMethods ($ autowire );
307
323
} else {
308
- $ definition ->setAutowired ($ service [ ' autowire ' ] );
324
+ $ definition ->setAutowired ($ autowire );
309
325
}
310
326
}
311
327
@@ -4
F438
26,7 +442,7 @@ private function validate($content, $file)
426
442
}
427
443
428
444
foreach ($ content as $ namespace => $ data ) {
429
- if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' ))) {
445
+ if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' , ' service_defaults ' ))) {
430
446
continue ;
431
447
}
432
448
@@ -491,7 +507,7 @@ private function resolveServices($value)
491
507
private function loadFromExtensions ($ content )
492
508
{
493
509
foreach ($ content as $ namespace => $ values ) {
494
- if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' ))) {
510
+ if (in_array ($ namespace , array ('imports ' , 'parameters ' , 'services ' , ' service_defaults ' ))) {
495
511
continue ;
496
512
}
497
513
0 commit comments