Skip to content

[FrameworkBundle] Allow to disable the PropertyAccessor and annotations #17706

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 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ private function addAnnotationsSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('annotations')
->info('annotation configuration')
->addDefaultsIfNotSet()
->canBeDisabled()
->children()
->scalarNode('cache')->defaultValue('file')->end()
->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
Expand Down Expand Up @@ -525,8 +525,8 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
$rootNode
->children()
->arrayNode('property_access')
->addDefaultsIfNotSet()
->info('Property access configuration')
->canBeDisabled()
->children()
->booleanNode('magic_call')->defaultFalse()->end()
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class FrameworkExtension extends Extension
private $formConfigEnabled = false;
private $translationConfigEnabled = false;
private $sessionConfigEnabled = false;
private $propertyAccessConfigEnabled = false;
private $annotationsConfigEnabled = false;

/**
* @var string|null
Expand Down Expand Up @@ -68,9 +70,6 @@ public function load(array $configs, ContainerBuilder $container)
// will be used and everything will still work as expected.
$loader->load('translation.xml');

// Property access is used by both the Form and the Validator component
$loader->load('property_access.xml');

$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

Expand All @@ -92,6 +91,16 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerSessionConfiguration($config['session'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['property_access'])) {
$this->propertyAccessConfigEnabled = true;
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['annotations'])) {
$this->annotationsConfigEnabled = true;
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['request'])) {
$this->registerRequestConfiguration($config['request'], $container, $loader);
}
Expand All @@ -106,7 +115,9 @@ public function load(array $configs, ContainerBuilder $container)
}
}

$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
if ($this->isConfigEnabled($container, $config['csrf_protection'])) {
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['assets'])) {
$this->registerAssetsConfiguration($config['assets'], $container, $loader);
Expand All @@ -116,25 +127,38 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader);
}

$this->registerValidationConfiguration($config['validation'], $container, $loader);
$this->registerEsiConfiguration($config['esi'], $container, $loader);
$this->registerSsiConfiguration($config['ssi'], $container, $loader);
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
$this->registerTranslatorConfiguration($config['translator'], $container);
if ($this->isConfigEnabled($container, $config['validation'])) {
$this->registerValidationConfiguration($config['validation'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['esi'])) {
$loader->load('esi.xml');
}

if ($this->isConfigEnabled($container, $config['ssi'])) {
$loader->load('ssi.xml');
}

if ($this->isConfigEnabled($container, $config['fragments'])) {
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['translator'])) {
$this->translationConfigEnabled = true;
$this->registerTranslatorConfiguration($config['translator'], $container);
}

$this->registerProfilerConfiguration($config['profiler'], $container, $loader);

if ($this->isConfigEnabled($container, $config['router'])) {
$this->registerRouterConfiguration($config['router'], $container, $loader);
}

$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
$this->registerPropertyAccessConfiguration($config['property_access'], $container);

if (isset($config['serializer'])) {
if ($this->isConfigEnabled($container, $config['serializer'])) {
$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
}

if (isset($config['property_info'])) {
if ($this->isConfigEnabled($container, $config['property_info'])) {
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
}

Expand Down Expand Up @@ -208,6 +232,10 @@ public function getConfiguration(array $config, ContainerBuilder $container)
*/
private function registerFormConfiguration($config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$this->propertyAccessConfigEnabled) {
throw new LogicException('"framework.property_access" must be enabled when "framework.form" is enabled.');
}

$loader->load('form.xml');
if (null === $config['form']['csrf_protection']['enabled']) {
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
Expand All @@ -223,22 +251,6 @@ private function registerFormConfiguration($config, ContainerBuilder $container,
}
}

/**
* Loads the ESI configuration.
*
* @param array $config An ESI configuration array
* @param ContainerBuilder $container A ContainerBuilder instance
* @param XmlFileLoader $loader An XmlFileLoader instance
*/
private function registerEsiConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$this->isConfigEnabled($container, $config)) {
return;
}

$loader->load('esi.xml');
}

/**
* Loads the SSI configuration.
*
Expand Down Expand Up @@ -654,11 +666,6 @@ private function createVersion(ContainerBuilder $container, $version, $format, $
*/
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container)
{
if (!$this->isConfigEnabled($container, $config)) {
return;
}
$this->translationConfigEnabled = true;

// Use the "real" translator instead of the identity default
$container->setAlias('translator', 'translator.default');
$translator = $container->findDefinition('translator.default');
Expand Down Expand Up @@ -752,6 +759,10 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
return;
}

if (!$this->propertyAccessConfigEnabled) {
throw new LogicException('"framework.property_access" must be enabled when "framework.validator" is enabled.');
}

$loader->load('validator.xml');

$validatorBuilder = $container->getDefinition('validator.builder');
Expand All @@ -771,6 +782,9 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
$definition->replaceArgument(0, $config['strict_email']);

if (array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
if (!$this->annotationsConfigEnabled) {
throw new \LogicException('"framework.annotations" must be enabled when "framework.serializer.enable_annotations" is set to true.');
}
$validatorBuilder->addMethodCall('enableAnnotationMapping', array(new Reference('annotation_reader')));
}

Expand Down Expand Up @@ -857,8 +871,11 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
}
}

private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container)
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
// Property access is used by the Form, the Validator and the Serializer component
$loader->load('property_access.xml');

$container
->getDefinition('property_accessor')
->replaceArgument(0, $config['magic_call'])
Expand All @@ -877,10 +894,6 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui
*/
private function registerSecurityCsrfConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$this->isConfigEnabled($container, $config)) {
return;
}

if (!$this->sessionConfigEnabled) {
throw new \LogicException('CSRF protection needs sessions to be enabled.');
}
Expand All @@ -898,8 +911,8 @@ private function registerSecurityCsrfConfiguration(array $config, ContainerBuild
*/
private function registerSerializerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$this->isConfigEnabled($container, $config)) {
return;
if (!$this->propertyAccessConfigEnabled) {
throw new LogicException('"framework.property_access" must be enabled when "framework.serializer" is enabled.');
}

if (class_exists('Symfony\Component\Serializer\Normalizer\DataUriNormalizer')) {
Expand Down Expand Up @@ -928,6 +941,9 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder

$serializerLoaders = array();
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
if (!$this->annotationsConfigEnabled) {
throw new \LogicException('"framework.annotations" must be enabled when "framework.serializer.enable_annotations" is set to true.');
}
$annotationLoader = new Definition(
'Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader',
array(new Reference('annotation_reader'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
</xsd:complexType>

<xsd:complexType name="property_access">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="magic-call" type="xsd:boolean" />
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ protected static function getBundleDefaultConfig()
'strict_email' => false,
),
'annotations' => array(
'enabled' => true,
'cache' => 'file',
'file_cache_dir' => '%kernel.cache_dir%/annotations',
'debug' => true,
Expand All @@ -222,6 +223,7 @@ protected static function getBundleDefaultConfig()
'enable_annotations' => false,
),
'property_access' => array(
'enabled' => true,
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$container->loadFromExtension('framework', array(
'annotations' => array(
'enabled' => false,
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$container->loadFromExtension('framework', array(
'property_access' => array(
'enabled' => false,
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:annotations enabled="false" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:property-access enabled="false" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
annotations:
enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
framework:
property_access:
enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public function testPropertyAccessWithOverriddenValues()
$this->assertTrue($def->getArgument(1));
}

public function testPropertyAccessDisabled()
{
$container = $this->createContainerFromFile('property_accessor_disabled');
$this->assertFalse($container->hasDefinition('property_accessor'));
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage CSRF protection needs sessions to be enabled.
Expand Down