From 5eebd376259278e85acf396a1f4b3ad65b0f6cdf Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 4 Feb 2020 14:02:49 +0100 Subject: [PATCH] [FrameworkBundle] use framework.translator.enabled_locales to build routes' default "_locale" requirement --- .../DependencyInjection/Configuration.php | 7 +------ .../DependencyInjection/FrameworkExtension.php | 9 +++++++-- .../Bundle/FrameworkBundle/Resources/config/routing.xml | 1 + .../Resources/config/schema/symfony-1.0.xsd | 1 + .../Bundle/FrameworkBundle/Routing/DelegatingLoader.php | 7 ++++++- .../Tests/DependencyInjection/Fixtures/php/full.php | 1 + .../Tests/DependencyInjection/Fixtures/xml/full.xml | 2 ++ .../Tests/DependencyInjection/Fixtures/yml/full.yml | 1 + .../Tests/DependencyInjection/FrameworkExtensionTest.php | 2 ++ .../Tests/Routing/DelegatingLoaderTest.php | 6 ++++-- 10 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index a2016b38c4..65cf6e5599 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -669,6 +669,7 @@ class Configuration implements ConfigurationInterface ->{!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".') @@ -689,12 +690,6 @@ class Configuration implements ConfigurationInterface ->arrayNode('enabled_locales') ->prototype('scalar') ->defaultValue([]) - ->beforeNormalization() - ->always() - ->then(function ($config) { - return array_unique((array) $config); - }) - ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 8b20c27f4d..09d141b2d2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -354,7 +354,7 @@ class FrameworkExtension extends Extension $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); @@ -845,7 +845,7 @@ class FrameworkExtension extends Extension } } - 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'); @@ -864,6 +864,11 @@ class FrameworkExtension extends Extension $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); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index 96ac2c72b4..bf739e71ab 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -48,6 +48,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 97c392812e..23a13677cc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -169,6 +169,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php b/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php index f25bdf32d7..36533e12f0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php @@ -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); } @@ -73,6 +75,9 @@ class DelegatingLoader extends BaseDelegatingLoader 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; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php index 813b51541e..b11b5e08dc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -50,6 +50,7 @@ $container->loadFromExtension('framework', [ 'fallback' => 'fr', 'paths' => ['%kernel.project_dir%/Fixtures/translations'], 'cache_dir' => '%kernel.cache_dir%/translations', + 'enabled_locales' => ['fr', 'en'] ], 'validation' => [ 'enabled' => true, diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index aaeeba580a..10a646049d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -28,6 +28,8 @@ %kernel.project_dir%/Fixtures/translations + fr + en diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index fff49e7528..5ad80a2da4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -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: diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 6f1c924f36..7ac19bcd18 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -458,6 +458,8 @@ abstract class FrameworkExtensionTest extends TestCase $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() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php index de7f91ee63..13a7f5547c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/DelegatingLoaderTest.php @@ -32,13 +32,13 @@ class DelegatingLoaderTest extends TestCase $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); @@ -48,6 +48,7 @@ class DelegatingLoaderTest extends TestCase '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', @@ -55,5 +56,6 @@ class DelegatingLoaderTest extends TestCase 'utf8' => true, ]; $this->assertSame($expected, $routeCollection->get('bar')->getOptions()); + $this->assertSame(['_locale' => 'de'], $routeCollection->get('bar')->getRequirements()); } }