Skip to content

[FrameworkBundle] Expose configuration of PropertyAccess #11731

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

Merged
merged 1 commit into from
Aug 26, 2014
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added `Controller::isCsrfTokenValid` helper
* Added configuration for the PropertyAccess component

2.5.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function getConfigTreeBuilder()
$this->addValidationSection($rootNode);
$this->addAnnotationsSection($rootNode);
$this->addSerializerSection($rootNode);
$this->addPropertyAccessSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -524,4 +525,20 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('property_access')
->addDefaultsIfNotSet()
->info('Property access configuration')
->children()
->booleanNode('magic_call')->defaultFalse()->end()
Copy link
Member

Choose a reason for hiding this comment

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

Should these booleanNode children also include some ->info() methods to explain their purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure, it's a direct mapping to the class argument. So it's already documented. Then I think very few people will change values here.

->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public function load(array $configs, ContainerBuilder $container)

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

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

if (isset($config['serializer']) && $config['serializer']['enabled']) {
$loader->load('serializer.xml');
}
Expand Down Expand Up @@ -818,6 +820,15 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
}
}

private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container)
{
$container
->getDefinition('property_accessor')
->replaceArgument(0, $config['magic_call'])
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please add a test for this functionality to FrameworkExtensionTest?

Copy link
Member Author

Choose a reason for hiding this comment

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

sure ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

}

/**
* Loads the security configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
</parameters>

<services>
<service id="property_accessor" class="%property_accessor.class%" />
<service id="property_accessor" class="%property_accessor.class%" >
<argument /> <!-- magicCall, set by the extension -->
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<xsd:element name="translator" type="translator" minOccurs="0" maxOccurs="1" />
<xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
<xsd:element name="annotations" type="annotations" minOccurs="0" maxOccurs="1" />
<xsd:element name="property-access" type="property_access" minOccurs="0" maxOccurs="1" />
</xsd:all>

<xsd:attribute name="http-method-override" type="xsd:boolean" />
Expand Down Expand Up @@ -177,4 +178,9 @@
<xsd:attribute name="debug" type="xsd:string" />
<xsd:attribute name="file-cache-dir" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="property_access">
<xsd:attribute name="magic-call" type="xsd:boolean" />
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
</xsd:complexType>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ protected static function getBundleDefaultConfig()
),
'serializer' => array(
'enabled' => false
)
),
'property_access' => array(
'magic_call' => false,
'throw_exception_on_invalid_index' => false,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', array(
'property_access' => array(
'magic_call' => true,
'throw_exception_on_invalid_index' => true,
),
));
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 magic-call="true" throw-exception-on-invalid-index="true" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework:
property_access:
magic_call: true
throw_exception_on_invalid_index: true
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ public function testCsrfProtection()
$this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(2));
}

public function testPropertyAccessWithDefaultValue()
{
$container = $this->createContainerFromFile('full');

$def = $container->getDefinition('property_accessor');
$this->assertEquals(false, $def->getArgument(0));
$this->assertEquals(false, $def->getArgument(1));
}

public function testPropertyAccessWithOverriddenValues()
{
$container = $this->createContainerFromFile('property_accessor');
$def = $container->getDefinition('property_accessor');
$this->assertEquals(true, $def->getArgument(0));
$this->assertEquals(true, $def->getArgument(1));
}

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