-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DIC] Better handling of enableable configurations #6852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
728dd93
868dc27
2c2521a
ccbabff
1fb6ede
30167b3
4979960
f714095
ea94ab0
1f540c0
c274274
58f221a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,39 +81,25 @@ public function load(array $configs, ContainerBuilder $container) | |
$this->registerSessionConfiguration($config['session'], $container, $loader); | ||
} | ||
|
||
if (isset($config['form']) && !empty($config['form']['enabled'])) { | ||
if ($this->isConfigEnabled($container, $config['form'])) { | ||
$this->registerFormConfiguration($config, $container, $loader); | ||
$config['validation']['enabled'] = true; | ||
} | ||
|
||
if (!empty($config['validation']['enabled'])) { | ||
$this->registerValidationConfiguration($config['validation'], $container, $loader); | ||
} | ||
|
||
if (isset($config['esi'])) { | ||
$this->registerEsiConfiguration($config['esi'], $loader); | ||
} | ||
|
||
if (isset($config['router_proxy'])) { | ||
$this->registerRouterProxyConfiguration($config['router_proxy'], $container, $loader); | ||
if (isset($config['templating'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why keeping a isset here while you use an early return in other methods ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stof For now this PR only fixes the current config and templating is not an enableable node as of today. |
||
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader); | ||
} | ||
|
||
if (isset($config['profiler'])) { | ||
$this->registerProfilerConfiguration($config['profiler'], $container, $loader); | ||
} | ||
$this->registerValidationConfiguration($config['validation'], $container, $loader); | ||
$this->registerEsiConfiguration($config['esi'], $container, $loader); | ||
$this->registerRouterProxyConfiguration($config['router_proxy'], $container, $loader); | ||
$this->registerProfilerConfiguration($config['profiler'], $container, $loader); | ||
$this->registerTranslatorConfiguration($config['translator'], $container); | ||
|
||
if (isset($config['router'])) { | ||
$this->registerRouterConfiguration($config['router'], $container, $loader); | ||
} | ||
|
||
if (isset($config['templating'])) { | ||
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader); | ||
} | ||
|
||
if (isset($config['translator'])) { | ||
$this->registerTranslatorConfiguration($config['translator'], $container); | ||
} | ||
|
||
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); | ||
|
||
$this->addClassesToCompile(array( | ||
|
@@ -161,7 +147,7 @@ public function load(array $configs, ContainerBuilder $container) | |
private function registerFormConfiguration($config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
$loader->load('form.xml'); | ||
if (isset($config['csrf_protection'])) { | ||
if ($this->isConfigEnabled($container, $config['csrf_protection'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In line 156 you can set |
||
if (!isset($config['session'])) { | ||
throw new \LogicException('CSRF protection needs that sessions are enabled.'); | ||
} | ||
|
@@ -170,36 +156,44 @@ private function registerFormConfiguration($config, ContainerBuilder $container, | |
} | ||
$loader->load('form_csrf.xml'); | ||
|
||
$container->setParameter('form.type_extension.csrf.enabled', $config['csrf_protection']['enabled']); | ||
$container->setParameter('form.type_extension.csrf.enabled', true); | ||
$container->setParameter('form.type_extension.csrf.field_name', $config['csrf_protection']['field_name']); | ||
} else { | ||
$container->setParameter('form.type_extension.csrf.enabled', false); | ||
} | ||
} | ||
|
||
/** | ||
* Loads the ESI configuration. | ||
* | ||
* @param array $config An ESI configuration array | ||
* @param XmlFileLoader $loader An XmlFileLoader instance | ||
* @param array $config A proxy configuration array | ||
* @param ContainerBuilder $container A ContainerBuilder instance | ||
* @param XmlFileLoader $loader An XmlFileLoader instance | ||
*/ | ||
private function registerEsiConfiguration(array $config, XmlFileLoader $loader) | ||
private function registerEsiConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
if (!empty($config['enabled'])) { | ||
$loader->load('esi.xml'); | ||
if (!$this->isConfigEnabled($container, $config)) { | ||
return; | ||
} | ||
|
||
$loader->load('esi.xml'); | ||
} | ||
|
||
/** | ||
* Loads the router proxy configuration. | ||
* | ||
* @param array $config A proxy configuration array | ||
* @param XmlFileLoader $loader An XmlFileLoader instance | ||
* @param array $config A proxy configuration array | ||
* @param ContainerBuilder $container A ContainerBuilder instance | ||
* @param XmlFileLoader $loader An XmlFileLoader instance | ||
*/ | ||
private function registerRouterProxyConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
if (!empty($config['enabled'])) { | ||
$loader->load('proxy.xml'); | ||
$container->setParameter('http_content_renderer.proxy_path', $config['path']); | ||
if (!$this->isConfigEnabled($container, $config)) { | ||
return; | ||
} | ||
|
||
$loader->load('proxy.xml'); | ||
$container->setParameter('http_content_renderer.proxy_path', $config['path']); | ||
} | ||
|
||
/** | ||
|
@@ -258,7 +252,7 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $ | |
} | ||
} | ||
|
||
if (!$config['enabled']) { | ||
if (!$this->isConfigEnabled($container, $config)) { | ||
$container->getDefinition('profiler')->addMethodCall('disable', array()); | ||
} | ||
} | ||
|
@@ -530,61 +524,63 @@ private function createPackageDefinition(ContainerBuilder $container, array $htt | |
*/ | ||
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container) | ||
{ | ||
if (!empty($config['enabled'])) { | ||
// Use the "real" translator instead of the identity default | ||
$container->setAlias('translator', 'translator.default'); | ||
$translator = $container->findDefinition('translator.default'); | ||
$translator->addMethodCall('setFallbackLocale', array($config['fallback'])); | ||
|
||
// Discover translation directories | ||
$dirs = array(); | ||
if (class_exists('Symfony\Component\Validator\Validator')) { | ||
$r = new \ReflectionClass('Symfony\Component\Validator\Validator'); | ||
|
||
$dirs[] = dirname($r->getFilename()).'/Resources/translations'; | ||
} | ||
if (class_exists('Symfony\Component\Form\Form')) { | ||
$r = new \ReflectionClass('Symfony\Component\Form\Form'); | ||
if (!$this->isConfigEnabled($container, $config)) { | ||
return; | ||
} | ||
|
||
$dirs[] = dirname($r->getFilename()).'/Resources/translations'; | ||
} | ||
if (class_exists('Symfony\Component\Security\Core\Exception\AuthenticationException')) { | ||
$r = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException'); | ||
// Use the "real" translator instead of the identity default | ||
$container->setAlias('translator', 'translator.default'); | ||
$translator = $container->findDefinition('translator.default'); | ||
$translator->addMethodCall('setFallbackLocale', array($config['fallback'])); | ||
|
||
$dirs[] = dirname($r->getFilename()).'/../../Resources/translations'; | ||
} | ||
$overridePath = $container->getParameter('kernel.root_dir').'/Resources/%s/translations'; | ||
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { | ||
$reflection = new \ReflectionClass($class); | ||
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/translations')) { | ||
$dirs[] = $dir; | ||
} | ||
if (is_dir($dir = sprintf($overridePath, $bundle))) { | ||
$dirs[] = $dir; | ||
} | ||
// Discover translation directories | ||
$dirs = array(); | ||
if (class_exists('Symfony\Component\Validator\Validator')) { | ||
$r = new \ReflectionClass('Symfony\Component\Validator\Validator'); | ||
|
||
$dirs[] = dirname($r->getFilename()).'/Resources/translations'; | ||
} | ||
if (class_exists('Symfony\Component\Form\Form')) { | ||
$r = new \ReflectionClass('Symfony\Component\Form\Form'); | ||
|
||
$dirs[] = dirname($r->getFilename()).'/Resources/translations'; | ||
} | ||
if (class_exists('Symfony\Component\Security\Core\Exception\AuthenticationException')) { | ||
$r = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException'); | ||
|
||
$dirs[] = dirname($r->getFilename()).'/../../Resources/translations'; | ||
} | ||
$overridePath = $container->getParameter('kernel.root_dir').'/Resources/%s/translations'; | ||
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { | ||
$reflection = new \ReflectionClass($class); | ||
if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/translations')) { | ||
$dirs[] = $dir; | ||
} | ||
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/translations')) { | ||
if (is_dir($dir = sprintf($overridePath, $bundle))) { | ||
$dirs[] = $dir; | ||
} | ||
} | ||
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/translations')) { | ||
$dirs[] = $dir; | ||
} | ||
|
||
// Register translation resources | ||
if ($dirs) { | ||
foreach ($dirs as $dir) { | ||
$container->addResource(new DirectoryResource($dir)); | ||
} | ||
$finder = Finder::create() | ||
->files() | ||
->filter(function (\SplFileInfo $file) { | ||
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename()); | ||
}) | ||
->in($dirs) | ||
; | ||
|
||
foreach ($finder as $file) { | ||
// filename is domain.locale.format | ||
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3); | ||
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain)); | ||
} | ||
// Register translation resources | ||
if ($dirs) { | ||
foreach ($dirs as $dir) { | ||
$container->addResource(new DirectoryResource($dir)); | ||
} | ||
$finder = Finder::create() | ||
->files() | ||
->filter(function (\SplFileInfo $file) { | ||
return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename()); | ||
}) | ||
->in($dirs) | ||
; | ||
|
||
foreach ($finder as $file) { | ||
// filename is domain.locale.format | ||
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3); | ||
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain)); | ||
} | ||
} | ||
} | ||
|
@@ -598,6 +594,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder | |
*/ | ||
private function registerValidationConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
if (!$this->isConfigEnabled($container, $config)) { | ||
return; | ||
} | ||
|
||
$loader->load('validator.xml'); | ||
|
||
$container->setParameter('validator.translation_domain', $config['translation_domain']); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not BC with 2.1. You need to add
treatNullLike(array('enabled' => true))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default: https://github.com/symfony/symfony/pull/6852/files#L4L226
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah indeed, I forgot it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried to keep BC as much as possible (some other configs should probably get updated later) but it still needs a careful review !