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 98b0ef6

Browse filesBrowse files
[FrameworkBundle] use framework.translator.enabled_locales to build routes' default "_locale" requirement
1 parent ef30ef5 commit 98b0ef6
Copy full SHA for 98b0ef6

File tree

10 files changed

+26
-11
lines changed
Filter options

10 files changed

+26
-11
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
669669
->{!class_exists(FullStack::class) && class_exists(Translator::class) ? 'canBeDisabled' : 'canBeEnabled'}()
670670
->fixXmlConfig('fallback')
671671
->fixXmlConfig('path')
672+
->fixXmlConfig('enabled_locale')
672673
->children()
673674
->arrayNode('fallbacks')
674675
->info('Defaults to the value of "default_locale".')
@@ -689,12 +690,6 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
689690
->arrayNode('enabled_locales')
690691
->prototype('scalar')
691692
->defaultValue([])
692-
->beforeNormalization()
693-
->always()
694-
->then(function ($config) {
695-
return array_unique((array) $config);
696-
})
697-
->end()
698693
->end()
699694
->end()
700695
->end()

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function load(array $configs, ContainerBuilder $container)
354354
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
355355
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
356356
$this->registerDebugConfiguration($config['php_errors'], $container, $loader);
357-
$this->registerRouterConfiguration($config['router'], $container, $loader);
357+
$this->registerRouterConfiguration($config['router'], $container, $loader, $config['translator']['enabled_locales'] ?? []);
358358
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
359359
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
360360
$this->registerSecretsConfiguration($config['secrets'], $container, $loader);
@@ -845,7 +845,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
845845
}
846846
}
847847

848-
private function registerRouterConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
848+
private function registerRouterConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $enabledLocales = [])
849849
{
850850
if (!$this->isConfigEnabled($container, $config)) {
851851
$container->removeDefinition('console.command.router_debug');
@@ -864,6 +864,11 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
864864
$container->getDefinition('routing.loader')->replaceArgument(1, ['utf8' => true]);
865865
}
866866

867+
if ($enabledLocales) {
868+
$enabledLocales = implode('|', array_map('preg_quote', $enabledLocales));
869+
$container->getDefinition('routing.loader')->replaceArgument(2, $enabledLocales);
870+
}
871+
867872
$container->setParameter('router.resource', $config['resource']);
868873
$router = $container->findDefinition('router.default');
869874
$argument = $router->getArgument(2);

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
4949
<argument type="service" id="routing.resolver" />
5050
<argument type="collection" />
51+
<argument />
5152
</service>
5253

5354
<service id="router.default" class="Symfony\Bundle\FrameworkBundle\Routing\Router">

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<xsd:sequence>
170170
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
171171
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
172+
<xsd:element name="enabled-locale" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
172173
</xsd:sequence>
173174
<xsd:attribute name="enabled" type="xsd:boolean" />
174175
<xsd:attribute name="fallback" type="xsd:string" />

‎src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ class DelegatingLoader extends BaseDelegatingLoader
2929
{
3030
private $loading = false;
3131
private $defaultOptions;
32+
private $defaultLocaleRequirement;
3233

33-
public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [])
34+
public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [], string $defaultLocaleRequirement = null)
3435
{
3536
$this->defaultOptions = $defaultOptions;
37+
$this->defaultLocaleRequirement = $defaultLocaleRequirement;
3638

3739
parent::__construct($resolver);
3840
}
@@ -73,6 +75,9 @@ public function load($resource, string $type = null)
7375
if ($this->defaultOptions) {
7476
$route->setOptions($route->getOptions() + $this->defaultOptions);
7577
}
78+
if ($this->defaultLocaleRequirement && !$route->getRequirement('_locale')) {
79+
$route->addRequirements(['_locale' => $this->defaultLocaleRequirement]);
80+
}
7681
if (!\is_string($controller = $route->getDefault('_controller'))) {
7782
continue;
7883
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'fallback' => 'fr',
5151
'paths' => ['%kernel.project_dir%/Fixtures/translations'],
5252
'cache_dir' => '%kernel.cache_dir%/translations',
53+
'enabled_locales' => ['fr', 'en']
5354
],
5455
'validation' => [
5556
'enabled' => true,

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<framework:assets version="v1" />
2929
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="%kernel.cache_dir%/translations">
3030
<framework:path>%kernel.project_dir%/Fixtures/translations</framework:path>
31+
<framework:enabled-locale>fr</framework:enabled-locale>
32+
<framework:enabled-locale>en</framework:enabled-locale>
3133
</framework:translator>
3234
<framework:validation enabled="true" />
3335
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ framework:
4141
default_path: '%kernel.project_dir%/translations'
4242
cache_dir: '%kernel.cache_dir%/translations'
4343
paths: ['%kernel.project_dir%/Fixtures/translations']
44+
enabled_locales: [fr, en]
4445
validation:
4546
enabled: true
4647
annotations:

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ public function testRouter()
458458
$this->assertEquals($container->getParameter('kernel.project_dir').'/config/routing.xml', $container->getParameter('router.resource'), '->registerRouterConfiguration() sets routing resource');
459459
$this->assertEquals('%router.resource%', $arguments[1], '->registerRouterConfiguration() sets routing resource');
460460
$this->assertEquals('xml', $arguments[2]['resource_type'], '->registerRouterConfiguration() sets routing resource type');
461+
462+
$this->assertSame('fr|en', $container->getDefinition('routing.loader')->getArgument(2));
461463
}
462464

463465
public function testRouterRequiresResourceOption()

‎src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public function testLoadDefaultOptions()
3232

3333
$routeCollection = new RouteCollection();
3434
$routeCollection->add('foo', new Route('/', [], [], ['utf8' => false]));
35-
$routeCollection->add('bar', new Route('/', [], [], ['foo' => 123]));
35+
$routeCollection->add('bar', new Route('/', [], ['_locale' => 'de'], ['foo' => 123]));
3636

3737
$loader->expects($this->once())
3838
->method('load')
3939
->willReturn($routeCollection);
4040

41-
$delegatingLoader = new DelegatingLoader($loaderResolver, ['utf8' => true]);
41+
$delegatingLoader = new DelegatingLoader($loaderResolver, ['utf8' => true], 'fr|en');
4242

4343
$loadedRouteCollection = $delegatingLoader->load('foo');
4444
$this->assertCount(2, $loadedRouteCollection);
@@ -48,12 +48,14 @@ public function testLoadDefaultOptions()
4848
'utf8' => false,
4949
];
5050
$this->assertSame($expected, $routeCollection->get('foo')->getOptions());
51+
$this->assertSame(['_locale' => 'fr|en'], $routeCollection->get('foo')->getRequirements());
5152

5253
$expected = [
5354
'compiler_class' => 'Symfony\Component\Routing\RouteCompiler',
5455
'foo' => 123,
5556
'utf8' => true,
5657
];
5758
$this->assertSame($expected, $routeCollection->get('bar')->getOptions());
59+
$this->assertSame(['_locale' => 'de'], $routeCollection->get('bar')->getRequirements());
5860
}
5961
}

0 commit comments

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