8000 [FrameworkBundle] allow turning routes to utf8 mode by default by nicolas-grekas · Pull Request #27774 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] allow turning routes to utf8 mode by defau 8000 lt #27774

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
Jul 9, 2018
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
8 changes: 8 additions & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ Form
{% endfor %}
```

FrameworkBundle
---------------

* The `framework.router.utf8` configuration option has been added. If your app's charset
is UTF-8 (see kernel's `getCharset()` method), it is recommended to set it to `true`:
this will generate 404s for non-UTF-8 URLs, which are incompatible with you app anyway,
and will allow dumping optimized routers and using Unicode classes in requirements.

Security
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
)
->defaultTrue()
->end()
->booleanNode('utf8')->defaultFalse()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co

$loader->load('routing.xml');

if ($config['utf8']) {
$container->getDefinition('routing.loader')->replaceArgument(2, array('utf8' => true));
}
if (!interface_exists(ContainerBagInterface::class)) {
$container->getDefinition('router.default')
->replaceArgument(0, new Reference('service_container'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
<argument type="service" id="controller_name_converter" />
<argument type="service" id="routing.resolver" />
<argument type="collection" />
</service>

<service id="router.default" class="Symfony\Bundle\FrameworkBundle\Routing\Router">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ class DelegatingLoader extends BaseDelegatingLoader
{
protected $parser;
private $loading = false;
private $defaultOptions;

/**
* @param ControllerNameParser $parser A ControllerNameParser instance
* @param LoaderResolverInterface $resolver A LoaderResolverInterface instance
*/
public function __construct(ControllerNameParser $parser, LoaderResolverInterface $resolver)
public function __construct(ControllerNameParser $parser, LoaderResolverInterface $resolver, array $defaultOptions = array())
{
$this->parser = $parser;
$this->defaultOptions = $defaultOptions;

parent::__construct($resolver);
}
Expand Down Expand Up @@ -73,6 +75,9 @@ public function load($resource, $type = null)
}

foreach ($collection->all() as $route) {
if ($this->defaultOptions) {
$route->setOptions($route->getOptions() + $this->defaultOptions);
}
if (!is_string($controller = $route->getDefault('_controller'))) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ protected static function getBundleDefaultConfig()
'http_port' => 80,
'https_port' => 443,
'strict_requirements' => true,
'utf8' => false,
),
'session' => array(
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,49 @@ public function testConstructorApi()
$this->assertTrue(true, '__construct() takes a ControllerNameParser and LoaderResolverInterface respectively as its first and second argument.');
}

public function testLoadDefaultOptions()
{
$controllerNameParser = $this->getMockBuilder(ControllerNameParser::class)
->disableOriginalConstructor()
->getMock();

$loaderResolver = $this->getMockBuilder(LoaderResolverInterface::class)
->disableOriginalConstructor()
->getMock();

$loader = $this->getMockBuilder(LoaderInterface::class)->getMock();

$loaderResolver->expects($this->once())
->method('resolve')
->willReturn($loader);

$routeCollection = new RouteCollection();
$routeCollection->add('foo', new Route('/', array(), array(), array('utf8' => false)));
$routeCollection->add('bar', new Route('/', array(), array(), array('foo' => 123)));

$loader->expects($this->once())
->method('load')
->willReturn($routeCollection);

$delegatingLoader = new DelegatingLoader($controllerNameParser, $loaderResolver, array('utf8' => true));

$loadedRouteCollection = $delegatingLoader->load('foo');
$this->assertCount(2, $loadedRouteCollection);

$expected = array(
'compiler_class' => 'Symfony\Component\Routing\RouteCompiler',
'utf8' => false,
);
$this->assertSame($expected, $routeCollection->get('foo')->getOptions());

$expected = array(
'compiler_class' => 'Symfony\Component\Routing\RouteCompiler',
'foo' => 123,
'utf8' => true,
);
$this->assertSame($expected, $routeCollection->get('bar')->getOptions());
}

/**
* @group legacy
* @expectedDeprecation Referencing controllers with foo:bar:baz is deprecated since Symfony 4.1, use "some_parsed::controller" instead.
Expand Down
0