@@ -146,9 +146,25 @@ 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
+ if (isset ($ content ['services ' ]['_defaults ' ])) {
150
+ $ defaults = $ content ['services ' ]['_defaults ' ];
151
+ $ defaultKeys = array ('public ' , 'tags ' , 'autowire ' );
152
+ unset($ content ['services ' ]['_defaults ' ]);
153
+
154
+ if (!is_array ($ defaults )) {
155
+ throw new InvalidArgumentException (sprintf ('Service defaults must be an array, "%s" given in "%s". ' , gettype ($ defaults ), $ file ));
156
+ }
157
+ foreach ($ defaults as $ key => $ default ) {
158
+ if (!in_array ($ key , $ defaultKeys )) {
159
+ 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 )));
160
+ }
161
+ }
162
+ } else {
163
+ $ defaults = array ();
164
+ }
149
165
150
166
foreach ($ content ['services ' ] as $ id => $ service ) {
151
- $ this ->parseDefinition ($ id , $ service , $ file );
167
+ $ this ->parseDefinition ($ id , $ service , $ file, $ defaults );
152
168
}
153
169
}
154
170
@@ -158,10 +174,11 @@ private function parseDefinitions($content, $file)
158
174
* @param string $id
159
175
* @param array $service
160
176
* @param string $file
177
+ * @param array $defaults
161
178
*
162
179
* @throws InvalidArgumentException When tags are invalid
163
180
*/
164
- private function parseDefinition ($ id , $ service , $ file )
181
+ private function parseDefinition ($ id , $ service , $ file, array $ defaults )
165
182
{
166
183
if (is_string ($ service ) && 0 === strpos ($ service , '@ ' )) {
167
184
$ this ->container ->setAlias ($ id , substr ($ service , 1 ));
@@ -176,7 +193,7 @@ private function parseDefinition($id, $service, $file)
176
193
static ::checkDefinition ($ id , $ service , $ file );
177
194
178
195
if (isset ($ service ['alias ' ])) {
179
- $ public = ! array_key_exists ('public ' , $ service ) || (bool ) $ service ['public ' ];
196
+ $ public = array_key_exists ('public ' , $ service ) ? (bool ) $ service ['public ' ] : ! empty ( $ defaults [ ' public ' ]) ;
180
197
$ this ->container ->setAlias ($ id , new Alias ($ service ['alias ' ], $ public ));
181
198
182
199
foreach ($ service as $ key => $ value ) {
@@ -190,6 +207,7 @@ private function parseDefinition($id, $service, $file)
190
207
191
208
if (isset ($ service ['parent ' ])) {
192
209
$ definition = new ChildDefinition ($ service ['parent ' ]);
210
+ $ defaults = array ();
193
211
} else {
194
212
$ definition = new Definition ();
195
213
}
@@ -210,8 +228,9 @@ private function parseDefinition($id, $service, $file)
210
228
$ definition ->setLazy ($ service ['lazy ' ]);
211
229
}
212
230
213
- if (isset ($ service ['public ' ])) {
214
- $ definition ->setPublic ($ service ['public ' ]);
231
+ $ public = isset ($ service ['public ' ]) ? $ service ['public ' ] : (isset ($ defaults ['public ' ]) ? $ defaults ['public ' ] : null );
232
+ if (null !== $ public ) {
233
+ $ definition ->setPublic ($ public );
215
234
}
216
235
217
236
if (isset ($ service ['abstract ' ])) {
@@ -260,12 +279,13 @@ private function parseDefinition($id, $service, $file)
260
279
}
261
280
}
262
281
263
- if (isset ($ service ['tags ' ])) {
264
- if (!is_array ($ service ['tags ' ])) {
282
+ $ tags = isset ($ service ['tags ' ]) ? $ service ['tags ' ] : (isset ($ defaults ['tags ' ]) ? $ defaults ['tags ' ] : null );
283
+ if (null !== $ tags ) {
284
+ if (!is_array ($ tags )) {
265
285
throw new InvalidArgumentException (sprintf ('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax. ' , $ id , $ file ));
266
286
}
267
287
268
- foreach ($ service [ ' tags ' ] as $ tag ) {
288
+ foreach ($ tags as $ tag ) {
269
289
if (!is_array ($ tag )) {
270
290
$ tag = array ('name ' => $ tag );
271
291
}
@@ -301,11 +321,12 @@ private function parseDefinition($id, $service, $file)
301
321
$ definition ->setDecoratedService ($ service ['decorates ' ], $ renameId , $ priority );
302
322
}
303
323
304
- if (isset ($ service ['autowire ' ])) {
305
- if (is_array ($ service ['autowire ' ])) {
306
- $ definition ->setAutowiredMethods ($ service ['autowire ' ]);
324
+ $ autowire = isset ($ service ['autowire ' ]) ? $ service ['autowire ' ] : (isset ($ defaults ['autowire ' ]) ? $ defaults ['autowire ' ] : null );
325
+ if (null !== $ autowire ) {
326
+ if (is_array ($ autowire )) {
327
+ $ definition ->setAutowiredMethods ($ autowire );
307
328
} else {
308
- $ definition ->setAutowired ($ service [ ' autowire ' ] );
329
+ $ definition ->setAutowired ($ autowire );
309
330
}
310
331
}
311
332
0 commit comments