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

[Routing] Exposed "utf8" option, defaults "locale" and "format" in configuration #30508

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

Merged
merged 1 commit into from
Mar 20, 2019
Merged
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
18 changes: 18 additions & 0 deletions 18 src/Symfony/Component/Routing/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Route
private $methods = [];
private $schemes = [];
private $condition;
private $locale;
private $format;
private $utf8;

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

if (isset($data['locale'])) {
$data['defaults']['_locale'] = $data['locale'];
unset($data['locale']);
}

if (isset($data['format'])) {
$data['defaults']['_format'] = $data['format'];
unset($data['format']);
}

if (isset($data['utf8'])) {
$data['options']['utf8'] = filter_var($data['utf8'], FILTER_VALIDATE_BOOLEAN) ?: false;
unset($data['utf8']);
}

foreach ($data as $key => $value) {
$method = 'set'.str_replace('_', '', $key);
if (!method_exists($this, $method)) {
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* deprecated `generator_base_class`, `generator_cache_class`, `matcher_base_class` and `matcher_cache_class` router options
* deprecated implementing `Serializable` for `Route` and `CompiledRoute`; if you serialize them, please
ensure your unserialization logic can recover from a failure related to an updated serialization format
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ final public function options(array $options)
return $this;
}

/**
* Whether paths should accept utf8 encoding.
*
* @return $this
*/
final public function utf8(bool $utf8 = true)
{
$this->route->addOptions(['utf8' => $utf8]);

return $this;
}

/**
* Sets the condition.
*
Expand Down Expand Up @@ -124,4 +136,28 @@ final public function controller($controller)

return $this;
}

/**
* Adds the "_locale" entry to defaults.
*
* @return $this
*/
final public function locale(string $locale)
{
$this->route->addDefaults(['_locale' => $locale]);

return $this;
}

/**
* Adds the "_format" entry to defaults.
*
* @return $this
*/
final public function format(string $format)
{
$this->route->addDefaults(['_format' => $format]);

return $this;
}
}
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $

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

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

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

$defaults['_controller'] = $controller;
}
if ($node->hasAttribute('locale')) {
$defaults['_locale'] = $node->getAttribute('locale');
}
if ($node->hasAttribute('format')) {
$defaults['_format'] = $node->getAttribute('format');
}
if ($node->hasAttribute('utf8')) {
$options['utf8'] = XmlUtils::phpize($node->getAttribute('utf8'));
}

return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
}
Expand Down
20 changes: 19 additions & 1 deletion 20 src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class YamlFileLoader extends FileLoader
{
private static $availableKeys = [
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8',
];
private $yamlParser;

Expand Down Expand Up @@ -125,6 +125,15 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}
if (isset($config['locale'])) {
$defaults['_locale'] = $config['locale'];
}
if (isset($config['format'])) {
$defaults['_format'] = $config['format'];
}
if (isset($config['utf8'])) {
$options['utf8'] = $config['utf8'];
}

if (\is_array($config['path'])) {
$route = new Route('', $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
Expand Down Expand Up @@ -166,6 +175,15 @@ protected function parseImport(RouteCollection $collection, array $config, $path
if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}
if (isset($config['locale'])) {
$defaults['_locale'] = $config['locale'];
}
if (isset($config['format'])) {
$defaults['_format'] = $config['format'];
}
if (isset($config['utf8'])) {
$options['utf8'] = $config['utf8'];
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" 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="import">
Expand All @@ -67,7 +70,10 @@
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" type="xsd:string" />
<xsd:attribute name="locale" type="xsd:string" />
<xsd:attribute name="format" type="xsd:string" />
<xsd:attribute name="trailing-slash-on-root" type="xsd:boolean" />
<xsd:attribute name="utf8" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="default" mixed="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Component\Routing\Tests\Fixtures\AnnotationFixtures;

use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/defaults", locale="g_locale", format="g_format")
*/
class GlobalDefaultsClass
{
/**
* @Route("/specific-locale", name="specific_locale", locale="s_locale")
*/
public function locale()
{
}

/**
* @Route("/specific-format", name="specific_format", format="s_format")
*/
public function format()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;

use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/test", utf8=true)
*/
class Utf8ActionControllers
{
/**
* @Route(name="one")
*/
public function one()
{
}

/**
* @Route(name="two", utf8=false)
*/
public function two()
{
}
}
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Routing/Tests/Fixtures/defaults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->add('defaults', '/defaults')
->locale('en')
->format('html')
;
};
8 changes: 8 additions & 0 deletions 8 src/Symfony/Component/Routing/Tests/Fixtures/defaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<route id="defaults" path="/defaults" locale="en" format="html" />
</routes>
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Routing/Tests/Fixtures/defaults.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaults:
path: /defaults
locale: en
format: html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes
->add('one', '/one')
->add('two', '/two')->defaults(['specific' => 'imported'])
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<route id="one" path="/one" />
<route id="two" path="/two">
<default key="specific">imported</default>
</route>
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
one:
path: /one

two:
path: /two
defaults:
specific: imported
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->import('imported-with-defaults.php')
->prefix('/defaults')
->locale('g_locale')
->format('g_format')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">

<import resource="imported-with-defaults.xml" prefix="/defaults"
locale="g_locale"
format="g_format" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defaults:
resource: imported-with-defaults.yml
prefix: /defaults
locale: g_locale
format: g_format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes
->add('utf8_one', '/one')
->add('utf8_two', '/two')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing">

<route id="utf8_one" path="/one" />
<route id="utf8_two" path="/two" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
utf8_one:
path: /one

utf8_two:
path: /two
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->import('imported-with-utf8.php')->utf8();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
https://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="imported-with-utf8.xml" utf8="true" />
</routes>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
utf8_routes:
resource: imported-with-utf8.yml
utf8: true
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Routing/Tests/Fixtures/localized/utf8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes
->add('some_route', '/')
->add('some_utf8_route', '/utf8')->utf8()
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
some_route:
path: /

some_utf8_route:
path: /utf8
utf8: true
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.