Skip to content
This repository
Browse code

refactor the XMlFileLoader

  • Loading branch information...
commit 8361b5a58bbb90b5b36f5ba15694fb83d5ecca0d 1 parent 83fc5ff
Tobias Schultze Tobion authored

Showing 1 changed file with 86 additions and 70 deletions. Show diff stats Hide diff stats

  1. +86 70 src/Symfony/Component/Routing/Loader/XmlFileLoader.php
156 src/Symfony/Component/Routing/Loader/XmlFileLoader.php
@@ -20,6 +20,7 @@
20 20 * XmlFileLoader loads XML routing files.
21 21 *
22 22 * @author Fabien Potencier <fabien@symfony.com>
  23 + * @author Tobias Schultze <http://tobion.de>
23 24 *
24 25 * @api
25 26 */
@@ -36,7 +37,8 @@ class XmlFileLoader extends FileLoader
36 37 *
37 38 * @return RouteCollection A RouteCollection instance
38 39 *
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.
40 42 *
41 43 * @api
42 44 */
@@ -64,12 +66,12 @@ public function load($file, $type = null)
64 66 /**
65 67 * Parses a node from a loaded XML file.
66 68 *
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
71 73 *
72   - * @throws \InvalidArgumentException When a tag can't be parsed
  74 + * @throws \InvalidArgumentException When the XML is invalid
73 75 */
74 76 protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
75 77 {
@@ -82,47 +84,10 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
82 84 $this->parseRoute($collection, $node, $path);
83 85 break;
84 86 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);
123 88 break;
124 89 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));
126 91 }
127 92 }
128 93
@@ -139,37 +104,53 @@ public function supports($resource, $type = null)
139 104 /**
140 105 * Parses a route and adds it to the RouteCollection.
141 106 *
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
145 110 *
146   - * @throws \InvalidArgumentException When the definition cannot be parsed
  111 + * @throws \InvalidArgumentException When the XML is invalid
147 112 */
148   - protected function parseRoute(RouteCollection $collection, \DOMElement $definition, $file)
  113 + protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
149 114 {
150   - $defaults = array();
151   - $requirements = array();
152   - $options = array();
  115 + list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
153 116
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'));
169 118
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);
171 152
172   - $collection->add($definition->getAttribute('id'), $route);
  153 + $collection->addCollection($subCollection);
173 154 }
174 155
175 156 /**
@@ -252,4 +233,39 @@ private function getXmlErrors($internalErrors)
252 233
253 234 return $errors;
254 235 }
  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 + }
255 271 }

0 comments on commit 8361b5a

Please sign in to comment.
Something went wrong with that request. Please try again.