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 3cf520a

Browse filesBrowse files
author
Jules Pietri
committed
[Routing] Exposed "compiler_class" and "utf8" options in configuration
1 parent 4619ae4 commit 3cf520a
Copy full SHA for 3cf520a

18 files changed

+296
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Annotation/Route.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public function __construct(array $data)
5353
unset($data['path']);
5454
}
5555

56+
if (isset($data['compilerClass'])) {
57+
$data['options']['compiler_class'] = $data['compilerClass'];
58+
unset($data['compilerClass']);
59+
}
60+
61+
if (isset($data['utf8'])) {
62+
$data['options']['utf8'] = filter_var($data['utf8'], FILTER_VALIDATE_BOOLEAN) ?: false;
63+
unset($data['utf8']);
64+
}
65+
5666
foreach ($data as $key => $value) {
5767
$method = 'set'.str_replace('_', '', $key);
5868
if (!method_exists($this, $method)) {

‎src/Symfony/Component/Routing/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/CHANGELOG.md
+1Lines 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 `compiler_class` and `utf8` Route options in configuration loaders and configurators
1314

1415
4.2.0
1516
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ final public function options(array $options)
5757
return $this;
5858
}
5959

60+
/**
61+
* Defines the compiler class used to compile the Route.
62+
*
63+
* @return $this
64+
*/
65+
final public function compilerClass(string $class)
66+
{
67+
$this->route->addOptions(['compiler_class' => $class]);
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Whether paths should accept utf8 encoding.
74+
*
75+
* @return $this
76+
*/
77+
final public function utf8(bool $utf8 = true)
78+
{
79+
$this->route->addOptions(['utf8' => $utf8]);
80+
81+
return $this;
82+
}
83+
6084
/**
6185
* Sets the condition.
6286
*

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/XmlFileLoader.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ private function parseConfigs(\DOMElement $node, $path)
311311

312312
$defaults['_controller'] = $controller;
313313
}
314+
if ($node->hasAttribute('compiler-class')) {
315+
$options['compiler_class'] = trim($node->getAttribute('compiler-class'));
316+
}
317+
if ($node->hasAttribute('utf8')) {
318+
$options['utf8'] = XmlUtils::phpize($node->getAttribute('utf8'));
319+
}
314320

315321
return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
316322
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+13-1Lines changed: 13 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', 'compiler_class', 'utf8',
3232
];
3333
private $yamlParser;
3434

@@ -125,6 +125,12 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
125125
if (isset($config['controller'])) {
126126
$defaults['_controller'] = $config['controller'];
127127
}
128+
if (isset($config['compiler_class'])) {
129+
$options['compiler_class'] = $config['compiler_class'];
130+
}
131+
if (isset($config['utf8'])) {
132+
$options['utf8'] = $config['utf8'];
133+
}
128134

129135
if (\is_array($config['path'])) {
130136
$route = new Route('', $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
@@ -166,6 +172,12 @@ protected function parseImport(RouteCollection $collection, array $config, $path
166172
if (isset($config['controller'])) {
167173
$defaults['_controller'] = $config['controller'];
168174
}
175+
if (isset($config['compiler_class'])) {
176+
$options['compiler_class'] = $config['compiler_class'];
177+
}
178+
if (isset($config['utf8'])) {
179+
$options['utf8'] = $config['utf8'];
180+
}
169181

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
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="compiler-class" type="xsd:string" />
56+
<xsd:attribute name="utf8" type="xsd:boolean" />
5557
</xsd:complexType>
5658

5759
<xsd:complexType name="import">
@@ -68,6 +70,8 @@
6870
<xsd:attribute name="methods" type="xsd:string" />
6971
<xsd:attribute name="controller" type="xsd:string" />
7072
<xsd:attribute name="trailing-slash-on-root" type="xsd:boolean" />
73+
<xsd:attribute name="compiler-class" type="xsd:string" />
74+
<xsd:attribute name="uft8" type="xsd:boolean" />
7175
</xsd:complexType>
7276

7377
<xsd:complexType name="default" mixed="true">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
use Symfony\Component\Routing\RouteCompiler;
7+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
8+
9+
/**
10+
* @Route("/test", compilerClass=CustomRouteCompiler::class)
11+
*/
12+
class CustomCompilerClassActionControllers
13+
{
14+
/**
15+
* @Route(name="one")
16+
*/
17+
public function one()
18+
{
19+
}
20+
21+
/**
22+
* @Route(name="two", compilerClass=RouteCompiler::class)
23+
*/
24+
public function two()
25+
{
26+
}
27+
}
+25Lines 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+
}
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
6+
7+
return function (RoutingConfigurator $routes) {
8+
$routes
9+
->add('some_route', '/')
10+
->add('custom_compiled_route', '/custom-compilation')->compilerClass(CustomRouteCompiler::class)
11+
;
12+
};
+8Lines 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="some_route" path="/" />
7+
<route id="custom_compiled_route" path="/custom-compilation" compiler-class="Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler"/>
8+
</routes>
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
some_route:
2+
path: /
3+
4+
custom_compiled_route:
5+
path: /custom-compilation
6+
compiler_class: 'Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler'
+10Lines 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+
};
+8Lines 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="some_route" path="/" />
7+
<route id="some_utf8_route" path="/utf8" utf8="true"/>
8+
</routes>
+6Lines 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

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Symfony\Component\Routing\Annotation\Route as RouteAnnotation;
1717
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
1818
use Symfony\Component\Routing\Route;
19+
use Symfony\Component\Routing\RouteCompiler;
1920
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;
2021
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\ActionPathController;
22+
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\CustomCompilerClassActionControllers;
2123
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\DefaultValueController;
2224
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\ExplicitLocalizedActionPathController;
2325
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableController;
@@ -35,6 +37,8 @@
3537
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\PrefixedActionPathController;
3638
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RequirementsWithoutPlaceholderNameController;
3739
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\RouteWithPrefixController;
40+
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\Utf8ActionControllers;
41+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
3842

3943
class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
4044
{
@@ -157,6 +161,22 @@ public function testInvokableClassRouteLoadWithMethodAnnotation()
157161
$this->assertEquals('/the/path', $routes->get('post.en')->getPath());
158162
}
159163

164+
public function testCustomCompilerRoutesLoadWithAnnotation()
165+
{
166+
$routes = $this->loader->load(CustomCompilerClassActionControllers::class);
167+
$this->assertCount(2, $routes);
168+
$this->assertSame(CustomRouteCompiler::class, $routes->get('one')->getOption('compiler_class'));
169+
$this->assertSame(RouteCompiler::class, $routes->get('two')->getOption('compiler_class'));
170+
}
171+
172+
public function testUtf8RoutesLoadWithAnnotation()
173+
{
174+
$routes = $this->loader->load(Utf8ActionControllers::class);
175+
$this->assertCount(2, $routes);
176+
$this->assertTrue($routes->get('one')->getOption('utf8'), 'The route must accept utf8');
177+
$this->assertFalse($routes->get('two')->getOption('utf8'), 'The route must not accept utf8');
178+
}
179+
160180
public function testRouteWithPathWithPrefix()
161181
{
162182
$routes = $this->loader->load(PrefixedActionPathController::class);

‎src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Routing\Loader\PhpFileLoader;
1818
use Symfony\Component\Routing\Route;
1919
use Symfony\Component\Routing\RouteCollection;
20+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
2021

2122
class PhpFileLoaderTest extends TestCase
2223
{
@@ -84,6 +85,42 @@ public function testThatDefiningVariableInConfigFileHasNoSideEffects()
8485
);
8586
}
8687

88+
public function testLoadingCustomCompiledRoute()
89+
{
90+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
91+
$routes = $loader->load('custom_compiler_class.php');
92+
93+
$this->assertCount(2, $routes);
94+
95+
$expectedRoutes = new RouteCollection();
96+
$expectedRoutes->add('some_route', new Route('/'));
97+
98+
$expectedRoutes->add('custom_compiled_route', $route = new Route('/custom-compilation'));
99+
$route->setOption('compiler_class', CustomRouteCompiler::class);
100+
101+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/custom_compiler_class.php'));
102+
103+
$this->assertEquals($expectedRoutes, $routes);
104+
}
105+
106+
public function testLoadingUtf8Route()
107+
{
108+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
109+
$routes = $loader->load('utf8.php');
110+
111+
$this->assertCount(2, $routes);
112+
113+
$expectedRoutes = new RouteCollection();
114+
$expectedRoutes->add('some_route', new Route('/'));
115+
116+
$expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
117+
$route->setOption('utf8', true);
118+
119+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.php'));
120+
121+
$this->assertEquals($expectedRoutes, $routes);
122+
}
123+
87124
public function testRoutingConfigurator()
88125
{
89126
$locator = new FileLocator([__DIR__.'/../Fixtures']);

‎src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\Resource\FileResource;
1617
use Symfony\Component\Routing\Loader\XmlFileLoader;
18+
use Symfony\Component\Routing\Route;
19+
use Symfony\Component\Routing\RouteCollection;
20+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
1721
use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
1822

1923
class XmlFileLoaderTest extends TestCase
@@ -83,6 +87,42 @@ public function testLoadWithImport()
8387
}
8488
}
8589

90+
public function testLoadingCustomCompiledRoute()
91+
{
92+
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
93+
$routes = $loader->load('custom_compiler_class.xml');
94+
95+
$this->assertCount(2, $routes);
96+
97+
$expectedRoutes = new RouteCollection();
98+
$expectedRoutes->add('some_route', new Route('/'));
99+
100+
$expectedRoutes->add('custom_compiled_route', $route = new Route('/custom-compilation'));
101+
$route->setOption('compiler_class', CustomRouteCompiler::class);
102+
103+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/custom_compiler_class.xml'));
104+
105+
$this->assertEquals($expectedRoutes, $routes);
106+
}
107+
108+
public function testLoadingUtf8Route()
109+
{
110+
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
111+
$routes = $loader->load('utf8.xml');
112+
113+
$this->assertCount(2, $routes);
114+
115+
$expectedRoutes = new RouteCollection();
116+
$expectedRoutes->add('some_route', new Route('/'));
117+
118+
$expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
119+
$route->setOption('utf8', true);
120+
121+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.xml'));
122+
123+
$this->assertEquals($expectedRoutes, $routes);
124+
}
125+
86126
public function testLoadLocalized()
87127
{
88128
$loader = new XmlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));

0 commit comments

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