8000 [Routing] Exposed "utf8" option, defaults "locale" and "format" in co… · symfony/symfony@e1dfa68 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1dfa68

Browse files
Jules PietriHeahDude
Jules Pietri
authored andcommitted
[Routing] Exposed "utf8" option, defaults "locale" and "format" in configuration
1 parent 401c1d3 commit e1dfa68

29 files changed

+528
-12
lines changed

src/Symfony/Component/Routing/Annotation/Route.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class Route
3131
private $methods = [];
3232
private $schemes = [];
3333
private $condition;
34+
private $locale;
35+
private $format;
36+
private $utf8;
3437

3538
/**
3639
* @param array $data An array of key/value parameters
@@ -53,6 +56,21 @@ public function __construct(array $data)
5356
unset($data['path']);
5457
}
5558

59+
if (isset($data['locale'])) {
60+
$data['defaults']['_locale'] = $data['locale'];
61+
unset($data['locale']);
62+
}
63+
64+
if (isset($data['format'])) {
65+
$data['defaults']['_format'] = $data['format'];
66+
unset($data['format']);
67+
}
68+
69+
if (isset($data['utf8'])) {
70+
$data['options']['utf8'] = filter_var($data['utf8'], FILTER_VALIDATE_BOOLEAN) ?: false;
71+
unset($data['utf8']);
72+
}
73+
5674
foreach ($data as $key => $value) {
5775
$method = 'set'.str_replace('_', '', $key);
5876
if (!method_exists($this, $method)) {

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* deprecated `generator_base_class`, `generator_cache_class`, `matcher_base_class` and `matcher_cache_class` router options
1111
* deprecated implementing `Serializable` for `Route` and `CompiledRoute`; if you serialize them, please
1212
ensure your unserialization logic can recover from a failure related to an updated serialization format
13+
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators
1314

1415
4.2.0
1516
-----

src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ final public function options(array $options)
5757
return $this;
5858
}
5959

60+
/**
61+
* Whether paths should accept utf8 encoding.
62+
*
63+
* @return $this
64+
*/
65+
final public function utf8(bool $utf8 = true)
66+
{
67+
$this->route->addOptions(['utf8' => $utf8]);
68+
69+
return $this;
70+
}
71+
6072
/**
6173
* Sets the condition.
6274
*
@@ -124,4 +136,28 @@ final public function controller($controller)
124136

125137
return $this;
126138
}
139+
140+
/**
141+
* Adds the "_locale" entry to defaults.
142+
*
143+
* @return $this
144+
*/
145+
final public function locale(string $locale)
146+
{
147+
$this->route->addDefaults(['_locale' => $locale]);
148+
149+
return $this;
150+
}
151+
152+
/**
153+
* Adds the "_format" entry to defaults.
154+
*
155+
* @return $this
156+
*/
157+
final public function format(string $format)
158+
{
159+
$this->route->addDefaults(['_format' => $format]);
160+
161+
return $this;
162+
}
127163
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
168168

169169
$this->setCurrentDir(\dirname($path));
170170

171+
/** @var RouteCollection[] $imported */
171172
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file);
172173

173174
if (!\is_array($imported)) {
@@ -312,6 +313,15 @@ private function parseConfigs(\DOMElement $node, $path)
312313

313314
$defaults['_controller'] = $controller;
314315
}
316+
if ($node->hasAttribute('locale')) {
317+
$defaults['_locale'] = $node->getAttribute('locale');
318+
}
319+
if ($node->hasAttribute('format')) {
320+
$defaults['_format'] = $node->getAttribute('format');
321+
}
322+
if ($node->hasAttribute('utf8')) {
323+
$options['utf8'] = XmlUtils::phpize($node->getAttribute('utf8'));
324+
}
315325

316326
return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
317327
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class YamlFileLoader extends FileLoader
2929
{
3030
private static $availableKeys = [
31-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root',
31+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8',
3232
];
3333
private $yamlParser;
3434

@@ -125,6 +125,15 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
125125
if (isset($config['controller'])) {
126126
$defaults['_controller'] = $config['controller'];
127127
}
128+
if (isset($config['locale'])) {
129+
$defaults['_locale'] = $config['locale'];
130+
}
131+
if (isset($config['format'])) {
132+
$defaults['_format'] = $config['format'];
133+
}
134+
if (isset($config['utf8'])) {
135+
$options['utf8'] = $config['utf8'];
136+
}
128137

129138
if (\is_array($config['path'])) {
130139
$route = new Route('', $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
@@ -166,6 +175,15 @@ protected function parseImport(RouteCollection $collection, array $config, $path
166175
if (isset($config['controller'])) {
167176
$defaults['_controller'] = $config['controller'];
168177
}
178+
if (isset($config['locale'])) {
179+
$defaults['_locale'] = $config['locale'];
180+
}
181+
if (isset($config['format'])) {
182+
$defaults['_format'] = $config['format'];
183+
}
184+
if (isset($config['utf8'])) {
185+
$options['utf8'] = $config['utf8'];
186+
}
169187

170188
$this->setCurrentDir(\dirname($path));
171189

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
<xsd:attribute name="schemes" type="xsd:string" />
5353
<xsd:attribute name="methods" type="xsd:string" />
5454
<xsd:attribute name="controller" type="xsd:string" />
55+
<xsd:attribute name="locale" type="xsd:string" />
56+
<xsd:attribute name="format" type="xsd:string" />
57+
<xsd:attribute name="utf8" type="xsd:boolean" />
5558
</xsd:complexType>
5659

5760
<xsd:complexType name="import">
@@ -67,7 +70,10 @@
6770
<xsd:attribute name="schemes" type="xsd:string" />
6871
<xsd:attribute name="methods" type="xsd:string" />
6972
<xsd:attribute name="controller" type="xsd:string" />
73+
<xsd:attribute name="locale" type="xsd:string" />
74+
<xsd:attribute name="format" type="xsd:string" />
7075
<xsd:attribute name="trailing-slash-on-root" type="xsd:boolean" />
76+
<xsd:attribute name="utf8" type="xsd:boolean" />
7177
</xsd:complexType>
7278

7379
<xsd:complexType name="default" mixed="true">
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Component\Routing\Tests\Fixtures\AnnotationFixtures;
13+
14+
use Symfony\Component\Routing\Annotation\Route;
15+
16+
/**
17+
* @Route("/defaults", locale="g_locale", format="g_format")
18+
*/
19+
class GlobalDefaultsClass
20+
{
21+
/**
22+
* @Route("/specific-locale", name="specific_locale", locale="s_locale")
23+
*/
24+
public function locale()
25+
{
26+
}
27+
28+
/**
29+
* @Route("/specific-format", name="specific_format", format="s_format")
30+
*/
31+
public function format()
32+
{
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
/**
8+
* @Route("/test", utf8=true)
9+
*/
10+
class Utf8ActionControllers
11+
{
12+
/**
13+
* @Route(name="one")
14+
*/
15+
public function one()
16+
{
17+
}
18+
19+
/**
20+
* @Route(name="two", utf8=false)
21+
*/
22+
public function two()
23+
{
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes->add('defaults', '/defaults')
7+
->locale('en')
8+
->format('html')
9+
;
10+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
https://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="defaults" path="/defaults" locale="en" format="html" />
8+
</routes>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defaults:
2+
path: /defaults
3+
locale: en
4+
format: html
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes
7+
->add('one', '/one')
8+
->add('two', '/two')->defaults(['specific' => 'imported'])
9+
;
10+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
https://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="one" path="/one" />
8+
<route id="two" path="/two">
9+
<default key="specific">imported</default>
10+
</route>
11+
</routes>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
one:
2+
path: /one
3+
4+
two:
5+
path: /two
6+
defaults:
7+
specific: imported
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes->import('imported-with-defaults.php')
7+
->prefix('/defaults')
8+
->locale('g_locale')
9+
->format('g_format')
10+
;
11+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
https://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<import resource="imported-with-defaults.xml" prefix="/defaults"
8+
locale="g_locale"
9+
format="g_format" />
10+
</routes>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defaults:
2+
resource: imported-with-defaults.yml
3+
prefix: /defaults
4+
locale: g_locale
5+
format: g_format
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes
7+
->add('utf8_one', '/one')
8+
->add('utf8_two', '/two')
9+
;
10+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing">
5+
6+
<route id="utf8_one" path="/one" />
7+
<route id="utf8_two" path="/two" />
8+
</routes>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
utf8_one:
2+
path: /one
3+
4+
utf8_two:
5+
path: /two
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes->import('imported-with-utf8.php')->utf8();
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing
5+
https://symfony.com/schema/routing/routing-1.0.xsd">
6+
<import resource="imported-with-utf8.xml" utf8="true" />
7+
</routes>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
utf8_routes:
2+
resource: imported-with-utf8.yml
3+
utf8: true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$routes
7+
->add('some_route', '/')
8+
->add('some_utf8_route', '/utf8')->utf8()
9+
;
10+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
some_route:
2+
path: /
3+
4+
some_utf8_route:
5+
path: /utf8
6+
utf8: true

0 commit comments

Comments
 (0)
0