Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('form')
->info('form configuration')
->canBeDisabled()
->canBeEnabled()
Copy link
Member

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))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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

Copy link
Contributor Author

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 !

->end()
->arrayNode('csrf_protection')
->canBeDisabled()
->canBeEnabled()
->children()
->scalarNode('field_name')->defaultValue('_token')->end()
->end()
Expand All @@ -109,7 +109,7 @@ private function addEsiSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('esi')
->info('esi configuration')
->canBeDisabled()
->canBeEnabled()
->end()
->end()
;
Expand All @@ -121,7 +121,7 @@ private function addRouterProxySection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('router_proxy')
->info('proxy configuration for the HTTP content renderer')
->canBeDisabled()
->canBeEnabled()
->children()
->scalarNode('path')->defaultValue('/_proxy')->end()
->end()
Expand All @@ -136,7 +136,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('profiler')
->info('profiler configuration')
->canBeDisabled()
->canBeEnabled()
->children()
->booleanNode('only_exceptions')->defaultFalse()->end()
->booleanNode('only_master_requests')->defaultFalse()->end()
Expand Down Expand Up @@ -176,8 +176,10 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
->scalarNode('https_port')->defaultValue(443)->end()
->scalarNode('strict_requirements')
->info(
'set to false to disable exceptions when a route is '.
'generated with invalid parameters (and return null instead)'
"set to true to throw an exception when a parameter does not match the requirements\n".
"set to false to disable exceptions when a parameter does not match the requirements (and return null instead)\n".
"set to null to disable parameter checks against requirements\n".
"'true' is the preferred configuration in development mode, while 'false' or 'null' might be preferred in production"
)
->defaultTrue()
->end()
Expand Down Expand Up @@ -367,7 +369,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('translator')
->info('translator configuration')
->canBeDisabled()
->canBeEnabled()
->children()
->scalarNode('fallback')->defaultValue('en')->end()
->end()
Expand All @@ -382,7 +384,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('validation')
->info('validation configuration')
->canBeDisabled()
->canBeEnabled()
->children()
->scalarNode('cache')->end()
->booleanNode('enable_annotations')->defaultFalse()->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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(
Expand Down Expand Up @@ -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'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 156 you can set $container->setParameter('form.type_extension.csrf.enabled', true); because it's only set when it avaluates to true anyway.

if (!isset($config['session'])) {
throw new \LogicException('CSRF protection needs that sessions are enabled.');
}
Expand All @@ -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']);
}

/**
Expand Down Expand Up @@ -258,7 +252,7 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
}
}

if (!$config['enabled']) {
if (!$this->isConfigEnabled($container, $config)) {
$container->getDefinition('profiler')->addMethodCall('disable', array());
}
}
Expand Down Expand Up @@ -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));
}
}
}
Expand All @@ -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']);
Expand Down
Loading