2020 * XmlFileLoader loads XML routing files.
2121 *
2222 * @author Fabien Potencier <fabien@symfony.com>
23+ * @author Tobias Schultze <http://tobion.de>
2324 *
2425 * @api
2526 */
@@ -36,7 +37,8 @@ class XmlFileLoader extends FileLoader
3637 *
3738 * @return RouteCollection A RouteCollection instance
3839 *
39- * @throws \InvalidArgumentException When a tag can't be parsed
40+ * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
41+ * parsed because it does not validate against the scheme.
4042 *
4143 * @api
4244 */
@@ -64,12 +66,12 @@ public function load($file, $type = null)
6466 /**
6567 * Parses a node from a loaded XML file.
6668 *
67- * @param RouteCollection $collection the collection to associate with the node
68- * @param \DOMElement $node the node to parse
69- * @param string $path the path of the XML file being processed
70- * @param string $file
69+ * @param RouteCollection $collection Collection to associate with the node
70+ * @param \DOMElement $node Element to parse
71+ * @param string $path Full path of the XML file being processed
72+ * @param string $file Loaded file name
7173 *
72- * @throws \InvalidArgumentException When a tag can't be parsed
74+ * @throws \InvalidArgumentException When the XML is invalid
7375 */
7476 protected function parseNode (RouteCollection $ collection , \DOMElement $ node , $ path , $ file )
7577 {
@@ -82,47 +84,10 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
8284 $ this ->parseRoute ($ collection , $ node , $ path );
8385 break ;
8486 case 'import ' :
85- $ resource = $ node ->getAttribute ('resource ' );
86- $ type = $ node ->getAttribute ('type ' );
87- $ prefix = $ node ->getAttribute ('prefix ' );
88- $ hostnamePattern = $ node ->hasAttribute ('hostname-pattern ' ) ? $ node ->getAttribute ('hostname-pattern ' ) : null ;
89-
90- $ defaults = array ();
91- $ requirements = array ();
92- $ options = array ();
93-
94- foreach ($ node ->getElementsByTagNameNS (self ::NAMESPACE_URI , '* ' ) as $ n ) {
95- switch ($ n ->localName ) {
96- case 'default ' :
97- $ defaults [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
98- break ;
99- case 'requirement ' :
100- $ requirements [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
101- break ;
102- case 'option ' :
103- $ options [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
104- break ;
105- default :
106- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ n ->localName , $ file ));
107- }
108- }
109-
110- $ this ->setCurrentDir (dirname ($ path ));
111-
112- $ subCollection = $ this ->import ($ resource , ('' !== $ type ? $ type : null ), false , $ file );
113- /* @var $subCollection RouteCollection */
114- $ subCollection ->addPrefix ($ prefix );
115- if (null !== $ hostnamePattern ) {
116- $ subCollection ->setHostnamePattern ($ hostnamePattern );
117- }
118- $ subCollection ->addDefaults ($ defaults );
119- $ subCollection ->addRequirements ($ requirements );
120- $ subCollection ->addOptions ($ options );
121-
122- $ collection ->addCollection ($ subCollection );
87+ $ this ->parseImport ($ collection , $ node , $ path , $ file );
12388 break ;
12489 default :
125- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "route" or "import". ' , $ node ->localName , $ file ));
90+ throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "route" or "import". ' , $ node ->localName , $ path ));
12691 }
12792 }
12893
@@ -139,37 +104,53 @@ public function supports($resource, $type = null)
139104 /**
140105 * Parses a route and adds it to the RouteCollection.
141106 *
142- * @param RouteCollection $collection A RouteCollection instance
143- * @param \DOMElement $definition Route definition
144- * @param string $file An XML file path
107+ * @param RouteCollection $collection RouteCollection instance
108+ * @param \DOMElement $node Element to parse that represents a Route
109+ * @param string $path Full path of the XML file being processed
145110 *
146- * @throws \InvalidArgumentException When the definition cannot be parsed
111+ * @throws \InvalidArgumentException When the XML is invalid
147112 */
148- protected function parseRoute (RouteCollection $ collection , \DOMElement $ definition , $ file )
113+ protected function parseRoute (RouteCollection $ collection , \DOMElement $ node , $ path )
149114 {
150- $ defaults = array ();
151- $ requirements = array ();
152- $ options = array ();
115+ list ($ defaults , $ requirements , $ options ) = $ this ->parseConfigs ($ node , $ path );
153116
154- foreach ($ definition ->getElementsByTagNameNS (self ::NAMESPACE_URI , '* ' ) as $ node ) {
155- switch ($ node ->localName ) {
156- case 'default ' :
157- $ defaults [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
158- break ;
159- case 'option ' :
160- $ options [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
161- break ;
162- case 'requirement ' :
163- $ requirements [$ node ->getAttribute ('key ' )] = trim ($ node ->textContent );
164- break ;
165- default :
166- throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ node ->localName , $ file ));
167- }
168- }
117+ $ route = new Route ($ node ->getAttribute ('pattern ' ), $ defaults , $ requirements , $ options , $ node ->getAttribute ('hostname-pattern ' ));
169118
170- $ route = new Route ($ definition ->getAttribute ('pattern ' ), $ defaults , $ requirements , $ options , $ definition ->getAttribute ('hostname-pattern ' ));
119+ $ collection ->add ($ node ->getAttribute ('id ' ), $ route );
120+ }
121+
122+ /**
123+ * Parses an import and adds the routes in the resource to the RouteCollection.
124+ *
125+ * @param RouteCollection $collection RouteCollection instance
126+ * @param \DOMElement $node Element to parse that represents a Route
127+ * @param string $path Full path of the XML file being processed
128+ * @param string $file Loaded file name
129+ *
130+ * @throws \InvalidArgumentException When the XML is invalid
131+ */
132+ protected function parseImport (RouteCollection $ collection , \DOMElement $ node , $ path , $ file )
133+ {
134+ $ resource = $ node ->getAttribute ('resource ' );
135+ $ type = $ node ->getAttribute ('type ' );
136+ $ prefix = $ node ->getAttribute ('prefix ' );
137+ $ hostnamePattern = $ node ->hasAttribute ('hostname-pattern ' ) ? $ node ->getAttribute ('hostname-pattern ' ) : null ;
138+
139+ list ($ defaults , $ requirements , $ options ) = $ this ->parseConfigs ($ node , $ path );
140+
141+ $ this ->setCurrentDir (dirname ($ path ));
142+
143+ $ subCollection = $ this ->import ($ resource , ('' !== $ type ? $ type : null ), false , $ file );
144+ /* @var $subCollection RouteCollection */
145+ $ subCollection ->addPrefix ($ prefix );
146+ if (null !== $ hostnamePattern ) {
147+ $ subCollection ->setHostnamePattern ($ hostnamePattern );
148+ }
149+ $ subCollection ->addDefaults ($ defaults );
150+ $ subCollection ->addRequirements ($ requirements );
151+ $ subCollection ->addOptions ($ options );
171152
172- $ collection ->add ( $ definition -> getAttribute ( ' id ' ), $ route );
153+ $ collection ->addCollection ( $ subCollection );
173154 }
174155
175156 /**
@@ -252,4 +233,39 @@ private function getXmlErrors($internalErrors)
252233
253234 return $ errors ;
254235 }
236+
237+ /**
238+ * Parses the config elements (default, requirement, option).
239+ *
240+ * @param \DOMElement $node Element to parse that contains the configs
241+ * @param string $path Full path of the XML file being processed
242+ *
243+ * @return array An array with the defaults as first item, requirements as second and options as third.
244+ *
245+ * @throws \InvalidArgumentException When the XML is invalid
246+ */
247+ private function parseConfigs (\DOMElement $ node , $ path )
248+ {
249+ $ defaults = array ();
250+ $ requirements = array ();
251+ $ options = array ();
252+
253+ foreach ($ node ->getElementsByTagNameNS (self ::NAMESPACE_URI , '* ' ) as $ n ) {
254+ switch ($ n ->localName ) {
255+ case 'default ' :
256+ $ defaults [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
257+ break ;
258+ case 'requirement ' :
259+ $ requirements [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
260+ break ;
261+ case 'option ' :
262+ $ options [$ n ->getAttribute ('key ' )] = trim ($ n ->textContent );
263+ break ;
264+ default :
265+ throw new \InvalidArgumentException (sprintf ('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option". ' , $ n ->localName , $ path ));
266+ }
267+ }
268+
269+ return array ($ defaults , $ requirements , $ options );
270+ }
255271}
0 commit comments