-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Routing] added XML and YAML loaders to handle template and redirect controllers #35653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[FrameworkBundle][Routing] added XML and YAML loaders to handle template and redirect controllers #35653
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/framework-routing-1.0.xsd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<xsd:schema xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | ||
targetNamespace="http://symfony.com/schema/routing" | ||
elementFormDefault="qualified"> | ||
|
||
<xsd:redefine schemaLocation="https://symfony.com/schema/routing/routing-1.0.xsd"> | ||
<xsd:complexType name="routes"> | ||
<xsd:complexContent> | ||
<xsd:extension base="routes"> | ||
<xsd:choice minOccurs="0" maxOccurs="unbounded"> | ||
<xsd:element name="template-route" type="template-route" /> | ||
<xsd:element name="redirect-route" type="redirect-route" /> | ||
<xsd:element name="url-redirect-route" type="url-redirect-route" /> | ||
<xsd:element name="gone-route" type="gone-route" /> | ||
</xsd:choice> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
</xsd:redefine> | ||
|
||
<xsd:group name="base-configs"> | ||
<xsd:choice> | ||
<xsd:element name="path" type="localized-path" /> | ||
<xsd:element name="requirement" type="element" /> | ||
<xsd:element name="option" type="element" /> | ||
<xsd:element name="condition" type="xsd:string" /> | ||
</xsd:choice> | ||
</xsd:group> | ||
|
||
<xsd:complexType name="base-route" abstract="true"> | ||
<xsd:attribute name="id" type="xsd:string" use="required" /> | ||
<xsd:attribute name="path" type="xsd:string" /> | ||
<xsd:attribute name="host" type="xsd:string" /> | ||
<xsd:attribute name="methods" type="xsd:string" /> | ||
<xsd:attribute name="locale" type="xsd:string" /> | ||
<xsd:attribute name="format" type="xsd:string" /> | ||
<xsd:attribute name="utf8" type="xsd:boolean" /> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="template-route"> | ||
<xsd:complexContent> | ||
<xsd:extension base="base-route"> | ||
<xsd:sequence> | ||
<xsd:element name="context" type="map" minOccurs="0" maxOccurs="1" /> | ||
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="template" type="xsd:string" use="required" /> | ||
<xsd:attribute name="max-age" type="xsd:int" /> | ||
<xsd:attribute name="shared-max-age" type="xsd:int" /> | ||
<xsd:attribute name="private" type="xsd:boolean" /> | ||
<xsd:attribute name="schemes" type="xsd:string" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="redirect-route"> | ||
<xsd:complexContent> | ||
<xsd:extension base="base-route"> | ||
<xsd:sequence> | ||
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="redirect-to-route" type="xsd:string" use="required"/> | ||
<xsd:attribute name="permanent" type="xsd:boolean" /> | ||
<xsd:attribute name="ignore-attributes" type="xsd:string" /> | ||
<xsd:attribute name="keep-request-method" type="xsd:boolean" /> | ||
<xsd:attribute name="keep-query-params" type="xsd:boolean" /> | ||
<xsd:attribute name="schemes" type="xsd:string" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="url-redirect-route"> | ||
<xsd:complexContent> | ||
<xsd:extension base="base-route"> | ||
<xsd:sequence> | ||
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="redirect-to-url" type="xsd:string" use="required" /> | ||
<xsd:attribute name="permanent" type="xsd:boolean" /> | ||
<xsd:attribute name="scheme" type="xsd:string" /> | ||
<xsd:attribute name="http-port" type="xsd:int" /> | ||
<xsd:attribute name="https-port" type="xsd:int" /> | ||
<xsd:attribute name="keep-request-method" type="xsd:boolean" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
|
||
<xsd:complexType name="gone-route"> | ||
<xsd:complexContent> | ||
<xsd:extension base="base-route"> | ||
<xsd:sequence> | ||
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" /> | ||
</xsd:sequence> | ||
<xsd:attribute name="permanent" type="xsd:boolean" /> | ||
</xsd:extension> | ||
</xsd:complexContent> | ||
</xsd:complexType> | ||
|
||
</xsd:schema> |
198 changes: 198 additions & 0 deletions
198
src/Symfony/Bundle/FrameworkBundle/Routing/Loader/XmlFileLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Routing\Loader; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; | ||
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; | ||
use Symfony\Component\Config\Util\Exception\InvalidXmlException; | ||
use Symfony\Component\Config\Util\Exception\XmlParsingException; | ||
use Symfony\Component\Config\Util\XmlUtils; | ||
use Symfony\Component\Routing\Loader\XmlFileLoader as BaseXmlFileLoader; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* @author Jules Pietri <jules@heahprod.com> | ||
*/ | ||
class XmlFileLoader extends BaseXmlFileLoader | ||
{ | ||
public const SCHEME_PATH = __DIR__.'/../../Resources/config/schema/framework-routing-1.0.xsd'; | ||
|
||
private const REDEFINED_SCHEME_URI = 'https://symfony.com/schema/routing/routing-1.0.xsd'; | ||
private const SCHEME_URI = 'https://symfony.com/schema/routing/framework-routing-1.0.xsd'; | ||
private const SCHEMA_LOCATIONS = [ | ||
self::REDEFINED_SCHEME_URI => parent::SCHEME_PATH, | ||
self::SCHEME_URI => self::SCHEME_PATH, | ||
]; | ||
|
||
/** @var \DOMDocument */ | ||
private $document; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function loadFile(string $file) | ||
{ | ||
if ('' === trim($content = @file_get_contents($file))) { | ||
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid XML, it is empty.', $file)); | ||
} | ||
|
||
foreach (self::SCHEMA_LOCATIONS as $uri => $path) { | ||
if (false !== strpos($content, $uri)) { | ||
$content = str_replace($uri, self::getRealSchemePath($path), $content); | ||
} | ||
} | ||
|
||
try { | ||
return $this->document = XmlUtils::parse($content, function (\DOMDocument $document) { | ||
return @$document->schemaValidateSource(str_replace( | ||
self::REDEFINED_SCHEME_URI, | ||
self::getRealSchemePath(parent::SCHEME_PATH), | ||
file_get_contents(self::SCHEME_PATH) | ||
)); | ||
}); | ||
} catch (InvalidXmlException $e) { | ||
throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious()); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) | ||
{ | ||
switch ($node->localName) { | ||
case 'template-route': | ||
case 'redirect-route': | ||
case 'url-redirect-route': | ||
case 'gone-route': | ||
if (self::NAMESPACE_URI !== $node->namespaceURI) { | ||
return; | ||
} | ||
|
||
$this->parseRoute($collection, $node, $path); | ||
|
||
return; | ||
} | ||
|
||
parent::parseNode($collection, $node, $path, $file); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path) | ||
{ | ||
$templateContext = []; | ||
|
||
if ('template-route' === $node->localName) { | ||
/** @var \DOMElement $context */ | ||
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, 'context') as $context) { | ||
$node->removeChild($context); | ||
$map = $this->document->createElementNS(self::NAMESPACE_URI, 'map'); | ||
|
||
// extract context vars into a map | ||
foreach ($context->childNodes as $n) { | ||
if (!$n instanceof \DOMElement) { | ||
continue; | ||
} | ||
|
||
$map->appendChild($n); | ||
} | ||
|
||
$default = $this->document->createElementNS(self::NAMESPACE_URI, 'default'); | ||
$default->setAttribute('key', 'context'); | ||
$default->appendChild($map); | ||
|
||
$templateContext = $this->parseDefaultsConfig($default, $path); | ||
} | ||
} | ||
|
||
parent::parseRoute($collection, $node, $path); | ||
|
||
if ($route = $collection->get($id = $node->getAttribute(('id')))) { | ||
$this->parseConfig($node, $route, $templateContext); | ||
|
||
return; | ||
} | ||
|
||
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, 'path') as $n) { | ||
$route = $collection->get($id.'.'.$n->getAttribute('locale')); | ||
|
||
$this->parseConfig($node, $route, $templateContext); | ||
} | ||
} | ||
|
||
private function parseConfig(\DOMElement $node, Route $route, array $templateContext): void | ||
{ | ||
switch ($node->localName) { | ||
case 'template-route': | ||
$route | ||
->setDefault('_controller', TemplateController::class) | ||
->setDefault('template', $node->getAttribute('template')) | ||
->setDefault('context', $templateContext) | ||
->setDefault('maxAge', (int) $node->getAttribute('max-age') ?: null) | ||
->setDefault('sharedAge', (int) $node->getAttribute('shared-max-age') ?: null) | ||
->setDefault('private', $node->hasAttribute('private') ? XmlUtils::phpize($node->getAttribute('private')) : null) | ||
; | ||
break; | ||
case 'redirect-route': | ||
$route | ||
->setDefault('_controller', RedirectController::class.'::redirectAction') | ||
->setDefault('route', $node->getAttribute('redirect-to-route')) | ||
->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')) | ||
->setDefault('keepRequestMethod', self::getBooleanAttribute($node, 'keep-request-method')) | ||
->setDefault('keepQueryParams', self::getBooleanAttribute($node, 'keep-query-params')) | ||
; | ||
|
||
if (\is_string($ignoreAttributes = XmlUtils::phpize($node->getAttribute('ignore-attributes')))) { | ||
$ignoreAttributes = array_map('trim', explode(',', $ignoreAttributes)); | ||
} | ||
|
||
$route->setDefault('ignoreAttributes', $ignoreAttributes); | ||
break; | ||
case 'url-redirect-route': | ||
$route | ||
->setDefault('_controller', RedirectController::class.'::urlRedirectAction') | ||
->setDefault('path', $node->getAttribute('redirect-to-url')) | ||
->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')) | ||
->setDefault('scheme', $node->getAttribute('scheme')) | ||
->setDefault('keepRequestMethod', self::getBooleanAttribute($node, 'keep-request-method')) | ||
; | ||
if ($node->hasAttribute('http-port')) { | ||
$route->setDefault('httpPort', (int) $node->getAttribute('http-port') ?: null); | ||
} elseif ($node->hasAttribute('https-port')) { | ||
$route->setDefault('httpsPort', (int) $node->getAttribute('https-port') ?: null); | ||
} | ||
break; | ||
case 'gone-route': | ||
$route | ||
->setDefault('_controller', RedirectController::class.'::redirectAction') | ||
->setDefault('route', '') | ||
; | ||
if ($node->hasAttribute('permanent')) { | ||
$route->setDefault('permanent', self::getBooleanAttribute($node, 'permanent')); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
private static function getRealSchemePath(string $schemePath): string | ||
{ | ||
return 'file:///'.str_replace('\\', '/', realpath($schemePath)); | ||
} | ||
|
||
private static function getBooleanAttribute(\DOMElement $node, string $attribute): bool | ||
{ | ||
return $node->hasAttribute($attribute) ? XmlUtils::phpize($node->getAttribute($attribute)) : false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand XSD - and how this relates to the original XSD file. But... do we need
methods
here?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is defining a group of base
elements
for the new route types, basically everything same as a classic route but without thecontroller
. There should be no reason to remove support for one one them here IMHO as we do it consistently in other formats too (YAML and PHP-DSL), and also because it may cause some BC issues?