Skip to content

Commit

Permalink
[FrameworkBundle] use framework.translator.enabled_locales to build r…
Browse files Browse the repository at this point in the history
…outes' default "_locale" requirement
  • Loading branch information
nicolas-grekas committed Feb 4, 2020
1 parent ef30ef5 commit 98b0ef6
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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, $enabledLocales);
}

$container->setParameter('router.resource', $config['resource']);
$router = $container->findDefinition('router.default');
$argument = $router->getArgument(2);
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="routing.resolver" />
<argument type="collection" />
<argument />
</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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class DelegatingLoader extends BaseDelegatingLoader
{
private $loading = false;
private $defaultOptions;
private $defaultLocaleRequirement;

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

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->defaultLocaleRequirement && !$route->getRequirement('_locale')) {
$route->addRequirements(['_locale' => $this->defaultLocaleRequirement]);
}
if (!\is_string($controller = $route->getDefault('_controller'))) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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('fr|en', $container->getDefinition('routing.loader')->getArgument(2));
}

public function testRouterRequiresResourceOption()
Expand Down
Original file line number Diff line number Diff line change
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], '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 98b0ef6

Please sign in to comment.