|
| 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\Bundle\FrameworkBundle\Routing\Loader; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; |
| 16 | +use Symfony\Component\Config\Util\Exception\InvalidXmlException; |
| 17 | +use Symfony\Component\Config\Util\Exception\XmlParsingException; |
| 18 | +use Symfony\Component\Config\Util\XmlUtils; |
| 19 | +use Symfony\Component\Routing\Loader\XmlFileLoader as BaseXmlFileLoader; |
| 20 | +use Symfony\Component\Routing\Route; |
| 21 | +use Symfony\Component\Routing\RouteCollection; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Jules Pietri <jules@heahprod.com> |
| 25 | + */ |
| 26 | +class XmlFileLoader extends BaseXmlFileLoader |
| 27 | +{ |
| 28 | + public const SCHEME_PATH = __DIR__.'/../../Resources/config/schema/framework-routing-1.0.xsd'; |
| 29 | + |
| 30 | + private const REDEFINED_SCHEME_URI = 'https://symfony.com/schema/routing/routing-1.0.xsd'; |
| 31 | + private const SCHEME_URI = 'https://symfony.com/schema/routing/framework-routing-1.0.xsd'; |
| 32 | + private const SCHEMA_LOCATIONS = [ |
| 33 | + self::REDEFINED_SCHEME_URI => parent::SCHEME_PATH, |
| 34 | + self::SCHEME_URI => self::SCHEME_PATH, |
| 35 | + ]; |
| 36 | + |
| 37 | + /** @var \DOMDocument */ |
| 38 | + private $document; |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + protected function loadFile(string $file) |
| 44 | + { |
| 45 | + if ('' === trim($content = @file_get_contents($file))) { |
| 46 | + throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid XML, it is empty.', $file)); |
| 47 | + } |
| 48 | + |
| 49 | + foreach (self::SCHEMA_LOCATIONS as $uri => $path) { |
| 50 | + if (false !== strpos($content, $uri)) { |
| 51 | + $content = str_replace($uri, self::getRealSchemePath($path), $content); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + return $this->document = XmlUtils::parse($content, function (\DOMDocument $document) { |
| 57 | + return @$document->schemaValidateSource(str_replace( |
| 58 | + self::REDEFINED_SCHEME_URI, |
| 59 | + self::getRealSchemePath(parent::SCHEME_PATH), |
| 60 | + file_get_contents(self::SCHEME_PATH) |
| 61 | + )); |
| 62 | + }); |
| 63 | + } catch (InvalidXmlException $e) { |
| 64 | + throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) |
| 72 | + { |
| 73 | + switch ($node->localName) { |
| 74 | + case 'template-route': |
| 75 | + case 'redirect-route': |
| 76 | + case 'url-redirect-route': |
| 77 | + case 'gone-route': |
| 78 | + if (self::NAMESPACE_URI !== $node->namespaceURI) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $this->parseRoute($collection, $node, $path); |
| 83 | + |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + parent::parseNode($collection, $node, $path, $file); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritdoc} |
| 92 | + */ |
| 93 | + protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path) |
| 94 | + { |
| 95 | + $templateContext = []; |
| 96 | + |
| 97 | + if ('template-route' === $node->localName) { |
| 98 | + /** @var \DOMElement $context */ |
| 99 | + foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, 'context') as $context) { |
| 100 | + $node->removeChild($context); |
| 101 | + $map = $this->document->createElementNS(self::NAMESPACE_URI, 'map'); |
| 102 | + |
| 103 | + // extract context vars into a map |
| 104 | + foreach ($context->childNodes as $n) { |
| 105 | + if (!$n instanceof \DOMElement) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $map->appendChild($n); |
| 110 | + } |
| 111 | + |
| 112 | + $default = $this->document->createElementNS(self::NAMESPACE_URI, 'default'); |
| 113 | + $default->setAttribute('key', 'context'); |
| 114 | + $default->appendChild($map); |
| 115 | + |
| 116 | + $templateContext = $this->parseDefaultsConfig($default, $path); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + parent::parseRoute($collection, $node, $path); |
| 121 | + |
| 122 | + if ($route = $collection->get($id = $node->getAttribute(('id')))) { |
| 123 | + $this->parseConfig($node, $route, $templateContext); |
| 124 | + |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, 'path') as $n) { |
| 129 | + $route = $collection->get($id.'.'.$n->getAttribute('locale')); |
| 130 | + |
| 131 | + $this->parseConfig($node, $route, $templateContext); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private function parseConfig(\DOMElement $node, Route $route, array $templateContext): void |
| 136 | + { |
| 137 | + switch ($node->localName) { |
| 138 | + case 'template-route': |
| 139 | + $route |
| 140 | + ->setDefault('_controller', TemplateController::class) |
| 141 | + ->setDefault('template', $node->getAttribute('template')) |
| 142 | + ->setDefault('context', $templateContext) |
| 143 | + ->setDefault('maxAge', (int) $node->getAttribute('max-age') ?: null) |
| 144 | + ->setDefault('sharedAge', (int) $node->getAttribute('shared-max-age') ?: null) |
| 145 | + ->setDefault('private', $node->hasAttribute('private') ? XmlUtils::phpize($node->getAttribute('private')) : null) |
| 146 | + ; |
| 147 | + break; |
| 148 | + case 'redirect-route': |
| 149 | + $route |
| 150 | + ->setDefault('_controller', RedirectController::class.'::redirectAction') |
| 151 | + ->setDefault('route', $node->getAttribute('redirect-to-route')) |
| 152 | + ->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')) |
| 153 | + ->setDefault('keepRequestMethod', self::getBooleanAttribute($node, 'keep-request-method')) |
| 154 | + ->setDefault('keepQueryParams', self::getBooleanAttribute($node, 'keep-query-params')) |
| 155 | + ; |
| 156 | + |
| 157 | + if (\is_string($ignoreAttributes = XmlUtils::phpize($node->getAttribute('ignore-attributes')))) { |
| 158 | + $ignoreAttributes = array_map('trim', explode(',', $ignoreAttributes)); |
| 159 | + } |
| 160 | + |
| 161 | + $route->setDefault('ignoreAttributes', $ignoreAttributes); |
| 162 | + break; |
| 163 | + case 'url-redirect-route': |
| 164 | + $route |
| 165 | + ->setDefault('_controller', RedirectController::class.'::urlRedirectAction') |
| 166 | + ->setDefault('path', $node->getAttribute('redirect-to-url')) |
| 167 | + ->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')) |
| 168 | + ->setDefault('scheme', $node->getAttribute('scheme')) |
| 169 | + ->setDefault('keepRequestMethod', self::getBooleanAttribute($node, 'keep-request-method')) |
| 170 | + ; |
| 171 | + if ($node->hasAttribute('http-port')) { |
| 172 | + $route->setDefault('httpPort', (int) $node->getAttribute('http-port') ?: null); |
| 173 | + } elseif ($node->hasAttribute('https-port')) { |
| 174 | + $route->setDefault('httpsPort', (int) $node->getAttribute('https-port') ?: null); |
| 175 | + } |
| 176 | + break; |
| 177 | + case 'gone-route': |
| 178 | + $route |
| 179 | + ->setDefault('_controller', RedirectController::class.'::redirectAction') |
| 180 | + ->setDefault('route', '') |
| 181 | + ; |
| 182 | + if ($node->hasAttribute('permanent')) { |
| 183 | + $route->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')); |
| 184 | + } |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private static function getRealSchemePath(string $schemePath): string |
| 190 | + { |
| 191 | + return 'file:///'.str_replace('\\', '/', realpath($schemePath)); |
| 192 | + } |
| 193 | + |
| 194 | + private static function getBooleanAttribute(\DOMElement $node, string $attribute): bool |
| 195 | + { |
| 196 | + return $node->hasAttribute($attribute) ? XmlUtils::phpize($node->getAttribute($attribute)) : false; |
| 197 | + } |
| 198 | +} |
0 commit comments