Skip to content

[PropertyAccess] Allow to disable magic __get & __set #32133

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 19, 2020
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
11 changes: 11 additions & 0 deletions UPGRADE-5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ Mime

* Deprecated `Address::fromString()`, use `Address::create()` instead

PropertyAccess
--------------

* Deprecated passing a boolean as the first argument of `PropertyAccessor::__construct()`.
Pass a combination of bitwise flags instead.

PropertyInfo
------------

* Dropped the `enable_magic_call_extraction` context option in `ReflectionExtractor::getWriteInfo()` and `ReflectionExtractor::getReadInfo()` in favor of `enable_magic_methods_extraction`.

TwigBundle
----------

Expand Down
11 changes: 11 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ PhpUnitBridge

* Removed support for `@expectedDeprecation` annotations, use the `ExpectDeprecationTrait::expectDeprecation()` method instead.

PropertyAccess
--------------

* Dropped support of a boolean as the first argument of `PropertyAccessor::__construct()`.
Pass a combination of bitwise flags instead.

PropertyInfo
------------

* Dropped the `enable_magic_call_extraction` context option in `ReflectionExtractor::getWriteInfo()` and `ReflectionExtractor::getReadInfo()` in favor of `enable_magic_methods_extraction`.

Routing
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,8 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
->info('Property access configuration')
->children()
->booleanNode('magic_call')->defaultFalse()->end()
->booleanNode('magic_get')->defaultTrue()->end()
->booleanNode('magic_set')->defaultTrue()->end()
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
->booleanNode('throw_exception_on_invalid_property_path')->defaultTrue()->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,14 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui

$loader->load('property_access.php');

$magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
$magicMethods |= $config['magic_call'] ? PropertyAccessor::MAGIC_CALL : 0;
$magicMethods |= $config['magic_get'] ? PropertyAccessor::MAGIC_GET : 0;
$magicMethods |= $config['magic_set'] ? PropertyAccessor::MAGIC_SET : 0;

$container
->getDefinition('property_accessor')
->replaceArgument(0, $config['magic_call'])
->replaceArgument(0, $magicMethods)
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
->replaceArgument(3, $config['throw_exception_on_invalid_property_path'])
->replaceArgument(4, new Reference(PropertyReadInfoExtractorInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$container->services()
->set('property_accessor', PropertyAccessor::class)
->args([
abstract_arg('magicCall, set by the extension'),
abstract_arg('magic methods allowed, set by the extension'),
abstract_arg('throwExceptionOnInvalidIndex, set by the extension'),
service('cache.property_access')->ignoreOnInvalid(),
abstract_arg('throwExceptionOnInvalidPropertyPath, set by the extension'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@

<xsd:complexType name="property_access">
<xsd:attribute name="magic-call" type="xsd:boolean" />
<xsd:attribute name="magic-get" type="xsd:boolean" />
<xsd:attribute name="magic-set" type="xsd:boolean" />
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
<xsd:attribute name="throw-exception-on-invalid-property-path" type="xsd:boolean" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ protected static function getBundleDefaultConfig()
],
'property_access' => [
'magic_call' => false,
'magic_get' => true,
'magic_set' => true,
'throw_exception_on_invalid_index' => false,
'throw_exception_on_invalid_property_path' => true,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
$container->loadFromExtension('framework', [
'property_access' => [
'magic_call' => true,
'magic_get' => true,
'magic_set' => false,
'throw_exception_on_invalid_index' => true,
'throw_exception_on_invalid_property_path' => false,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:property-access magic-call="true" throw-exception-on-invalid-index="true" throw-exception-on-invalid-property-path="false"/>
<framework:property-access magic-call="true" magic-get="true" magic-set="false" throw-exception-on-invalid-index="true" throw-exception-on-invalid-property-path="false"/>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
framework:
property_access:
magic_call: true
magic_get: true
magic_set: false
throw_exception_on_invalid_index: true
throw_exception_on_invalid_property_path: false
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testPropertyAccessWithDefaultValue()
$container = $this->createContainerFromFile('full');

$def = $container->getDefinition('property_accessor');
$this->assertFalse($def->getArgument(0));
$this->assertSame(PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_GET, $def->getArgument(0));
$this->assertFalse($def->getArgument(1));
$this->assertTrue($def->getArgument(3));
}
Expand All @@ -95,7 +95,7 @@ public function testPropertyAccessWithOverriddenValues()
{
$container = $this->createContainerFromFile('property_accessor');
$def = $container->getDefinition('property_accessor');
$this->assertTrue($def->getArgument(0));
$this->assertSame(PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_CALL, $def->getArgument(0));
$this->assertTrue($def->getArgument(1));
$this->assertFalse($def->getArgument(3));
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"symfony/messenger": "<4.4",
"symfony/mime": "<4.4",
"symfony/property-info": "<4.4",
"symfony/property-access": "<5.2",
"symfony/serializer": "<5.2",
"symfony/stopwatch": "<4.4",
"symfony/translation": "<5.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/PropertyAccess/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

5.2.0
-----

* deprecated passing a boolean as the first argument of `PropertyAccessor::__construct()`, expecting a combination of bitwise flags instead
* added the ability to disable usage of the magic `__get` & `__set` methods

5.1.0
-----

Expand Down
33 changes: 24 additions & 9 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@
*/
class PropertyAccessor implements PropertyAccessorInterface
{
/** @var int Allow none of the magic methods */
public const DISALLOW_MAGIC_METHODS = ReflectionExtractor::DISALLOW_MAGIC_METHODS;
/** @var int Allow magic __get methods */
public const MAGIC_GET = ReflectionExtractor::ALLOW_MAGIC_GET;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are now aliases to the ReflectionExtractor constants, since it would be unexpected/not-discoverable for the user to use ReflectionExtractor consts to configure the accessor.

/** @var int Allow magic __set methods */
public const MAGIC_SET = ReflectionExtractor::ALLOW_MAGIC_SET;
/** @var int Allow magic __call methods */
public const MAGIC_CALL = ReflectionExtractor::ALLOW_MAGIC_CALL;

private const VALUE = 0;
private const REF = 1;
private const IS_REF_CHAINED = 2;
private const CACHE_PREFIX_READ = 'r';
private const CACHE_PREFIX_WRITE = 'w';
private const CACHE_PREFIX_PROPERTY_PATH = 'p';

/**
* @var bool
*/
private $magicCall;
private $magicMethodsFlags;
private $ignoreInvalidIndices;
private $ignoreInvalidProperty;

Expand All @@ -68,18 +74,27 @@ class PropertyAccessor implements PropertyAccessorInterface
* @var PropertyWriteInfoExtractorInterface
*/
private $writeInfoExtractor;

private $readPropertyCache = [];
private $writePropertyCache = [];
private static $resultProto = [self::VALUE => null];

/**
* Should not be used by application code. Use
* {@link PropertyAccess::createPropertyAccessor()} instead.
*
* @param int $magicMethods A bitwise combination of the MAGIC_* constants
* to specify the allowed magic methods (__get, __set, __call)
* or self::DISALLOW_MAGIC_METHODS for none
*/
public function __construct(bool $magicCall = false, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null, bool $throwExceptionOnInvalidPropertyPath = true, PropertyReadInfoExtractorInterface $readInfoExtractor = null, PropertyWriteInfoExtractorInterface $writeInfoExtractor = null)
public function __construct(/*int */$magicMethods = self::MAGIC_GET | self::MAGIC_SET, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null, bool $throwExceptionOnInvalidPropertyPath = true, PropertyReadInfoExtractorInterface $readInfoExtractor = null, PropertyWriteInfoExtractorInterface $writeInfoExtractor = null)
{
$this->magicCall = $magicCall;
if (\is_bool($magicMethods)) {
trigger_deprecation('symfony/property-info', '5.2', 'Passing a boolean to "%s()" first argument is deprecated since 5.1 and expect a combination of bitwise flags instead (i.e an integer).', __METHOD__);

$magicMethods = ($magicMethods ? self::MAGIC_CALL : 0) | self::MAGIC_GET | self::MAGIC_SET;
}

$this->magicMethodsFlags = $magicMethods;
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
$this->cacheItemPool = $cacheItemPool instanceof NullAdapter ? null : $cacheItemPool; // Replace the NullAdapter by the null value
$this->ignoreInvalidProperty = !$throwExceptionOnInvalidPropertyPath;
Expand Down Expand Up @@ -472,7 +487,7 @@ private function getReadInfo(string $class, string $property): ?PropertyReadInfo

$accessor = $this->readInfoExtractor->getReadInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_call_extraction' => $this->magicCall,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
]);

Expand Down Expand Up @@ -592,7 +607,7 @@ private function getWriteInfo(string $class, string $property, $value): Property

$mutator = $this->writeInfoExtractor->getWriteInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_call_extraction' => $this->magicCall,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
'enable_adder_remover_extraction' => $useAdderAndRemover,
]);
Expand Down
87 changes: 82 additions & 5 deletions src/Symfony/Component/PropertyAccess/PropertyAccessorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
class PropertyAccessorBuilder
{
private $magicCall = false;
/** @var int */
private $magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET;
private $throwExceptionOnInvalidIndex = false;
private $throwExceptionOnInvalidPropertyPath = true;

Expand All @@ -41,14 +42,54 @@ class PropertyAccessorBuilder
*/
private $writeInfoExtractor;

/**
* Enables the use of all magic methods by the PropertyAccessor.
*/
public function enableMagicMethods(): self
{
$this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;

return $this;
}

/**
* Disable the use of all magic methods by the PropertyAccessor.
*/
public function disableMagicMethods(): self
{
$this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;

return $this;
}

/**
* Enables the use of "__call" by the PropertyAccessor.
*
* @return $this
*/
public function enableMagicCall()
{
$this->magicCall = true;
$this->magicMethods |= PropertyAccessor::MAGIC_CALL;

return $this;
}

/**
* Enables the use of "__get" by the PropertyAccessor.
*/
public function enableMagicGet(): self
{
$this->magicMethods |= PropertyAccessor::MAGIC_GET;

return $this;
}

/**
* Enables the use of "__set" by the PropertyAccessor.
*/
public function enableMagicSet(): self
{
$this->magicMethods |= PropertyAccessor::MAGIC_SET;

return $this;
}
Expand All @@ -60,7 +101,27 @@ public function enableMagicCall()
*/
public function disableMagicCall()
{
$this->magicCall = false;
$this->magicMethods ^= PropertyAccessor::MAGIC_CALL;

return $this;
}

/**
* Disables the use of "__get" by the PropertyAccessor.
*/
public function disableMagicGet(): self
{
$this->magicMethods ^= PropertyAccessor::MAGIC_GET;

return $this;
}

/**
* Disables the use of "__set" by the PropertyAccessor.
*/
public function disableMagicSet(): self
{
$this->magicMethods ^= PropertyAccessor::MAGIC_SET;

return $this;
}
Expand All @@ -70,7 +131,23 @@ public function disableMagicCall()
*/
public function isMagicCallEnabled()
{
return $this->magicCall;
return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
}

/**
* @return bool whether the use of "__get" by the PropertyAccessor is enabled
*/
public function isMagicGetEnabled(): bool
{
return $this->magicMethods & PropertyAccessor::MAGIC_GET;
}

/**
* @return bool whether the use of "__set" by the PropertyAccessor is enabled
*/
public function isMagicSetEnabled(): bool
{
return $this->magicMethods & PropertyAccessor::MAGIC_SET;
}

/**
Expand Down Expand Up @@ -206,6 +283,6 @@ public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
*/
public function getPropertyAccessor()
{
return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool, $this->throwExceptionOnInvalidPropertyPath, $this->readInfoExtractor, $this->writeInfoExtractor);
return new PropertyAccessor($this->magicMethods, $this->throwExceptionOnInvalidIndex, $this->cacheItemPool, $this->throwExceptionOnInvalidPropertyPath, $this->readInfoExtractor, $this->writeInfoExtractor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ public function testDisableMagicCall()
$this->assertSame($this->builder, $this->builder->disableMagicCall());
}

public function testIsMagicCallEnable()
public function testTogglingMagicGet()
{
$this->assertTrue($this->builder->isMagicGetEnabled());
$this->assertFalse($this->builder->disableMagicGet()->isMagicCallEnabled());
$this->assertTrue($this->builder->enableMagicGet()->isMagicGetEnabled());
}

public function testTogglingMagicSet()
{
$this->assertTrue($this->builder->isMagicSetEnabled());
$this->assertFalse($this->builder->disableMagicSet()->isMagicSetEnabled());
$this->assertTrue($this->builder->enableMagicSet()->isMagicSetEnabled());
}

public function testTogglingMagicCall()
{
$this->assertFalse($this->builder->isMagicCallEnabled());
$this->assertTrue($this->builder->enableMagicCall()->isMagicCallEnabled());
Expand Down
Loading