[FrameworkBundle] use framework.translator.enabled_locales to build routes' default "_locale" requirement
This commit is contained in:
parent
ef30ef55d0
commit
5eebd37625
@ -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()
|
||||
|
@ -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);
|
||||
|
@ -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">
|
||||
|
@ -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" />
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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" />
|
||||
|
@ -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:
|
||||
|
@ -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()
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user