Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c45619

Browse files
author
Jules Pietri
committed
[Routing] Exposed "compiler_class" and "utf8" options in configuration
1 parent 6fd6b94 commit 9c45619

14 files changed

+214
-1
lines changed

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 `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

Lines 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 compiled 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

Lines 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

Lines 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

Lines 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">
Lines 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+
};
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="some_route" path="/" />
7+
<route id="custom_compiled_route" path="/custom-compilation" compiler-class="Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler"/>
8+
</routes>
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+
custom_compiled_route:
5+
path: /custom-compilation
6+
compiler_class: 'Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler'
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: 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>
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

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

Lines 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

Lines 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']));

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\Config\FileLocator;
1616
use Symfony\Component\Config\Resource\FileResource;
1717
use Symfony\Component\Routing\Loader\YamlFileLoader;
18+
use Symfony\Component\Routing\Route;
19+
use Symfony\Component\Routing\RouteCollection;
20+
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
1821

1922
class YamlFileLoaderTest extends TestCase
2023
{
@@ -222,6 +225,42 @@ public function testRemoteSourcesAreNotAccepted()
222225
$loader->load('http://remote.com/here.yml');
223226
}
224227

228+
public function testLoadingCustomCompiledRoute()
229+
{
230+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
231+
$routes = $loader->load('custom_compiler_class.yml');
232+
233+
$this->assertCount(2, $routes);
234+
235+
$expectedRoutes = new RouteCollection();
236+
$expectedRoutes->add('some_route', new Route('/'));
237+
238+
$expectedRoutes->add('custom_compiled_route', $route = new Route('/custom-compilation'));
239+
$route->setOption('compiler_class', CustomRouteCompiler::class);
240+
241+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/custom_compiler_class.yml'));
242+
243+
$this->assertEquals($expectedRoutes, $routes);
244+
}
245+
246+
public function testLoadingUtf8Route()
247+
{
248+
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));
249+
$routes = $loader->load('utf8.yml');
250+
251+
$this->assertCount(2, $routes);
252+
253+
$expectedRoutes = new RouteCollection();
254+
$expectedRoutes->add('some_route', new Route('/'));
255+
256+
$expectedRoutes->add('some_utf8_route', $route = new Route('/utf8'));
257+
$route->setOption('utf8', true);
258+
259+
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/localized/utf8.yml'));
260+
261+
$this->assertEquals($expectedRoutes, $routes);
262+
}
263+
225264
public function testLoadingLocalizedRoute()
226265
{
227266
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/localized']));

0 commit comments

Comments
 (0)