Skip to content

Commit

Permalink
feature #30508 [Routing] Exposed "utf8" option, defaults "locale" and…
Browse files Browse the repository at this point in the history
… "format" in configuration (Jules Pietri)

This PR was merged into the 4.3-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | symfony/symfony-docs#11126

A sibling to #30501, everything is in the title :).

Commits
-------

2911490 [Routing] Exposed "utf8" option, defaults "locale" and "format" in configuration
  • Loading branch information
fabpot committed Mar 20, 2019
2 parents 1479a26 + 2911490 commit fc826aa
Show file tree
Hide file tree
Showing 30 changed files with 529 additions and 13 deletions.
18 changes: 18 additions & 0 deletions 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 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 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 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 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 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 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 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

0 comments on commit fc826aa

Please sign in to comment.