@@ -116,28 +116,74 @@ private function parseDefinitions(\DOMDocument $xml, $file)
116
116
}
117
117
118
118
foreach ($ services as $ service ) {
119
- if (null !== $ definition = $ this ->parseDefinition ($ service , $ file )) {
119
+ if (null !== $ definition = $ this ->parseDefinition ($ service , $ file, $ this -> getServiceDefaults ( $ xml , $ file ) )) {
120
120
$ this ->container ->setDefinition ((string ) $ service ->getAttribute ('id ' ), $ definition );
121
121
}
122
122
}
123
123
}
124
124
125
+ /**
126
+ * Get service defaults.
127
+ *
128
+ * @return array
129
+ */
130
+ private function getServiceDefaults (\DOMDocument $ xml , $ file )
131
+ {
132
+ $ xpath = new \DOMXPath ($ xml );
133
+ $ xpath ->registerNamespace ('container ' , self ::NS );
134
+
135
+ if (null === $ defaultsNode = $ xpath ->query ('//container:services/container:defaults ' )->item (0 )) {
136
+ return array ();
137
+ }
138
+ $ defaults = array (
139
+ 'tags ' => $ this ->getChildren ($ defaultsNode , 'tag ' ),
140
+ 'autowire ' => $ this ->getChildren ($ defaultsNode , 'autowire ' ),
141
+ );
142
+
143
+ foreach ($ defaults ['tags ' ] as $ tag ) {
144
+ if ('' === $ tag ->getAttribute ('name ' )) {
145
+ throw new InvalidArgumentException (sprintf ('The tag name for tag "<defaults>" in %s must be a non-empty string. ' , $ file ));
146
+ }
147
+ }
148
+ if ($ defaultsNode ->hasAttribute ('public ' )) {
149
+ $ defaults ['public ' ] = XmlUtils::phpize ($ defaultsNode ->getAttribute ('public ' ));
150
+ }
151
+ if (!$ defaultsNode ->hasAttribute ('autowire ' )) {
152
+ foreach ($ defaults ['autowire ' ] as $ k => $ v ) {
153
+ $ defaults ['autowire ' ][$ k ] = $ v ->textContent ;
154
+ }
155
+
156
+ return $ defaults ;
157
+ }
158
+ if ($ defaults ['autowire ' ]) {
159
+ throw new InvalidArgumentException (sprintf ('The "autowire" attribute cannot be used together with "<autowire>" tags for tag "<defaults>" in %s. ' , $ file ));
160
+ }
161
+ if (XmlUtils::phpize ($ defaultsNode ->getAttribute ('autowire ' ))) {
162
+ $ defaults ['autowire ' ][] = '__construct ' ;
163
+ }
164
+
165
+ return $ defaults ;
166
+ }
167
+
125
168
/**
126
169
* Parses an individual Definition.
127
170
*
128
171
* @param \DOMElement $service
129
172
* @param string $file
173
+ * @param array $defaults
130
174
*
131
175
* @return Definition|null
132
176
*/
133
- private function parseDefinition (\DOMElement $ service , $ file )
177
+ private function parseDefinition (\DOMElement $ service , $ file, array $ defaults = array () )
134
178
{
135
179
if ($ alias = $ service ->getAttribute ('alias ' )) {
136
180
$ this ->validateAlias ($ service , $ file );
137
181
138
182
$ public = true ;
139
183
if ($ publicAttr = $ service ->getAttribute ('public ' )) {
140
184
$ public = XmlUtils::phpize ($ publicAttr );
185
+ } elseif (isset ($ defaults ['public ' ])) {
186
+ $ public = $ defaults ['public ' ];
141
187
}
142
188
$ this ->container ->setAlias ((string ) $ service ->getAttribute ('id ' ), new Alias ($ alias , $ public ));
143
189
@@ -146,11 +192,18 @@ private function parseDefinition(\DOMElement $service, $file)
146
192
147
193
if ($ parent = $ service ->getAttribute ('parent ' )) {
148
194
$ definition = new ChildDefinition ($ parent );
195
+ $ defaults = array ();
149
196
} else {
150
197
$ definition = new Definition ();
151
198
}
152
199
153
- foreach (array ('class ' , 'shared ' , 'public ' , 'synthetic ' , 'lazy ' , 'abstract ' ) as $ key ) {
200
+ if ($ publicAttr = $ service ->getAttribute ('public ' )) {
201
+ $ definition ->setPublic (XmlUtils::phpize ($ publicAttr ));
202
+ } elseif (isset ($ defaults ['public ' ])) {
203
+ $ definition ->setPublic ($ defaults ['public ' ]);
204
+ }
205
+
206
+ foreach (array ('class ' , 'shared ' , 'synthetic ' , 'lazy ' , 'abstract ' ) as $ key ) {
154
207
if ($ value = $ service ->getAttribute ($ key )) {
155
208
$ method = 'set ' .$ key ;
156
209
$ definition ->$ method (XmlUtils::phpize ($ value ));
@@ -214,7 +267,12 @@ private function parseDefinition(\DOMElement $service, $file)
214
267
$ definition ->addMethodCall ($ call ->getAttribute ('method ' ), $ this ->getArgumentsAsPhp ($ call , 'argument ' ));
215
268
}
216
269
217
- foreach ($ this ->getChildren ($ service , 'tag ' ) as $ tag ) {
270
+ $ tags = $ this ->getChildren ($ service , 'tag ' );
271
+ if (!$ tags && !empty ($ defaults ['tags ' ])) {
272
+ $ tags = $ defaults ['tags ' ];
273
+ }
274
+
275
+ foreach ($ tags as $ tag ) {
218
276
$ parameters = array ();
219
277
foreach ($ tag ->attributes as $ name => $ node ) {
220
278
if ('name ' === $ name ) {
@@ -250,6 +308,8 @@ private function parseDefinition(\DOMElement $service, $file)
250
308
}
251
309
252
310
$ definition ->setAutowiredMethods ($ autowireTags );
311
+ } elseif (!$ service ->hasAttribute ('autowire ' ) && !empty ($ defaults ['autowire ' ])) {
312
+ $ definition ->setAutowiredMethods ($ defaults ['autowire ' ]);
253
313
}
254
314
255
315
if ($ value = $ service ->getAttribute ('decorates ' )) {
0 commit comments