@@ -50,23 +50,36 @@ public function load($resource, string $type = null)
50
50
51
51
$ this ->container ->fileExists ($ path );
52
52
53
- $ defaults = $ this ->getServiceDefaults ($ xml , $ path );
53
+ $ this ->loadXml ($ xml , $ path );
54
+
55
+ if ($ this ->env ) {
56
+ $ xpath = new \DOMXPath ($ xml );
57
+ $ xpath ->registerNamespace ('container ' , self ::NS );
58
+ foreach ($ xpath ->query (sprintf ('//container:when[@env="%s"] ' , $ this ->env )) ?: [] as $ root ) {
59
+ $ this ->loadXml ($ xml , $ path , $ root );
60
+ }
61
+ }
62
+ }
63
+
64
+ private function loadXml (\DOMDocument $ xml , string $ path , \DOMNode $ root = null ): void
65
+ {
66
+ $ defaults = $ this ->getServiceDefaults ($ xml , $ path , $ root );
54
67
55
68
// anonymous services
56
- $ this ->processAnonymousServices ($ xml , $ path );
69
+ $ this ->processAnonymousServices ($ xml , $ path, $ root );
57
70
58
71
// imports
59
- $ this ->parseImports ($ xml , $ path );
72
+ $ this ->parseImports ($ xml , $ path, $ root );
60
73
61
74
// parameters
62
- $ this ->parseParameters ($ xml , $ path );
75
+ $ this ->parseParameters ($ xml , $ path, $ root );
63
76
64
77
// extensions
65
- $ this ->loadFromExtensions ($ xml );
78
+ $ this ->loadFromExtensions ($ xml, $ root );
66
79
67
80
// services
68
81
try {
69
- $ this ->parseDefinitions ($ xml , $ path , $ defaults );
82
+ $ this ->parseDefinitions ($ xml , $ path , $ defaults, $ root );
70
83
} finally {
71
84
$ this ->instanceof = [];
72
85
$ this ->registerAliasesForSinglyImplementedInterfaces ();
@@ -89,19 +102,19 @@ public function supports($resource, string $type = null)
89
102
return 'xml ' === $ type ;
90
103
}
91
104
92
- private function parseParameters (\DOMDocument $ xml , string $ file )
105
+ private function parseParameters (\DOMDocument $ xml , string $ file, \ DOMNode $ root = null )
93
106
{
94
- if ($ parameters = $ this ->getChildren ($ xml ->documentElement , 'parameters ' )) {
107
+ if ($ parameters = $ this ->getChildren ($ root ?? $ xml ->documentElement , 'parameters ' )) {
95
108
$ this ->container ->getParameterBag ()->add ($ this ->getArgumentsAsPhp ($ parameters [0 ], 'parameter ' , $ file ));
96
109
}
97
110
}
98
111
99
- private function parseImports (\DOMDocument $ xml , string $ file )
112
+ private function parseImports (\DOMDocument $ xml , string $ file, \ DOMNode $ root = null )
100
113
{
101
114
$ xpath = new \DOMXPath ($ xml );
102
115
$ xpath ->registerNamespace ('container ' , self ::NS );
103
116
104
- if (false === $ imports = $ xpath ->query ('//container:imports/container:import ' )) {
117
+ if (false === $ imports = $ xpath ->query ('. //container:imports/container:import ' , $ root )) {
105
118
return ;
106
119
}
107
120
@@ -112,19 +125,19 @@ private function parseImports(\DOMDocument $xml, string $file)
112
125
}
113
126
}
114
127
115
- private function parseDefinitions (\DOMDocument $ xml , string $ file , Definition $ defaults )
128
+ private function parseDefinitions (\DOMDocument $ xml , string $ file , Definition $ defaults, \ DOMNode $ root = null )
116
129
{
117
130
$ xpath = new \DOMXPath ($ xml );
118
131
$ xpath ->registerNamespace ('container ' , self ::NS );
119
132
120
- if (false === $ services = $ xpath ->query ('//container:services/container:service|//container:services/container:prototype|//container:services/container:stack ' )) {
133
+ if (false === $ services = $ xpath ->query ('. //container:services/container:service|. //container:services/container:prototype|. //container:services/container:stack ' , $ root )) {
121
134
return ;
122
135
}
123
136
$ this ->setCurrentDir (\dirname ($ file ));
124
137
125
138
$ this ->instanceof = [];
126
139
$ this ->isLoadingInstanceof = true ;
127
- $ instanceof = $ xpath ->query ('//container:services/container:instanceof ' );
140
+ $ instanceof = $ xpath ->query ('. //container:services/container:instanceof ' , $ root );
128
141
foreach ($ instanceof as $ service ) {
129
142
$ this ->setDefinition ((string ) $ service ->getAttribute ('id ' ), $ this ->parseDefinition ($ service , $ file , new Definition ()));
130
143
}
@@ -170,12 +183,12 @@ private function parseDefinitions(\DOMDocument $xml, string $file, Definition $d
170
183
}
171
184
}
172
185
173
- private function getServiceDefaults (\DOMDocument $ xml , string $ file ): Definition
186
+ private function getServiceDefaults (\DOMDocument $ xml , string $ file, \ DOMNode $ root = null ): Definition
174
187
{
175
188
$ xpath = new \DOMXPath ($ xml );
176
189
$ xpath ->registerNamespace ('container ' , self ::NS );
177
190
178
- if (null === $ defaultsNode = $ xpath ->query ('//container:services/container:defaults ' )->item (0 )) {
191
+ if (null === $ defaultsNode = $ xpath ->query ('. //container:services/container:defaults ' , $ root )->item (0 )) {
179
192
return new Definition ();
180
193
}
181
194
@@ -393,7 +406,7 @@ private function parseFileToDOM(string $file): \DOMDocument
393
406
/**
394
407
* Processes anonymous services.
395
408
*/
396
- private function processAnonymousServices (\DOMDocument $ xml , string $ file )
409
+ private function processAnonymousServices (\DOMDocument $ xml , string $ file, \ DOMNode $ root = null )
397
410
{
398
411
$ definitions = [];
399
412
$ count = 0 ;
@@ -403,7 +416,7 @@ private function processAnonymousServices(\DOMDocument $xml, string $file)
403
416
$ xpath ->registerNamespace ('container ' , self ::NS );
404
417
405
418
// anonymous services as arguments/properties
406
- if (false !== $ nodes = $ xpath ->query ('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]|//container:bind[not(@id)]|//container:factory[not(@service)]|//container:configurator[not(@service)] ' )) {
419
+ if (false !== $ nodes = $ xpath ->query ('. //container:argument[@type="service"][not(@id)]|. //container:property[@type="service"][not(@id)]|. //container:bind[not(@id)]|. //container:factory[not(@service)]|. //container:configurator[not(@service)] ' , $ root )) {
407
420
foreach ($ nodes as $ node ) {
408
421
if ($ services = $ this ->getChildren ($ node , 'service ' )) {
409
422
// give it a unique name
@@ -422,7 +435,7 @@ private function processAnonymousServices(\DOMDocument $xml, string $file)
422
435
}
423
436
424
437
// anonymous services "in the wild"
425
- if (false !== $ nodes = $ xpath ->query ('//container:services/container:service[not(@id)] ' )) {
438
+ if (false !== $ nodes = $ xpath ->query ('. //container:services/container:service[not(@id)] ' , $ root )) {
426
439
foreach ($ nodes as $ node ) {
427
440
throw new InvalidArgumentException (sprintf ('Top-level services must have "id" attribute, none found in "%s" at line %d. ' , $ file , $ node ->getLineNo ()));
428
441
}
0 commit comments