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