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

Commit ee4a389

Browse filesBrowse files
committed
[FrameworkBundle][Routing] added XML and YAML loaders to handle template and redirect controllers
1 parent f46ab58 commit ee4a389
Copy full SHA for ee4a389

File tree

10 files changed

+581
-5
lines changed
Filter options

10 files changed

+581
-5
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

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

18-
<service id="routing.loader.xml" class="Symfony\Component\Routing\Loader\XmlFileLoader">
18+
<service id="routing.loader.xml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\XmlFileLoader">
1919
<tag name="routing.loader" />
2020
<argument type="service" id="file_locator" />
2121
</service>
2222

23-
<service id="routing.loader.yml" class="Symfony\Component\Routing\Loader\YamlFileLoader">
23+
<service id="routing.loader.yml" class="Symfony\Bundle\FrameworkBundle\Routing\Loader\YamlFileLoader">
2424
<tag name="routing.loader" />
2525
<argument type="service" id="file_locator" />
2626
</service>
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<xsd:schema xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
5+
targetNamespace="http://symfony.com/schema/routing"
6+
elementFormDefault="qualified">
7+
8+
<xsd:redefine schemaLocation="https://symfony.com/schema/routing/routing-1.0.xsd">
9+
<xsd:complexType name="routes">
10+
<xsd:complexContent>
11+
<xsd:extension base="routes">
12+
<xsd:choice minOccurs="0" maxOccurs="unbounded">
13+
<xsd:element name="template-route" type="template-route" />
14+
<xsd:element name="redirect-route" type="redirect-route" />
15+
<xsd:element name="url-redirect-route" type="url-redirect-route" />
16+
<xsd:element name="gone-route" type="gone-route" />
17+
</xsd:choice>
18+
</xsd:extension>
19+
</xsd:complexContent>
20+
</xsd:complexType>
21+
</xsd:redefine>
22+
23+
<xsd:group name="base-configs">
24+
<xsd:choice>
25+
<xsd:element name="path" type="localized-path" />
26+
<xsd:element name="requirement" type="element" />
27+
<xsd:element name="option" type="element" />
28+
<xsd:element name="condition" type="xsd:string" />
29+
</xsd:choice>
30+
</xsd:group>
31+
32+
<xsd:complexType name="base-route">
33+
<xsd:attribute name="id" type="xsd:string" use="required" />
34+
<xsd:attribute name="path" type="xsd:string" />
35+
<xsd:attribute name="host" type="xsd:string" />
36+
<xsd:attribute name="methods" type="xsd:string" />
37+
<xsd:attribute name="locale" type="xsd:string" />
38+
<xsd:attribute name="format" type="xsd:string" />
39+
<xsd:attribute name="utf8" type="xsd:boolean" />
40+
</xsd:complexType>
41+
42+
<xsd:complexType name="template-route">
43+
<xsd:complexContent>
44+
<xsd:extension base="base-route">
45+
<xsd:sequence>
46+
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" />
47+
<xsd:element name="context" type="map" minOccurs="0" maxOccurs="1" />
48+
</xsd:sequence>
49+
<xsd:attribute name="template" type="xsd:string" />
50+
<xsd:attribute name="max-age" type="xsd:int" />
51+
<xsd:attribute name="shared-max-age" type="xsd:int" />
52+
<xsd:attribute name="private" type="xsd:boolean" />
53+
<xsd:attribute name="schemes" type="xsd:string" />
54+
</xsd:extension>
55+
</xsd:complexContent>
56+
</xsd:complexType>
57+
58+
<xsd:complexType name="redirect-route">
59+
<xsd:complexContent>
60+
<xsd:extension base="base-route">
61+
<xsd:sequence>
62+
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" />
63+
</xsd:sequence>
64+
<xsd:attribute name="redirect-to-route" type="xsd:string" />
65+
<xsd:attribute name="permanent" type="xsd:boolean" />
66+
<xsd:attribute name="ignore-attributes" type="xsd:string" />
67+
<xsd:attribute name="keep-request-method" type="xsd:boolean" />
68+
<xsd:attribute name="keep-query-params" type="xsd:boolean" />
69+
<xsd:attribute name="schemes" type="xsd:string" />
70+
</xsd:extension>
71+
</xsd:complexContent>
72+
</xsd:complexType>
73+
74+
<xsd:complexType name="url-redirect-route">
75+
<xsd:complexContent>
76+
<xsd:extension base="base-route">
77+
<xsd:sequence>
78+
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" />
79+
</xsd:sequence>
80+
<xsd:attribute name="redirect-to-url" type="xsd:string" />
81+
<xsd:attribute name="permanent" type="xsd:boolean" />
82+
<xsd:attribute name="scheme" type="xsd:string" />
83+
<xsd:attribute name="http-port" type="xsd:int" />
84+
<xsd:attribute name="https-port" type="xsd:int" />
85+
<xsd:attribute name="keep-request-method" type="xsd:boolean" />
86+
</xsd:extension>
87+
</xsd:complexContent>
88+
</xsd:complexType>
89+
90+
<xsd:complexType name="gone-route">
91+
<xsd:complexContent>
92+
<xsd:extension base="base-route">
93+
<xsd:sequence>
94+
<xsd:group ref="base-configs" minOccurs="0" maxOccurs="unbounded" />
95+
</xsd:sequence>
96+
<xsd:attribute name="permanent" type="xsd:boolean" />
97+
</xsd:extension>
98+
</xsd:complexContent>
99+
</xsd:complexType>
100+
101+
</xsd:schema>
+186Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.