-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
af3a824
5b82237
00a26eb
e4aaac9
e46df98
5f206e6
94e6157
09707ad
d02d93a
748b894
688cbda
003efdb
dc5cacb
1a35d45
1e29523
7c6a79c
d0bd777
6b7eeff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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}. | ||
|
@@ -168,7 +168,7 @@ class PropertyAccessor implements PropertyAccessorInterface | |
* @param CacheItemPoolInterface $cacheItemPool | ||
* @param ClassMetadataFactoryInterface $classMetadataFactory | ||
*/ | ||
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; | ||
|
@@ -555,6 +555,7 @@ private function getReadAccessInfo($class, $property) | |
} | ||
} | ||
|
||
/** @var $metadata */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useless |
||
$metadata = null; | ||
$access = array(); | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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) { | ||
|
@@ -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]; | ||
|
@@ -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)) { | ||
|
@@ -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, | ||
|
@@ -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)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,7 +207,7 @@ public function testSetValueCallsCustomAdderAndRemoverForCollections() | |
$car = new PropertyAccessorCollectionTest_Car($axesBefore); | ||
|
||
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
@@ -229,7 +229,7 @@ public function testSetValueCallsCustomAdderAndRemoverForCollectionsMethodAnnota | |
$car = new PropertyAccessorCollectionTest_Car($axesBefore); | ||
|
||
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
"doctrine/annotations": "~1.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be added to |
||
"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." | ||
|
There was a problem hiding this comment.
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
)