Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ CHANGELOG

* Added link to source for controllers registered as named services
* Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
* Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers
* Added XML and YAML routing loaders to ease defining routes with redirect and template controllers
* Added the `Routing\Loader\Configurator` namespace to ease defining routes with redirect and template controllers
* Added the `framework.router.context` configuration node to configure the `RequestContext`
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

<service id="routing.resolver" class="Symfony\Component\Config\Loader\LoaderResolver" />

<service id="routing.loader.xml" class="Symfony\Component\Routing\Loader\XmlFileLoader">
<service id="routing.loader.xml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\XmlFileLoader">
<tag name="routing.loader" />
<argument type="service" id="file_locator" />
</service>

<service id="routing.loader.yml" class="Symfony\Component\Routing\Loader\YamlFileLoader">
<service id="routing.loader.yml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\YamlFileLoader">
<tag name="routing.loader" />
<argument type="service" id="file_locator" />
</service>
Expand Down
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>
Copy link
Member

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?

Copy link
Contributor Author

@HeahDude HeahDude Feb 25, 2020

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 the controller. 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?


<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
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;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.