Skip to content

Commit 8e497f2

Browse files
committed
feature symfony#21196 [FrameworkBundle] changed some default configs from canBeEnabled to canBeDisabled (fabpot)
This PR was squashed before being merged into the 3.3-dev branch (closes symfony#21196). Discussion ---------- [FrameworkBundle] changed some default configs from canBeEnabled to canBeDisabled | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a FrameworkBundle configuration is currently "optimized" for when a project depends on symfony/symfony (which is the vast majority of Symfony projects out there as that's how Symfony Standard Edition is set up). As all components are always available, features (forms, validation, translation, serializer, ...) are disabled by default in FrameworkBundle's configuration (`canBeEnabled`) and developers must enable them when they need them (that was done mainly for performance reasons). That's annoying as it means one configuration step before being able to use forms for the first time (or validation, or serialization, or translation, ...). To make features auto-configurable and make the framework a bit more user-friendly (that's where I think I need to invoke DX :), I'd like Symfony 4 to work in a different way. Instead of relying on symfony/symfony, I want people to install only the components/bundles they need. In that scenario, we can auto-configure Symfony FrameworkBundle based on the available components. If you add symfony/form as a dependency, then it makes sense to automatically enable the feature in framework bundle (with the possibility to disable it if needed thanks to `canBeDisabled`). Let's recap: * Before: * You want to use forms; you have symfony/symfony, so nothing to install; * Using forms does not work out of the box though; you need to know that you have to edit `app/config/config.yml` to explicitly enable forms; * Forms work! * After: * You want to use forms so you install symfony/form (like for any other packages out there; want to use Twig for templating, install twig/twig); * But for Symfony components, there are no other steps; forms are auto-configured just because you installed the dependency, go work now! In a way, it makes handling/installing/configuring Symfony components no different than doing the same for a third party package. That's about relying even more on Composer and less on configuration. Symfony components have the extra benefit of being auto-configured via FrameworkBundle. That's not the case for other third-party packages/bundles, but for those who attended SymfonyCon Berlin, you know that this is coming soon via Symfony Flex. That's even more interesting for forms as CSRF protection needs an extra knob to be turned on currently. With the new way, just install the CSRF security component. An again, you still have the possibility to turn it off if you want to. Anyway, this PR gives us the flexibility to do both: when using symfony/symfony, everything works as before, if you are using symfony/framework-bundle, then auto-configuration based on the installed packages is automatically activated. This also brings consistency as this behavior is already what we've done for the Doctrine Annotation library in 3.2. Last, but not the least, with all the work currently done on the container lazyness for Symfony 3.3, concerns about performance are less important than before, so having components auto-enabled when installed should not be a big deal. We might even go one step further and remove enabling/disabling for ESI/SSI/fragments/... And whenever we create an independent Session component, we will be able to do the same with the session configuration. The astute reader might have noticed that I haven't talked about the templating configuration, but as the component will be deprecated in 3.3, I prefer to keep its activation explicit. In terms of BC, the only change is for people using symfony/framework-bundle with some packages that were installed but not enabled in the config. I would say that this should be pretty rare and anyway, the only consequence is a small performance hit which can be easily offset by explicitly disabling the config. That's all folks! Commits ------- ef80873 [FrameworkBundle] changed some default configs from canBeEnabled to canBeDisabled 98ce21a [FrameworkBundle] changed the default value of annotation setting based on the existence of Doctrine Annotations
2 parents e1d3900 + ef80873 commit 8e497f2

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* Changed default configuration for
8+
assets/forms/validation/translation/serialization/csrf from `canBeEnabled()` to
9+
`canBeDisabled()` when Flex is used
710
* The server:* commands and their associated router files were moved to WebServerBundle
811
* Translation related services are not loaded anymore when the `framework.translator` option
912
is disabled.

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
1313

1414
use Doctrine\Common\Annotations\Annotation;
15+
use Symfony\Bundle\FullStack;
16+
use Symfony\Component\Asset\Package;
1517
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1618
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1719
use Symfony\Component\Config\Definition\ConfigurationInterface;
20+
use Symfony\Component\Form\Form;
21+
use Symfony\Component\Serializer\Serializer;
22+
use Symfony\Component\Translation\Translator;
23+
use Symfony\Component\Validator\Validation;
1824

1925
/**
2026
* FrameworkExtension configuration structure.
@@ -139,7 +145,7 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
139145
->children()
140146
->arrayNode('form')
141147
->info('form configuration')
142-
->canBeEnabled()
148+
->{!class_exists(FullStack::class) && class_exists(Form::class) ? 'canBeDisabled' : 'canBeEnabled'}()
143149
->children()
144150
->arrayNode('csrf_protection')
145151
->treatFalseLike(array('enabled' => false))
@@ -506,7 +512,7 @@ private function addAssetsSection(ArrayNodeDefinition $rootNode)
506512
->children()
507513
->arrayNode('assets')
508514
->info('assets configuration')
509-
->canBeEnabled()
515+
->{!class_exists(FullStack::class) && class_exists(Package::class) ? 'canBeDisabled' : 'canBeEnabled'}()
510516
->fixXmlConfig('base_url')
511517
->children()
512518
->scalarNode('version_strategy')->defaultNull()->end()
@@ -573,7 +579,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
573579
->children()
574580
->arrayNode('translator')
575581
->info('translator configuration')
576-
->canBeEnabled()
582+
->{!class_exists(FullStack::class) && class_exists(Translator::class) ? 'canBeDisabled' : 'canBeEnabled'}()
577583
->fixXmlConfig('fallback')
578584
->fixXmlConfig('path')
579585
->children()
@@ -598,10 +604,10 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
598604
->children()
599605
->arrayNode('validation')
600606
->info('validation configuration')
601-
->canBeEnabled()
607+
->{!class_exists(FullStack::class) && class_exists(Validation::class) ? 'canBeDisabled' : 'canBeEnabled'}()
602608
->children()
603609
->scalarNode('cache')->end()
604-
->booleanNode('enable_annotations')->defaultFalse()->end()
610+
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && class_exists(Annotation::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
605611
->arrayNode('static_method')
606612
->defaultValue(array('loadValidatorMetadata'))
607613
->prototype('scalar')->end()
@@ -642,9 +648,9 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
642648
->children()
643649
->arrayNode('serializer')
644650
->info('serializer configuration')
645-
->canBeEnabled()
651+
->{!class_exists(FullStack::class) && class_exists(Serializer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
646652
->children()
647-
->booleanNode('enable_annotations')->defaultFalse()->end()
653+
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) && class_exists(Annotation::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
648654
->scalarNode('cache')->end()
649655
->scalarNode('name_converter')->end()
650656
->end()

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
1313

1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration;
15+
use Symfony\Bundle\FullStack;
1516
use Symfony\Component\Config\Definition\Processor;
1617

1718
class ConfigurationTest extends \PHPUnit_Framework_TestCase
@@ -176,7 +177,7 @@ protected static function getBundleDefaultConfig()
176177
'enabled' => false,
177178
),
178179
'form' => array(
179-
'enabled' => false,
180+
'enabled' => !class_exists(FullStack::class),
180181
'csrf_protection' => array(
181182
'enabled' => null, // defaults to csrf_protection.enabled
182183
'field_name' => '_token',
@@ -200,14 +201,14 @@ protected static function getBundleDefaultConfig()
200201
),
201202
),
202203
'translator' => array(
203-
'enabled' => false,
204+
'enabled' => !class_exists(FullStack::class),
204205
'fallbacks' => array('en'),
205206
'logging' => true,
206207
'paths' => array(),
207208
),
208209
'validation' => array(
209-
'enabled' => false,
210-
'enable_annotations' => false,
210+
'enabled' => !class_exists(FullStack::class),
211+
'enable_annotations' => !class_exists(FullStack::class),
211212
'static_method' => array('loadValidatorMetadata'),
212213
'translation_domain' => 'validators',
213214
'strict_email' => false,
@@ -219,8 +220,8 @@ protected static function getBundleDefaultConfig()
219220
'enabled' => true,
220221
),
221222
'serializer' => array(
222-
'enabled' => false,
223-
'enable_annotations' => false,
223+
'enabled' => !class_exists(FullStack::class),
224+
'enable_annotations' => !class_exists(FullStack::class),
224225
),
225226
'property_access' => array(
226227
'magic_call' => false,
@@ -258,7 +259,7 @@ protected static function getBundleDefaultConfig()
258259
'loaders' => array(),
259260
),
260261
'assets' => array(
261-
'enabled' => false,
262+
'enabled' => !class_exists(FullStack::class),
262263
'version_strategy' => null,
263264
'version' => null,
264265
'version_format' => '%%s?%%s',

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
1313

14+
use Doctrine\Common\Annotations\Annotation;
15+
use Symfony\Bundle\FullStack;
1416
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1517
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
1618
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -24,6 +26,7 @@
2426
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2527
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2628
use Symfony\Component\DependencyInjection\Reference;
29+
use Symfony\Component\Serializer\Serializer;
2730
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
2831
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
2932
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
@@ -413,7 +416,9 @@ public function testValidation()
413416

414417
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
415418

416-
$this->assertCount(6, $calls);
419+
$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);
420+
421+
$this->assertCount($annotations ? 7 : 6, $calls);
417422
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
418423
$this->assertEquals(array(new Reference('validator.validator_factory')), $calls[0][1]);
419424
$this->assertSame('setTranslator', $calls[1][0]);
@@ -422,10 +427,14 @@ public function testValidation()
422427
$this->assertSame(array('%validator.translation_domain%'), $calls[2][1]);
423428
$this->assertSame('addXmlMappings', $calls[3][0]);
424429
$this->assertSame(array($xmlMappings), $calls[3][1]);
425-
$this->assertSame('addMethodMapping', $calls[4][0]);
426-
$this->assertSame(array('loadValidatorMetadata'), $calls[4][1]);
427-
$this->assertSame('setMetadataCache', $calls[5][0]);
428-
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[5][1]);
430+
$i = 3;
431+
if ($annotations) {
432+
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
433+
}
434+
$this->assertSame('addMethodMapping', $calls[++$i][0]);
435+
$this->assertSame(array('loadValidatorMetadata'), $calls[$i][1]);
436+
$this->assertSame('setMetadataCache', $calls[++$i][0]);
437+
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[$i][1]);
429438
}
430439

431440
public function testValidationService()
@@ -536,10 +545,16 @@ public function testValidationNoStaticMethod()
536545

537546
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
538547

539-
$this->assertCount(5, $calls);
548+
$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);
549+
550+
$this->assertCount($annotations ? 6 : 5, $calls);
540551
$this->assertSame('addXmlMappings', $calls[3][0]);
541-
$this->assertSame('setMetadataCache', $calls[4][0]);
542-
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[4][1]);
552+
$i = 3;
553+
if ($annotations) {
554+
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
555+
}
556+
$this->assertSame('setMetadataCache', $calls[++$i][0]);
557+
$this->assertEquals(array(new Reference('validator.mapping.cache.symfony')), $calls[$i][1]);
543558
// no cache, no annotations, no static methods
544559
}
545560

@@ -572,7 +587,7 @@ public function testStopwatchEnabledWithDebugModeDisabled()
572587
public function testSerializerDisabled()
573588
{
574589
$container = $this->createContainerFromFile('default_config');
575-
$this->assertFalse($container->has('serializer'));
590+
$this->assertSame(!class_exists(FullStack::class) && class_exists(Serializer::class), $container->has('serializer'));
576591
}
577592

578593
public function testSerializerEnabled()

src/Symfony/Bundle/FullStack.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle;
13+
14+
/**
15+
* A marker to be able to check if symfony/symfony is installed instead of the individual components/bundles.
16+
*
17+
* @internal
18+
*/
19+
final class FullStack
20+
{
21+
}

0 commit comments

Comments
 (0)