25
25
*/
26
26
class XmlFileLoader extends FileLoader
27
27
{
28
+ const NAMESPACE_URI = 'http://symfony.com/schema/routing ' ;
29
+ const SCHEME_PATH = '/schema/routing/routing-1.0.xsd ' ;
30
+
28
31
/**
29
32
* Loads an XML file.
30
33
*
@@ -70,7 +73,11 @@ public function load($file, $type = null)
70
73
*/
71
74
protected function parseNode (RouteCollection $ collection , \DOMElement $ node , $ path , $ file )
72
75
{
73
- switch ($ node ->tagName ) {
76
+ if (self ::NAMESPACE_URI !== $ node ->namespaceURI ) {
77
+ return ;
78
+ }
79
+
80
+ switch ($ node ->localName ) {
74
81
case 'route ' :
75
82
$ this ->parseRoute ($ collection , $ node , $ path );
76
83
break ;
@@ -84,23 +91,19 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
84
91
$ requirements = array ();
85
92
$ options = array ();
86
93
87
- foreach ($ node ->childNodes as $ n ) {
88
- if (!$ n instanceof \DOMElement) {
89
- continue ;
90
- }
91
-
92
- switch ($ n ->tagName ) {
94
+ foreach ($ node ->getElementsByTagNameNS (self ::NAMESPACE_URI , '* ' ) as $ n ) {
95
+ switch ($ n ->localName ) {
93
96
case 'default ' :
94
- $ defaults [$ n ->getAttribute ('key ' )] = trim ($ n ->nodeValue );
97
+ $ defaults [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
95
98
break ;
96
99
case 'requirement ' :
97
- $ requirements [$ n ->getAttribute ('key ' )] = trim ($ n ->nodeValue );
100
+ $ requirements [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
98
101
break ;
99
102
case 'option ' :
100
- $ options [$ n ->getAttribute ('key ' )] = trim ($ n ->nodeValue );
103
+ $ options [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
101
104
break ;
102
105
default :
103
- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ n ->tagName , $ file ));
106
+ throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ n ->localName , $ file ));
104
107
}
105
108
}
106
109
@@ -119,7 +122,7 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
119
122
$ collection ->addCollection ($ subCollection );
120
123
break ;
121
124
default :
122
- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "route" or "import". ' , $ node ->tagName , $ file ));
125
+ throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "route" or "import". ' , $ node ->localName , $ file ));
123
126
}
124
127
}
125
128
@@ -148,23 +151,19 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $definiti
148
151
$ requirements = array ();
149
152
$ options = array ();
150
153
151
- foreach ($ definition ->childNodes as $ node ) {
152
- if (!$ node instanceof \DOMElement) {
153
- continue ;
154
- }
155
-
156
- switch ($ node ->tagName ) {
154
+ foreach ($ definition ->getElementsByTagNameNS (self ::NAMESPACE_URI , '* ' ) as $ node ) {
155
+ switch ($ node ->localName ) {
157
156
case 'default ' :
158
- $ defaults [$ node ->getAttribute ('key ' )] = trim ($ node ->nodeValue );
157
+ $ defaults [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
159
158
break ;
160
159
case 'option ' :
161
- $ options [$ node ->getAttribute ('key ' )] = trim ($ node ->nodeValue );
160
+ $ options [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
162
161
break ;
163
162
case 'requirement ' :
164
- $ requirements [$ node ->getAttribute ('key ' )] = trim ($ node ->nodeValue );
163
+ $ requirements [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
165
164
break ;
166
165
default :
167
- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ node ->tagName , $ file ));
166
+ throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ node ->localName , $ file ));
168
167
}
169
168
}
170
169
@@ -220,12 +219,10 @@ protected function loadFile($file)
220
219
*/
221
220
protected function validate (\DOMDocument $ dom )
222
221
{
223
- $ location = __DIR__ .'/schema/routing/routing-1.0.xsd ' ;
224
-
225
222
$ current = libxml_use_internal_errors (true );
226
223
libxml_clear_errors ();
227
224
228
- if (!$ dom ->schemaValidate ($ location )) {
225
+ if (!$ dom ->schemaValidate (__DIR__ . static :: SCHEME_PATH )) {
229
226
throw new \InvalidArgumentException (implode ("\n" , $ this ->getXmlErrors ($ current )));
230
227
}
231
228
libxml_use_internal_errors ($ current );
0 commit comments