Skip to content

Commit

Permalink
feature #35590 [FrameworkBundle] use framework.translator.enabled_loc…
Browse files Browse the repository at this point in the history
…ales to build routes' default "_locale" requirement (nicolas-grekas)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[FrameworkBundle] use framework.translator.enabled_locales to build routes' default "_locale" requirement

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

No need to configure the same requirements for `_locale` in all routes any more thanks to the `framework.translator.enabled_locales` config option introduced in #32433.

Commits
-------

5eebd37 [FrameworkBundle] use framework.translator.enabled_locales to build routes' default "_locale" requirement
  • Loading branch information
fabpot committed Feb 4, 2020
2 parents c8725bf + 5eebd37 commit 7a4637e
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 11 deletions.
Expand Up @@ -669,6 +669,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->{!class_exists(FullStack::class) && class_exists(Translator::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->fixXmlConfig('fallback')
->fixXmlConfig('path')
->fixXmlConfig('enabled_locale')
->children()
->arrayNode('fallbacks')
->info('Defaults to the value of "default_locale".')
Expand All @@ -689,12 +690,6 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->arrayNode('enabled_locales')
->prototype('scalar')
->defaultValue([])
->beforeNormalization()
->always()
->then(function ($config) {
return array_unique((array) $config);
})
->end()
->end()
->end()
->end()
Expand Down
Expand Up @@ -354,7 +354,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
$this->registerDebugConfiguration($config['php_errors'], $container, $loader);
$this->registerRouterConfiguration($config['router'], $container, $loader);
$this->registerRouterConfiguration($config['router'], $container, $loader, $config['translator']['enabled_locales'] ?? []);
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
$this->registerSecretsConfiguration($config['secrets'], $container, $loader);
Expand Down Expand Up @@ -845,7 +845,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
}
}

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

if ($enabledLocales) {
$enabledLocales = implode('|', array_map('preg_quote', $enabledLocales));
$container->getDefinition('routing.loader')->replaceArgument(2, ['_locale' => $enabledLocales]);
}

$container->setParameter('router.resource', $config['resource']);
$router = $container->findDefinition('router.default');
$argument = $router->getArgument(2);
Expand Down
Expand Up @@ -48,6 +48,7 @@
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
<argument type="service" id="routing.resolver" />
<argument type="collection" />
<argument type="collection" />
</service>

<service id="router.default" class="Symfony\Bundle\FrameworkBundle\Routing\Router">
Expand Down
Expand Up @@ -169,6 +169,7 @@
<xsd:sequence>
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="enabled-locale" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="fallback" type="xsd:string" />
Expand Down
Expand Up @@ -29,10 +29,12 @@ class DelegatingLoader extends BaseDelegatingLoader
{
private $loading = false;
private $defaultOptions;
private $defaultRequirements;

public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [])
public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [], array $defaultRequirements = [])
{
$this->defaultOptions = $defaultOptions;
$this->defaultRequirements = $defaultRequirements;

parent::__construct($resolver);
}
Expand Down Expand Up @@ -73,6 +75,9 @@ public function load($resource, string $type = null)
if ($this->defaultOptions) {
$route->setOptions($route->getOptions() + $this->defaultOptions);
}
if ($this->defaultRequirements) {
$route->setRequirements($route->getRequirements() + $this->defaultRequirements);
}
if (!\is_string($controller = $route->getDefault('_controller'))) {
continue;
}
Expand Down
Expand Up @@ -50,6 +50,7 @@
'fallback' => 'fr',
'paths' => ['%kernel.project_dir%/Fixtures/translations'],
'cache_dir' => '%kernel.cache_dir%/translations',
'enabled_locales' => ['fr', 'en']
],
'validation' => [
'enabled' => true,
Expand Down
Expand Up @@ -28,6 +28,8 @@
<framework:assets version="v1" />
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="%kernel.cache_dir%/translations">
<framework:path>%kernel.project_dir%/Fixtures/translations</framework:path>
<framework:enabled-locale>fr</framework:enabled-locale>
<framework:enabled-locale>en</framework:enabled-locale>
</framework:translator>
<framework:validation enabled="true" />
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
Expand Down
Expand Up @@ -41,6 +41,7 @@ framework:
default_path: '%kernel.project_dir%/translations'
cache_dir: '%kernel.cache_dir%/translations'
paths: ['%kernel.project_dir%/Fixtures/translations']
enabled_locales: [fr, en]
validation:
enabled: true
annotations:
Expand Down
Expand Up @@ -458,6 +458,8 @@ public function testRouter()
$this->assertEquals($container->getParameter('kernel.project_dir').'/config/routing.xml', $container->getParameter('router.resource'), '->registerRouterConfiguration() sets routing resource');
$this->assertEquals('%router.resource%', $arguments[1], '->registerRouterConfiguration() sets routing resource');
$this->assertEquals('xml', $arguments[2]['resource_type'], '->registerRouterConfiguration() sets routing resource type');

$this->assertSame(['_locale' => 'fr|en'], $container->getDefinition('routing.loader')->getArgument(2));
}

public function testRouterRequiresResourceOption()
Expand Down
Expand Up @@ -32,13 +32,13 @@ public function testLoadDefaultOptions()

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

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

$delegatingLoader = new DelegatingLoader($loaderResolver, ['utf8' => true]);
$delegatingLoader = new DelegatingLoader($loaderResolver, ['utf8' => true], ['_locale' => 'fr|en']);

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

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

0 comments on commit 7a4637e

Please sign in to comment.