[PropertyAccess] Port of the performance optimization from 2.3
This commit is contained in:
parent
399b1d5cb0
commit
aa4cc90a87
@ -20,12 +20,24 @@ use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
|
|||||||
* Default implementation of {@link PropertyAccessorInterface}.
|
* Default implementation of {@link PropertyAccessorInterface}.
|
||||||
*
|
*
|
||||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||||
|
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||||
*/
|
*/
|
||||||
class PropertyAccessor implements PropertyAccessorInterface
|
class PropertyAccessor implements PropertyAccessorInterface
|
||||||
{
|
{
|
||||||
const VALUE = 0;
|
const VALUE = 0;
|
||||||
const IS_REF = 1;
|
const IS_REF = 1;
|
||||||
const IS_REF_CHAINED = 2;
|
const IS_REF_CHAINED = 2;
|
||||||
|
const ACCESS_HAS_PROPERTY = 0;
|
||||||
|
const ACCESS_TYPE = 1;
|
||||||
|
const ACCESS_NAME = 2;
|
||||||
|
const ACCESS_REF = 3;
|
||||||
|
const ACCESS_ADDER = 4;
|
||||||
|
const ACCESS_REMOVER = 5;
|
||||||
|
const ACCESS_TYPE_METHOD = 0;
|
||||||
|
const ACCESS_TYPE_PROPERTY = 1;
|
||||||
|
const ACCESS_TYPE_MAGIC = 2;
|
||||||
|
const ACCESS_TYPE_ADDER_AND_REMOVER = 3;
|
||||||
|
const ACCESS_TYPE_NOT_FOUND = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
@ -37,6 +49,16 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
*/
|
*/
|
||||||
private $ignoreInvalidIndices;
|
private $ignoreInvalidIndices;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $readPropertyCache = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $writePropertyCache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should not be used by application code. Use
|
* Should not be used by application code. Use
|
||||||
* {@link PropertyAccess::createPropertyAccessor()} instead.
|
* {@link PropertyAccess::createPropertyAccessor()} instead.
|
||||||
@ -330,51 +352,31 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%s]" instead.', $property, $property));
|
throw new NoSuchPropertyException(sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%s]" instead.', $property, $property));
|
||||||
}
|
}
|
||||||
|
|
||||||
$camelized = $this->camelize($property);
|
$access = $this->getReadAccessInfo($object, $property);
|
||||||
$reflClass = new \ReflectionClass($object);
|
|
||||||
$getter = 'get'.$camelized;
|
|
||||||
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
|
|
||||||
$isser = 'is'.$camelized;
|
|
||||||
$hasser = 'has'.$camelized;
|
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
if (self::ACCESS_TYPE_METHOD === $access[self::ACCESS_TYPE]) {
|
||||||
$result[self::VALUE] = $object->$getter();
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
|
||||||
} elseif ($this->isMethodAccessible($reflClass, $getsetter, 0)) {
|
} elseif (self::ACCESS_TYPE_PROPERTY === $access[self::ACCESS_TYPE]) {
|
||||||
$result[self::VALUE] = $object->$getsetter();
|
if ($access[self::ACCESS_REF]) {
|
||||||
} elseif ($reflClass->hasMethod($isser) && $reflClass->getMethod($isser)->isPublic()) {
|
$result[self::VALUE] = &$object->{$access[self::ACCESS_NAME]};
|
||||||
$result[self::VALUE] = $object->$isser();
|
|
||||||
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
|
|
||||||
$result[self::VALUE] = $object->$hasser();
|
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
|
||||||
$result[self::VALUE] = &$object->$property;
|
|
||||||
$result[self::IS_REF] = true;
|
$result[self::IS_REF] = true;
|
||||||
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
|
} else {
|
||||||
$result[self::VALUE] = $object->$property;
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]};
|
||||||
} elseif (!$classHasProperty && property_exists($object, $property)) {
|
}
|
||||||
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $classHasProperty, otherwise if in the previous clause
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
|
|
||||||
$result[self::VALUE] = &$object->$property;
|
$result[self::VALUE] = &$object->$property;
|
||||||
$result[self::IS_REF] = true;
|
$result[self::IS_REF] = true;
|
||||||
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
|
||||||
// we call the getter and hope the __call do the job
|
// we call the getter and hope the __call do the job
|
||||||
$result[self::VALUE] = $object->$getter();
|
$result[self::VALUE] = $object->{$access[self::ACCESS_NAME]}();
|
||||||
} else {
|
} else {
|
||||||
$methods = array($getter, $getsetter, $isser, $hasser, '__get');
|
throw new NoSuchPropertyException($access[self::ACCESS_NAME]);
|
||||||
if ($this->magicCall) {
|
|
||||||
$methods[] = '__call';
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NoSuchPropertyException(sprintf(
|
|
||||||
'Neither the property "%s" nor one of the methods "%s()" '.
|
|
||||||
'exist and have public access in class "%s".',
|
|
||||||
$property,
|
|
||||||
implode('()", "', $methods),
|
|
||||||
$reflClass->name
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Objects are always passed around by reference
|
// Objects are always passed around by reference
|
||||||
@ -385,6 +387,81 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guesses how to read the property value.
|
||||||
|
*
|
||||||
|
* @param string $object
|
||||||
|
* @param string $property
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getReadAccessInfo($object, $property)
|
||||||
|
{
|
||||||
|
$key = get_class($object).'::'.$property;
|
||||||
|
|
||||||
|
if (isset($this->readPropertyCache[$key])) {
|
||||||
|
$access = $this->readPropertyCache[$key];
|
||||||
|
} else {
|
||||||
|
$access = array();
|
||||||
|
|
||||||
|
$reflClass = new \ReflectionClass($object);
|
||||||
|
$access[self::ACCESS_HAS_PROPERTY] = $reflClass->hasProperty($property);
|
||||||
|
$camelProp = $this->camelize($property);
|
||||||
|
$getter = 'get'.$camelProp;
|
||||||
|
$getsetter = lcfirst($camelProp); // jQuery style, e.g. read: last(), write: last($item)
|
||||||
|
$isser = 'is'.$camelProp;
|
||||||
|
$hasser = 'has'.$camelProp;
|
||||||
|
$classHasProperty = $reflClass->hasProperty($property);
|
||||||
|
|
||||||
|
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $getter;
|
||||||
|
} elseif ($reflClass->hasMethod($getsetter) && $reflClass->getMethod($getsetter)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $getsetter;
|
||||||
|
} elseif ($reflClass->hasMethod($isser) && $reflClass->getMethod($isser)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $isser;
|
||||||
|
} elseif ($reflClass->hasMethod($hasser) && $reflClass->getMethod($hasser)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $hasser;
|
||||||
|
} elseif ($reflClass->hasMethod('__get') && $reflClass->getMethod('__get')->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
$access[self::ACCESS_REF] = false;
|
||||||
|
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
$access[self::ACCESS_REF] = true;
|
||||||
|
|
||||||
|
$result[self::VALUE] = &$object->$property;
|
||||||
|
$result[self::IS_REF] = true;
|
||||||
|
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
||||||
|
// we call the getter and hope the __call do the job
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
||||||
|
$access[self::ACCESS_NAME] = $getter;
|
||||||
|
} else {
|
||||||
|
$methods = array($getter, $getsetter, $isser, $hasser, '__get');
|
||||||
|
if ($this->magicCall) {
|
||||||
|
$methods[] = '__call';
|
||||||
|
}
|
||||||
|
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
|
||||||
|
$access[self::ACCESS_NAME] = sprintf(
|
||||||
|
'Neither the property "%s" nor one of the methods "%s()" '.
|
||||||
|
'exist and have public access in class "%s".',
|
||||||
|
$property,
|
||||||
|
implode('()", "', $methods),
|
||||||
|
$reflClass->name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->readPropertyCache[$key] = $access;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $access;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of an index in a given array-accessible value.
|
* Sets the value of an index in a given array-accessible value.
|
||||||
*
|
*
|
||||||
@ -419,55 +496,26 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
throw new NoSuchPropertyException(sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%s]" instead?', $property, $property));
|
||||||
}
|
}
|
||||||
|
|
||||||
$reflClass = new \ReflectionClass($object);
|
$access = $this->getWriteAccessInfo($object, $property, $value);
|
||||||
$camelized = $this->camelize($property);
|
|
||||||
$singulars = (array) StringUtil::singularify($camelized);
|
|
||||||
|
|
||||||
if (is_array($value) || $value instanceof \Traversable) {
|
if (self::ACCESS_TYPE_METHOD === $access[self::ACCESS_TYPE]) {
|
||||||
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
$object->{$access[self::ACCESS_NAME]}($value);
|
||||||
|
} elseif (self::ACCESS_TYPE_PROPERTY === $access[self::ACCESS_TYPE]) {
|
||||||
// Use addXxx() and removeXxx() to write the collection
|
$object->{$access[self::ACCESS_NAME]} = $value;
|
||||||
if (null !== $methods) {
|
} elseif (self::ACCESS_TYPE_ADDER_AND_REMOVER === $access[self::ACCESS_TYPE]) {
|
||||||
$this->writeCollection($object, $property, $value, $methods[0], $methods[1]);
|
$this->writeCollection($object, $property, $value, $access[self::ACCESS_ADDER], $access[self::ACCESS_REMOVER]);
|
||||||
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$setter = 'set'.$camelized;
|
|
||||||
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
|
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($this->isMethodAccessible($reflClass, $setter, 1)) {
|
|
||||||
$object->$setter($value);
|
|
||||||
} elseif ($this->isMethodAccessible($reflClass, $getsetter, 1)) {
|
|
||||||
$object->$getsetter($value);
|
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
|
||||||
$object->$property = $value;
|
|
||||||
} elseif ($this->isMethodAccessible($reflClass, '__set', 2)) {
|
|
||||||
$object->$property = $value;
|
|
||||||
} elseif (!$classHasProperty && property_exists($object, $property)) {
|
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $classHasProperty, otherwise if in the previous clause
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
|
|
||||||
$object->$property = $value;
|
$object->$property = $value;
|
||||||
} elseif ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2)) {
|
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
|
||||||
// we call the getter and hope the __call do the job
|
$object->{$access[self::ACCESS_NAME]}($value);
|
||||||
$object->$setter($value);
|
|
||||||
} else {
|
} else {
|
||||||
throw new NoSuchPropertyException(sprintf(
|
throw new NoSuchPropertyException($access[self::ACCESS_NAME]);
|
||||||
'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.'()", ';
|
|
||||||
}, $singulars)),
|
|
||||||
$setter,
|
|
||||||
$getsetter,
|
|
||||||
$reflClass->name
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -519,6 +567,90 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guesses how to write the property value.
|
||||||
|
*
|
||||||
|
* @param string $object
|
||||||
|
* @param string $property
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getWriteAccessInfo($object, $property, $value)
|
||||||
|
{
|
||||||
|
$key = get_class($object).'::'.$property;
|
||||||
|
$guessedAdders = '';
|
||||||
|
|
||||||
|
if (isset($this->writePropertyCache[$key])) {
|
||||||
|
$access = $this->writePropertyCache[$key];
|
||||||
|
} else {
|
||||||
|
$access = array();
|
||||||
|
|
||||||
|
$reflClass = new \ReflectionClass($object);
|
||||||
|
$access[self::ACCESS_HAS_PROPERTY] = $reflClass->hasProperty($property);
|
||||||
|
$camelized = $this->camelize($property);
|
||||||
|
$singulars = (array) StringUtil::singularify($camelized);
|
||||||
|
|
||||||
|
if (is_array($value) || $value instanceof \Traversable) {
|
||||||
|
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
||||||
|
|
||||||
|
if (null === $methods) {
|
||||||
|
// It is sufficient to include only the adders in the error
|
||||||
|
// message. If the user implements the adder but not the remover,
|
||||||
|
// an exception will be thrown in findAdderAndRemover() that
|
||||||
|
// the remover has to be implemented as well.
|
||||||
|
$guessedAdders = '"add'.implode('()", "add', $singulars).'()", ';
|
||||||
|
} else {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
|
||||||
|
$access[self::ACCESS_ADDER] = $methods[0];
|
||||||
|
$access[self::ACCESS_REMOVER] = $methods[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($access[self::ACCESS_TYPE])) {
|
||||||
|
$setter = 'set'.$this->camelize($property);
|
||||||
|
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
|
||||||
|
|
||||||
|
$classHasProperty = $reflClass->hasProperty($property);
|
||||||
|
|
||||||
|
if ($this->isMethodAccessible($reflClass, $setter, 1)) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $setter;
|
||||||
|
} elseif ($this->isMethodAccessible($reflClass, $getsetter, 1)) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
|
$access[self::ACCESS_NAME] = $getsetter;
|
||||||
|
} elseif ($this->isMethodAccessible($reflClass, '__set', 2)) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
|
$access[self::ACCESS_NAME] = $property;
|
||||||
|
} elseif ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2)) {
|
||||||
|
// we call the getter and hope the __call do the job
|
||||||
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
||||||
|
$access[self::ACCESS_NAME] = $setter;
|
||||||
|
} 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()", '.
|
||||||
|
'"__set()" or "__call()" exist and have public access in class "%s".',
|
||||||
|
$property,
|
||||||
|
implode('', array_map(function ($singular) {
|
||||||
|
return '"add'.$singular.'()"/"remove'.$singular.'()", ';
|
||||||
|
}, $singulars)),
|
||||||
|
$setter,
|
||||||
|
$getsetter,
|
||||||
|
$reflClass->name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->writePropertyCache[$key] = $access;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $access;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether a property is writable in the given object.
|
* Returns whether a property is writable in the given object.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user