Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PropertyAccess] Allow custom methods on property accesses #18016

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 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Minor rebase fixes
  • Loading branch information
lrlopez committed Jul 3, 2016
commit 1a35d4538c9304f969813c93af28e3b5f28b2186
24 changes: 13 additions & 11 deletions 24 src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\PropertyAccess;

use Doctrine\Common\Annotations\AnnotationReader;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
Expand All @@ -24,6 +23,7 @@
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Mapping\Factory\MetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;

/**
* Default implementation of {@link PropertyAccessorInterface}.
Expand Down Expand Up @@ -168,7 +168,7 @@ class PropertyAccessor implements PropertyAccessorInterface
* @param CacheItemPoolInterface $cacheItemPool
* @param ClassMetadataFactoryInterface $classMetadataFactory
Copy link
Contributor

Choose a reason for hiding this comment

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

The type hint is not same (MetadataFactoryInterface|null)

*/
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false, MetadataFactoryInterface $classMetadataFactory = null)
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null, MetadataFactoryInterface $classMetadataFactory = null)
{
$this->magicCall = $magicCall;
$this->ignoreInvalidIndices = !$throwExceptionOnInvalidIndex;
Expand Down Expand Up @@ -555,6 +555,7 @@ private function getReadAccessInfo($class, $property)
}
}

/** @var $metadata */
Copy link
Contributor

Choose a reason for hiding this comment

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

useless

$metadata = null;
$access = array();

Expand All @@ -563,7 +564,7 @@ private function getReadAccessInfo($class, $property)
$access[self::ACCESS_HAS_PROPERTY] = $hasProperty;

if ($this->classMetadataFactory) {
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertiesMetadata();
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertyMetadataCollection();
$metadata = isset($metadata[$property]) ? $metadata[$property] : null;
}

Expand Down Expand Up @@ -754,7 +755,7 @@ private function getWriteAccessInfo($class, $property, $value)
$done = false;

if ($this->classMetadataFactory) {
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertiesMetadata();
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertyMetadataCollection();
$metadata = isset($metadata[$property]) ? $metadata[$property] : null;

if ($metadata) {
Expand All @@ -773,11 +774,11 @@ private function getWriteAccessInfo($class, $property, $value)

if (!$done) {
$camelized = $this->camelize($property);
$singulars = (array) Inflector::singularize($camelized);
$singulars = (array)Inflector::singularize($camelized);

if ($traversable) {
$methods = $this->findAdderAndRemover($reflClass, $singulars);

if (null !== $methods) {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
$access[self::ACCESS_ADDER] = $methods[0];
Expand All @@ -786,7 +787,7 @@ private function getWriteAccessInfo($class, $property, $value)
}

if (!isset($access[self::ACCESS_TYPE])) {
$setter = 'set'.$camelized;
$setter = 'set' . $camelized;
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)

if ($this->isMethodAccessible($reflClass, $setter, 1)) {
Expand All @@ -808,8 +809,8 @@ private function getWriteAccessInfo($class, $property, $value)
} elseif (null !== $methods = $this->findAdderAndRemover($reflClass, $singulars)) {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
'the new value must be an array or an instance of \Traversable, '.
'The property "%s" in class "%s" can be defined with the methods "%s()" but ' .
'the new value must be an array or an instance of \Traversable, ' .
'"%s" given.',
$property,
$reflClass->name,
Expand All @@ -819,18 +820,19 @@ private function getWriteAccessInfo($class, $property, $value)
} else {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
'Neither the property "%s" nor one of the methods %s"%s()", "%s()", '.
'Neither the property "%s" nor one of the methods %s"%s()", "%s()", ' .
'"__set()" or "__call()" exist and have public access in class "%s".',
$property,
implode('', array_map(function ($singular) {
return '"add'.$singular.'()"/"remove'.$singular.'()", ';
return '"add' . $singular . '()"/"remove' . $singular . '()", ';
}, $singulars)),
$setter,
$getsetter,
$reflClass->name
);
}
}
}

if (isset($item)) {
$this->cacheItemPool->save($item->set($access));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function testSetValueCallsCustomAdderAndRemoverForCollections()
$car = new PropertyAccessorCollectionTest_Car($axesBefore);

AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
Copy link
Contributor

Choose a reason for hiding this comment

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

annotations should already be loaded

$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$this->propertyAccessor->setValue($car, 'customAxes', $axesMerged);

Expand All @@ -229,7 +229,7 @@ public function testSetValueCallsCustomAdderAndRemoverForCollectionsMethodAnnota
$car = new PropertyAccessorCollectionTest_Car($axesBefore);

AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$this->propertyAccessor->setValue($car, 'customVirtualAxes', $axesMerged);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ public function testGetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $p
public function testGetWithCustomGetter()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->assertSame('webmozart', $this->propertyAccessor->getValue(new TestClass('webmozart'), 'customGetterSetter'));
}

public function testGetWithCustomGetterMethodAnnotation()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->assertSame(200, $this->propertyAccessor->getValue(new TestClass('webmozart', 10, 20), 'total'));
}

Expand Down Expand Up @@ -319,7 +319,7 @@ public function testSetValueThrowsExceptionIfNotObjectOrArray($objectOrArray, $p
public function testSetValueWithCustomSetter()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$custom = new TestClass('webmozart');

Expand All @@ -331,7 +331,7 @@ public function testSetValueWithCustomSetter()
public function testSetValueWithCustomSetterMethodAnnotation()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->propertyAccessor = new PropertyAccessor(false, false, null, new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$custom = new TestClass('webmozart', 10, 20);

Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/PropertyAccess/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"doctrine/annotations": "~1.2",
Copy link
Contributor

@GuilhemN GuilhemN Jul 21, 2016

Choose a reason for hiding this comment

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

shouldn't this be added to suggest ?

"symfony/cache": "~3.1",
"symfony/config": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0",
"symfony/yaml": "~2.8|~3.0"
},
"suggest": {
"psr/cache-implementation": "To cache access methods."
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.