@@ -116,28 +116,67 @@ 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 ) )) {
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
+ * @param \DOMDocument $xml
129
+ *
130
+ * @return array
131
+ */
132
+ private function getServiceDefaults (\DOMDocument $ xml )
133
+ {
134
+ $ xpath = new \DOMXPath ($ xml );
135
+ $ xpath ->registerNamespace ('container ' , self ::NS );
136
+
137
+ $ defaults = array ();
138
+
139
+ if (null === $ defaultsNode = $ xpath ->query ('//container:services/container:defaults ' )->item (0 )) {
140
+ return $ defaults ;
141
+ }
142
+
143
+ if ($ defaultsNode ->hasAttribute ('public ' )) {
144
+ $ defaults ['public ' ] = XmlUtils::phpize ($ defaultsNode ->getAttribute ('public ' ));
145
+ }
146
+
147
+ if ($ defaultsNode ->hasAttribute ('autowire ' )) {
148
+ $ defaults ['autowire ' ] = XmlUtils::phpize ($ defaultsNode ->getAttribute ('autowire ' ));
149
+ }
150
+
151
+ $ defaults ['tags ' ] = $ this ->getChildren ($ defaultsNode , 'tag ' );
152
+
153
+ $ defaults ['autowire_methods ' ] = [];
154
+ foreach ($ this ->getChildren ($ defaultsNode , 'autowire ' ) as $ type ) {
155
+ $ defaults ['autowire_methods ' ][] = $ type ->textContent ;
156
+ }
157
+
158
+ return $ defaults ;
159
+ }
160
+
125
161
/**
126
162
* Parses an individual Definition.
127
163
*
128
164
* @param \DOMElement $service
129
165
* @param string $file
166
+ * @param array $defaults
130
167
*
131
168
* @return Definition|null
132
169
*/
133
- private function parseDefinition (\DOMElement $ service , $ file )
170
+ private function parseDefinition (\DOMElement $ service , $ file, array $ defaults = array () )
134
171
{
135
172
if ($ alias = $ service ->getAttribute ('alias ' )) {
136
173
$ this ->validateAlias ($ service , $ file );
137
174
138
175
$ public = true ;
139
176
if ($ publicAttr = $ service ->getAttribute ('public ' )) {
140
177
$ public = XmlUtils::phpize ($ publicAttr );
178
+ } elseif (isset ($ defaults ['public ' ])) {
179
+ $ public = $ defaults ['public ' ];
141
180
}
142
181
$ this ->container ->setAlias ((string ) $ service ->getAttribute ('id ' ), new Alias ($ alias , $ public ));
143
182
@@ -146,11 +185,18 @@ private function parseDefinition(\DOMElement $service, $file)
146
185
147
186
if ($ parent = $ service ->getAttribute ('parent ' )) {
148
187
$ definition = new ChildDefinition ($ parent );
188
+ $ defaults = array ();
149
189
} else {
150
190
$ definition = new Definition ();
151
191
}
152
192
153
- foreach (array ('class ' , 'shared ' , 'public ' , 'synthetic ' , 'lazy ' , 'abstract ' ) as $ key ) {
193
+ if ($ publicAttr = $ service ->getAttribute ('public ' )) {
194
+ $ definition ->setPublic (XmlUtils::phpize ($ publicAttr ));
195
+ } elseif (isset ($ defaults ['public ' ])) {
196
+ $ definition ->setPublic ($ defaults ['public ' ]);
197
+ }
198
+
199
+ foreach (array ('class ' , 'shared ' , 'synthetic ' , 'lazy ' , 'abstract ' ) as $ key ) {
154
200
if ($ value = $ service ->getAttribute ($ key )) {
155
201
$ method = 'set ' .$ key ;
156
202
$ definition ->$ method (XmlUtils::phpize ($ value ));
@@ -159,6 +205,8 @@ private function parseDefinition(\DOMElement $service, $file)
159
205
160
206
if ($ value = $ service ->getAttribute ('autowire ' )) {
161
207
$ definition ->setAutowired (XmlUtils::phpize ($ value ));
208
+ } elseif (isset ($ defaults ['autowire ' ])) {
209
+ $ definition ->setAutowired ($ defaults ['autowire ' ]);
162
210
}
163
211
164
212
if ($ files = $ this ->getChildren ($ service , 'file ' )) {
@@ -214,7 +262,12 @@ private function parseDefinition(\DOMElement $service, $file)
214
262
$ definition ->addMethodCall ($ call ->getAttribute ('method ' ), $ this ->getArgumentsAsPhp ($ call , 'argument ' ));
215
263
}
216
264
217
- foreach ($ this ->getChildren ($ service , 'tag ' ) as $ tag ) {
265
+ $ tags = $ this ->getChildren ($ service , 'tag ' );
266
+ if (0 === count ($ tags ) && isset ($ defaults ['tags ' ])) {
267
+ $ tags = $ defaults ['tags ' ];
268
+ }
269
+
270
+ foreach ($ tags as $ tag ) {
218
271
$ parameters = array ();
219
272
foreach ($ tag ->attributes as $ name => $ node ) {
220
273
if ('name ' === $ name ) {
@@ -244,6 +297,10 @@ private function parseDefinition(\DOMElement $service, $file)
244
297
$ autowireTags [] = $ type ->textContent ;
245
298
}
246
299
300
+ if (0 === count ($ autowireTags ) && isset ($ defaults ['autowire_methods ' ])) {
301
+ $ autowireTags = $ defaults ['autowire_methods ' ];
302
+ }
303
+
247
304
if ($ autowireTags ) {
248
305
if ($ service ->hasAttribute ('autowire ' )) {
249
306
throw new InvalidArgumentException (sprintf ('The "autowire" attribute cannot be used together with "<autowire>" tags for service "%s" in %s. ' , (string ) $ service ->getAttribute ('id ' ), $ file ));
0 commit comments