[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\Routing\Loader; 13 14 use Symfony\Component\Routing\RouteCollection; 15 use Symfony\Component\Routing\Route; 16 use Symfony\Component\Config\Resource\FileResource; 17 use Symfony\Component\Config\Loader\FileLoader; 18 use Symfony\Component\Config\Util\XmlUtils; 19 20 /** 21 * XmlFileLoader loads XML routing files. 22 * 23 * @author Fabien Potencier <fabien@symfony.com> 24 * @author Tobias Schultze <http://tobion.de> 25 */ 26 class XmlFileLoader extends FileLoader 27 { 28 const NAMESPACE_URI = 'http://symfony.com/schema/routing'; 29 const SCHEME_PATH = '/schema/routing/routing-1.0.xsd'; 30 31 /** 32 * Loads an XML file. 33 * 34 * @param string $file An XML file path 35 * @param string|null $type The resource type 36 * 37 * @return RouteCollection A RouteCollection instance 38 * 39 * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be 40 * parsed because it does not validate against the scheme. 41 */ 42 public function load($file, $type = null) 43 { 44 $path = $this->locator->locate($file); 45 46 $xml = $this->loadFile($path); 47 48 $collection = new RouteCollection(); 49 $collection->addResource(new FileResource($path)); 50 51 // process routes and imports 52 foreach ($xml->documentElement->childNodes as $node) { 53 if (!$node instanceof \DOMElement) { 54 continue; 55 } 56 57 $this->parseNode($collection, $node, $path, $file); 58 } 59 60 return $collection; 61 } 62 63 /** 64 * Parses a node from a loaded XML file. 65 * 66 * @param RouteCollection $collection Collection to associate with the node 67 * @param \DOMElement $node Element to parse 68 * @param string $path Full path of the XML file being processed 69 * @param string $file Loaded file name 70 * 71 * @throws \InvalidArgumentException When the XML is invalid 72 */ 73 protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) 74 { 75 if (self::NAMESPACE_URI !== $node->namespaceURI) { 76 return; 77 } 78 79 switch ($node->localName) { 80 case 'route': 81 $this->parseRoute($collection, $node, $path); 82 break; 83 case 'import': 84 $this->parseImport($collection, $node, $path, $file); 85 break; 86 default: 87 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path)); 88 } 89 } 90 91 /** 92 * {@inheritdoc} 93 */ 94 public function supports($resource, $type = null) 95 { 96 return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type); 97 } 98 99 /** 100 * Parses a route and adds it to the RouteCollection. 101 * 102 * @param RouteCollection $collection RouteCollection instance 103 * @param \DOMElement $node Element to parse that represents a Route 104 * @param string $path Full path of the XML file being processed 105 * 106 * @throws \InvalidArgumentException When the XML is invalid 107 */ 108 protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path) 109 { 110 if ('' === ($id = $node->getAttribute('id')) || (!$node->hasAttribute('pattern') && !$node->hasAttribute('path'))) { 111 throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path)); 112 } 113 114 if ($node->hasAttribute('pattern')) { 115 if ($node->hasAttribute('path')) { 116 throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path)); 117 } 118 119 $node->setAttribute('path', $node->getAttribute('pattern')); 120 $node->removeAttribute('pattern'); 121 } 122 123 $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY); 124 $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY); 125 126 list($defaults, $requirements, $options) = $this->parseConfigs($node, $path); 127 128 $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods); 129 $collection->add($id, $route); 130 } 131 132 /** 133 * Parses an import and adds the routes in the resource to the RouteCollection. 134 * 135 * @param RouteCollection $collection RouteCollection instance 136 * @param \DOMElement $node Element to parse that represents a Route 137 * @param string $path Full path of the XML file being processed 138 * @param string $file Loaded file name 139 * 140 * @throws \InvalidArgumentException When the XML is invalid 141 */ 142 protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file) 143 { 144 if ('' === $resource = $node->getAttribute('resource')) { 145 throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path)); 146 } 147 148 $type = $node->getAttribute('type'); 149 $prefix = $node->getAttribute('prefix'); 150 $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null; 151 $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null; 152 $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null; 153 154 list($defaults, $requirements, $options) = $this->parseConfigs($node, $path); 155 156 $this->setCurrentDir(dirname($path)); 157 158 $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file); 159 /* @var $subCollection RouteCollection */ 160 $subCollection->addPrefix($prefix); 161 if (null !== $host) { 162 $subCollection->setHost($host); 163 } 164 if (null !== $schemes) { 165 $subCollection->setSchemes($schemes); 166 } 167 if (null !== $methods) { 168 $subCollection->setMethods($methods); 169 } 170 $subCollection->addDefaults($defaults); 171 $subCollection->addRequirements($requirements); 172 $subCollection->addOptions($options); 173 174 $collection->addCollection($subCollection); 175 } 176 177 /** 178 * Loads an XML file. 179 * 180 * @param string $file An XML file path 181 * 182 * @return \DOMDocument 183 * 184 * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors 185 * or when the XML structure is not as expected by the scheme - 186 * see validate() 187 */ 188 protected function loadFile($file) 189 { 190 return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH); 191 } 192 193 /** 194 * Parses the config elements (default, requirement, option). 195 * 196 * @param \DOMElement $node Element to parse that contains the configs 197 * @param string $path Full path of the XML file being processed 198 * 199 * @return array An array with the defaults as first item, requirements as second and options as third. 200 * 201 * @throws \InvalidArgumentException When the XML is invalid 202 */ 203 private function parseConfigs(\DOMElement $node, $path) 204 { 205 $defaults = array(); 206 $requirements = array(); 207 $options = array(); 208 209 foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) { 210 switch ($n->localName) { 211 case 'default': 212 if ($this->isElementValueNull($n)) { 213 $defaults[$n->getAttribute('key')] = null; 214 } else { 215 $defaults[$n->getAttribute('key')] = trim($n->textContent); 216 } 217 218 break; 219 case 'requirement': 220 $requirements[$n->getAttribute('key')] = trim($n->textContent); 221 break; 222 case 'option': 223 $options[$n->getAttribute('key')] = trim($n->textContent); 224 break; 225 default: 226 throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path)); 227 } 228 } 229 230 return array($defaults, $requirements, $options); 231 } 232 233 private function isElementValueNull(\DOMElement $element) 234 { 235 $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance'; 236 237 if (!$element->hasAttributeNS($namespaceUri, 'nil')) { 238 return false; 239 } 240 241 return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil'); 242 } 243 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |