@@ -146,9 +146,10 @@ private function parseDefinitions($content, $file)
146146 if (!is_array ($ content ['services ' ])) {
147147 throw new InvalidArgumentException (sprintf ('The "services" key should contain an array in %s. Check your YAML syntax. ' , $ file ));
148148 }
149+ $ defaults = isset ($ content ['service_defaults ' ]) ? $ content ['service_defaults ' ] : array ();
149150
150151 foreach ($ content ['services ' ] as $ id => $ service ) {
151- $ this ->parseDefinition ($ id , $ service , $ file );
152+ $ this ->parseDefinition ($ id , $ service , $ file, $ defaults );
152153 }
153154 }
154155
@@ -158,10 +159,11 @@ private function parseDefinitions($content, $file)
158159 * @param string $id
159160 * @param array $service
160161 * @param string $file
162+ * @param array $defaults
161163 *
162164 * @throws InvalidArgumentException When tags are invalid
163165 */
164- private function parseDefinition ($ id , $ service , $ file )
166+ private function parseDefinition ($ id , $ service , $ file, array $ defaults )
165167 {
166168 if (is_string ($ service ) && 0 === strpos ($ service , '@ ' )) {
167169 $ this ->container ->setAlias ($ id , substr ($ service , 1 ));
@@ -176,7 +178,7 @@ private function parseDefinition($id, $service, $file)
176178 static ::checkDefinition ($ id , $ service , $ file );
177179
178180 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 ' ]) ;
180182 $ this ->container ->setAlias ($ id , new Alias ($ service ['alias ' ], $ public ));
181183
182184 foreach ($ service as $ key => $ value ) {
@@ -190,6 +192,7 @@ private function parseDefinition($id, $service, $file)
190192
191193 if (isset ($ service ['parent ' ])) {
192194 $ definition = new ChildDefinition ($ service ['parent ' ]);
195+ $ defaults = array ();
193196 } else {
194197 $ definition = new Definition ();
195198 }
@@ -210,8 +213,9 @@ private function parseDefinition($id, $service, $file)
210213 $ definition ->setLazy ($ service ['lazy ' ]);
211214 }
212215
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 );
215219 }
216220
217221 if (isset ($ service ['abstract ' ])) {
@@ -260,12 +264,13 @@ private function parseDefinition($id, $service, $file)
260264 }
261265 }
262266
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 )) {
265270 throw new InvalidArgumentException (sprintf ('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax. ' , $ id , $ file ));
266271 }
267272
268- foreach ($ service [ ' tags ' ] as $ tag ) {
273+ foreach ($ tags as $ tag ) {
269274 if (!is_array ($ tag )) {
270275 $ tag = array ('name ' => $ tag );
271276 }
@@ -301,11 +306,12 @@ private function parseDefinition($id, $service, $file)
301306 $ definition ->setDecoratedService ($ service ['decorates ' ], $ renameId , $ priority );
302307 }
303308
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 );
307313 } else {
308- $ definition ->setAutowired ($ service [ ' autowire ' ] );
314+ $ definition ->setAutowired ($ autowire );
309315 }
310316 }
311317
@@ -426,7 +432,7 @@ private function validate($content, $file)
426432 }
427433
428434 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 ' ))) {
430436 continue ;
431437 }
432438
0 commit comments