1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyAccess;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyReadInfo;
use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyWriteInfo;
use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
/**
* Default implementation of {@link PropertyAccessorInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
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;
/** @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;
public const DO_NOT_THROW = 0;
public const THROW_ON_INVALID_INDEX = 1;
public const THROW_ON_INVALID_PROPERTY_PATH = 2;
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';
private const RESULT_PROTO = [self::VALUE => null];
private bool $ignoreInvalidIndices;
private bool $ignoreInvalidProperty;
private ?CacheItemPoolInterface $cacheItemPool;
private array $propertyPathCache = [];
private PropertyReadInfoExtractorInterface $readInfoExtractor;
private PropertyWriteInfoExtractorInterface $writeInfoExtractor;
private array $readPropertyCache = [];
private array $writePropertyCache = [];
/**
* Should not be used by application code. Use
* {@link PropertyAccess::createPropertyAccessor()} instead.
*
* @param int $magicMethodsFlags A bitwise combination of the MAGIC_* constants
* to specify the allowed magic methods (__get, __set, __call)
* or self::DISALLOW_MAGIC_METHODS for none
* @param int $throw A bitwise combination of the THROW_* constants
* to specify when exceptions should be thrown
*/
public function __construct(
private int $magicMethodsFlags = self::MAGIC_GET | self::MAGIC_SET,
int $throw = self::THROW_ON_INVALID_PROPERTY_PATH,
?CacheItemPoolInterface $cacheItemPool = null,
?PropertyReadInfoExtractorInterface $readInfoExtractor = null,
?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null,
) {
$this->ignoreInvalidIndices = 0 === ($throw & self::THROW_ON_INVALID_INDEX);
$this->cacheItemPool = $cacheItemPool instanceof NullAdapter ? null : $cacheItemPool; // Replace the NullAdapter by the null value
$this->ignoreInvalidProperty = 0 === ($throw & self::THROW_ON_INVALID_PROPERTY_PATH);
$this->readInfoExtractor = $readInfoExtractor ?? new ReflectionExtractor([], null, null, false);
$this->writeInfoExtractor = $writeInfoExtractor ?? new ReflectionExtractor(['set'], null, null, false);
}
public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed
{
$zval = [
self::VALUE => $objectOrArray,
];
if (\is_object($objectOrArray) && (false === strpbrk((string) $propertyPath, '.[?') || $objectOrArray instanceof \stdClass && property_exists($objectOrArray, $propertyPath))) {
return $this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty)[self::VALUE];
}
$propertyPath = $this->getPropertyPath($propertyPath);
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
return $propertyValues[\count($propertyValues) - 1][self::VALUE];
}
public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value): void
{
if (\is_object($objectOrArray) && (false === strpbrk((string) $propertyPath, '.[') || $objectOrArray instanceof \stdClass && property_exists($objectOrArray, $propertyPath))) {
$zval = [
self::VALUE => $objectOrArray,
];
try {
$this->writeProperty($zval, $propertyPath, $value);
return;
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath, $e);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
$propertyPath = $this->getPropertyPath($propertyPath);
$zval = [
self::VALUE => $objectOrArray,
self::REF => &$objectOrArray,
];
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);
$overwrite = true;
try {
for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {
$zval = $propertyValues[$i];
unset($propertyValues[$i]);
// You only need set value for current element if:
// 1. it's the parent of the last index element
// OR
// 2. its child is not passed by reference
//
// This may avoid unnecessary value setting process for array elements.
// For example:
// '[a][b][c]' => 'old-value'
// If you want to change its value to 'new-value',
// you only need set value for '[a][b][c]' and it's safe to ignore '[a][b]' and '[a]'
if ($overwrite) {
$property = $propertyPath->getElement($i);
if ($propertyPath->isIndex($i)) {
if ($overwrite = !isset($zval[self::REF])) {
$ref = &$zval[self::REF];
$ref = $zval[self::VALUE];
}
$this->writeIndex($zval, $property, $value);
if ($overwrite) {
$zval[self::VALUE] = $zval[self::REF];
}
} else {
$this->writeProperty($zval, $property, $value);
}
// if current element is an object
// OR
// if current element's reference chain is not broken - current element
// as well as all its ancients in the property path are all passed by reference,
// then there is no need to continue the value setting process
if (\is_object($zval[self::VALUE]) || isset($zval[self::IS_REF_CHAINED])) {
break;
}
}
$value = $zval[self::VALUE];
}
} catch (\TypeError $e) {
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath, $e);
// It wasn't thrown in this class so rethrow it
throw $e;
}
}
private static function throwInvalidArgumentException(string $message, array $trace, int $i, string $propertyPath, ?\Throwable $previous = null): void
{
if (!isset($trace[$i]['file']) || __FILE__ !== $trace[$i]['file']) {
return;
}
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;
throw new InvalidTypeException($expectedType, $actualType, $propertyPath, $previous);
}
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
[, $actualType, $expectedType] = $matches;
throw new InvalidTypeException($expectedType, $actualType, $propertyPath, $previous);
}
}
public function isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
{
if (!$propertyPath instanceof PropertyPathInterface) {
$propertyPath = new PropertyPath($propertyPath);
}
try {
$zval = [
self::VALUE => $objectOrArray,
];
// handle stdClass with properties with a dot in the name
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
} else {
$this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
}
return true;
} catch (AccessException|UnexpectedTypeException) {
return false;
}
}
public function isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
{
$propertyPath = $this->getPropertyPath($propertyPath);
try {
$zval = [
self::VALUE => $objectOrArray,
];
// handle stdClass with properties with a dot in the name
if ($objectOrArray instanceof \stdClass && str_contains($propertyPath, '.') && property_exists($objectOrArray, $propertyPath)) {
$this->readProperty($zval, $propertyPath, $this->ignoreInvalidProperty);
return true;
}
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength() - 1);
for ($i = \count($propertyValues) - 1; 0 <= $i; --$i) {
$zval = $propertyValues[$i];
unset($propertyValues[$i]);
if ($propertyPath->isIndex($i)) {
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
return false;
}
} elseif (!\is_object($zval[self::VALUE]) || !$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))) {
return false;
}
if (\is_object($zval[self::VALUE])) {
return true;
}
}
return true;
} catch (AccessException|UnexpectedTypeException) {
return false;
}
}
/**
* Reads the path from an object up to a given path index.
*
* @throws UnexpectedTypeException if a value within the path is neither object nor array
* @throws NoSuchIndexException If a non-existing index is accessed
*/
private function readPropertiesUntil(array $zval, PropertyPathInterface $propertyPath, int $lastIndex, bool $ignoreInvalidIndices = true): array
{
if (!\is_object($zval[self::VALUE]) && !\is_array($zval[self::VALUE])) {
throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, 0);
}
// Add the root object to the list
$propertyValues = [$zval];
for ($i = 0; $i < $lastIndex; ++$i) {
$property = $propertyPath->getElement($i);
$isIndex = $propertyPath->isIndex($i);
$isNullSafe = $propertyPath->isNullSafe($i);
if ($isIndex) {
// Create missing nested arrays on demand
if (($zval[self::VALUE] instanceof \ArrayAccess && !$zval[self::VALUE]->offsetExists($property))
|| (\is_array($zval[self::VALUE]) && !isset($zval[self::VALUE][$property]) && !\array_key_exists($property, $zval[self::VALUE]))
) {
if (!$ignoreInvalidIndices && !$isNullSafe) {
if (!\is_array($zval[self::VALUE])) {
if (!$zval[self::VALUE] instanceof \Traversable) {
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" while trying to traverse path "%s".', $property, (string) $propertyPath));
}
$zval[self::VALUE] = iterator_to_array($zval[self::VALUE]);
}
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" while trying to traverse path "%s". Available indices are "%s".', $property, (string) $propertyPath, print_r(array_keys($zval[self::VALUE]), true)));
}
if ($i + 1 < $propertyPath->getLength()) {
if (isset($zval[self::REF])) {
$zval[self::VALUE][$property] = [];
$zval[self::REF] = $zval[self::VALUE];
} else {
$zval[self::VALUE] = [$property => []];
}
}
}
$zval = $this->readIndex($zval, $property);
} elseif ($isNullSafe && !\is_object($zval[self::VALUE])) {
$zval[self::VALUE] = null;
} else {
$zval = $this->readProperty($zval, $property, $this->ignoreInvalidProperty, $isNullSafe);
}
// the final value of the path must not be validated
if ($i + 1 < $propertyPath->getLength() && !\is_object($zval[self::VALUE]) && !\is_array($zval[self::VALUE]) && !$isNullSafe) {
throw new UnexpectedTypeException($zval[self::VALUE], $propertyPath, $i + 1);
}
if (isset($zval[self::REF]) && (0 === $i || isset($propertyValues[$i - 1][self::IS_REF_CHAINED]))) {
// Set the IS_REF_CHAINED flag to true if:
// current property is passed by reference and
// it is the first element in the property path or
// the IS_REF_CHAINED flag of its parent element is true
// Basically, this flag is true only when the reference chain from the top element to current element is not broken
$zval[self::IS_REF_CHAINED] = true;
}
$propertyValues[] = $zval;
if ($isNullSafe && null === $zval[self::VALUE]) {
break;
}
}
return $propertyValues;
}
/**
* Reads a key from an array-like structure.
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function readIndex(array $zval, string|int $index): array
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(\sprintf('Cannot read index "%s" from object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$result = self::RESULT_PROTO;
if (isset($zval[self::VALUE][$index])) {
$result[self::VALUE] = $zval[self::VALUE][$index];
if (!isset($zval[self::REF])) {
// Save creating references when doing read-only lookups
} elseif (\is_array($zval[self::VALUE])) {
$result[self::REF] = &$zval[self::REF][$index];
} elseif (\is_object($result[self::VALUE])) {
$result[self::REF] = $result[self::VALUE];
}
}
return $result;
}
/**
* Reads the value of a property from an object.
*
* @throws NoSuchPropertyException If $ignoreInvalidProperty is false and the property does not exist or is not public
*/
private function readProperty(array $zval, string $property, bool $ignoreInvalidProperty = false, bool $isNullSafe = false): array
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(\sprintf('Cannot read property "%s" from an array. Maybe you intended to write the property path as "[%1$s]" instead.', $property));
}
$result = self::RESULT_PROTO;
$object = $zval[self::VALUE];
$class = $object::class;
$access = $this->getReadInfo($class, $property);
if (null !== $access) {
$name = $access->getName();
$type = $access->getType();
try {
if (PropertyReadInfo::TYPE_METHOD === $type) {
try {
$result[self::VALUE] = $object->$name();
} catch (\TypeError $e) {
[$trace] = $e->getTrace();
// handle uninitialized properties in PHP >= 7
if (__FILE__ === ($trace['file'] ?? null)
&& $name === $trace['function']
&& $object instanceof $trace['class']
&& preg_match('/Return value (?:of .*::\w+\(\) )?must be of (?:the )?type (\w+), null returned$/', $e->getMessage(), $matches)
) {
throw new UninitializedPropertyException(\sprintf('The method "%s::%s()" returned "null", but expected type "%3$s". Did you forget to initialize a property or to make the return type nullable using "?%3$s"?', get_debug_type($object), $name, $matches[1]), 0, $e);
}
throw $e;
}
} elseif (PropertyReadInfo::TYPE_PROPERTY === $type) {
if (!isset($object->$name) && !\array_key_exists($name, (array) $object)) {
try {
$r = new \ReflectionProperty($class, $name);
if ($r->isPublic() && !$r->hasType()) {
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not initialized.', $class, $name));
}
} catch (\ReflectionException $e) {
if (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(\sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
}
}
}
$result[self::VALUE] = $object->$name;
if (isset($zval[self::REF]) && $access->canBeReference()) {
$result[self::REF] = &$object->$name;
}
}
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches) || preg_match('/^Cannot access uninitialized non-nullable property ([\w\\\\@]+)::\$(\w+) by reference$/', $e->getMessage(), $matches)) {
$r = new \ReflectionProperty(str_contains($matches[1], '@anonymous') ? $class : $matches[1], $matches[2]);
$type = ($type = $r->getType()) instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not readable because it is typed "%s". You should initialize it or declare a default value instead.', $matches[1], $r->getName(), $type), 0, $e);
}
throw $e;
}
} elseif (property_exists($object, $property) && \array_key_exists($property, (array) $object)) {
$result[self::VALUE] = $object->$property;
if (isset($zval[self::REF])) {
$result[self::REF] = &$object->$property;
}
} elseif ($isNullSafe) {
$result[self::VALUE] = null;
} elseif (!$ignoreInvalidProperty) {
throw new NoSuchPropertyException(\sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class));
}
// Objects are always passed around by reference
if (isset($zval[self::REF]) && \is_object($result[self::VALUE])) {
$result[self::REF] = $result[self::VALUE];
}
return $result;
}
/**
* Guesses how to read the property value.
*/
private function getReadInfo(string $class, string $property): ?PropertyReadInfo
{
$key = str_replace('\\', '.', $class).'..'.$property;
if (isset($this->readPropertyCache[$key])) {
return $this->readPropertyCache[$key];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_READ.rawurlencode($key));
if ($item->isHit()) {
return $this->readPropertyCache[$key] = $item->get();
}
}
$accessor = $this->readInfoExtractor->getReadInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
]);
if (isset($item)) {
$this->cacheItemPool->save($item->set($accessor));
}
return $this->readPropertyCache[$key] = $accessor;
}
/**
* Sets the value of an index in a given array-accessible value.
*
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
*/
private function writeIndex(array $zval, string|int $index, mixed $value): void
{
if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) {
throw new NoSuchIndexException(\sprintf('Cannot modify index "%s" in object of type "%s" because it doesn\'t implement \ArrayAccess.', $index, get_debug_type($zval[self::VALUE])));
}
$zval[self::REF][$index] = $value;
}
/**
* Sets the value of a property in the given object.
*
* @throws NoSuchPropertyException if the property does not exist or is not public
*/
private function writeProperty(array $zval, string $property, mixed $value, bool $recursive = false): void
{
if (!\is_object($zval[self::VALUE])) {
throw new NoSuchPropertyException(\sprintf('Cannot write property "%s" to an array. Maybe you should write the property path as "[%1$s]" instead?', $property));
}
$object = $zval[self::VALUE];
$class = $object::class;
$mutator = $this->getWriteInfo($class, $property, $value);
try {
if (PropertyWriteInfo::TYPE_NONE !== $mutator->getType()) {
$type = $mutator->getType();
if (PropertyWriteInfo::TYPE_METHOD === $type) {
$object->{$mutator->getName()}($value);
} elseif (PropertyWriteInfo::TYPE_PROPERTY === $type) {
$object->{$mutator->getName()} = $value;
} elseif (PropertyWriteInfo::TYPE_ADDER_AND_REMOVER === $type) {
$this->writeCollection($zval, $property, $value, $mutator->getAdderInfo(), $mutator->getRemoverInfo());
}
} elseif ($object instanceof \stdClass && property_exists($object, $property)) {
$object->$property = $value;
} elseif (!$this->ignoreInvalidProperty) {
if ($mutator->hasErrors()) {
throw new NoSuchPropertyException(implode('. ', $mutator->getErrors()).'.');
}
throw new NoSuchPropertyException(\sprintf('Could not determine access type for property "%s" in class "%s".', $property, get_debug_type($object)));
}
} catch (\TypeError $e) {
if ($recursive || !$value instanceof \DateTimeInterface || !\in_array($value::class, ['DateTime', 'DateTimeImmutable'], true) || __FILE__ !== ($e->getTrace()[0]['file'] ?? null)) {
throw $e;
}
$value = $value instanceof \DateTimeImmutable ? \DateTime::createFromImmutable($value) : \DateTimeImmutable::createFromMutable($value);
try {
$this->writeProperty($zval, $property, $value, true);
} catch (\TypeError) {
throw $e; // throw the previous error
}
}
}
/**
* Adjusts a collection-valued property by calling add*() and remove*() methods.
*/
private function writeCollection(array $zval, string $property, iterable $collection, PropertyWriteInfo $addMethod, PropertyWriteInfo $removeMethod): void
{
// At this point the add and remove methods have been found
$previousValue = $this->readProperty($zval, $property);
$previousValue = $previousValue[self::VALUE];
$removeMethodName = $removeMethod->getName();
$addMethodName = $addMethod->getName();
if ($previousValue instanceof \Traversable) {
$previousValue = iterator_to_array($previousValue);
}
if ($previousValue && \is_array($previousValue)) {
if (\is_object($collection)) {
$collection = iterator_to_array($collection);
}
foreach ($previousValue as $key => $item) {
if (!\in_array($item, $collection, true)) {
unset($previousValue[$key]);
$zval[self::VALUE]->$removeMethodName($item);
}
}
} else {
$previousValue = false;
}
foreach ($collection as $item) {
if (!$previousValue || !\in_array($item, $previousValue, true)) {
$zval[self::VALUE]->$addMethodName($item);
}
}
}
private function getWriteInfo(string $class, string $property, mixed $value): PropertyWriteInfo
{
$useAdderAndRemover = is_iterable($value);
$key = str_replace('\\', '.', $class).'..'.$property.'..'.(int) $useAdderAndRemover;
if (isset($this->writePropertyCache[$key])) {
return $this->writePropertyCache[$key];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_WRITE.rawurlencode($key));
if ($item->isHit()) {
return $this->writePropertyCache[$key] = $item->get();
}
}
$mutator = $this->writeInfoExtractor->getWriteInfo($class, $property, [
'enable_getter_setter_extraction' => true,
'enable_magic_methods_extraction' => $this->magicMethodsFlags,
'enable_constructor_extraction' => false,
'enable_adder_remover_extraction' => $useAdderAndRemover,
]);
if (isset($item)) {
$this->cacheItemPool->save($item->set($mutator));
}
return $this->writePropertyCache[$key] = $mutator;
}
/**
* Returns whether a property is writable in the given object.
*/
private function isPropertyWritable(object $object, string $property): bool
{
if ($object instanceof \stdClass && property_exists($object, $property)) {
return true;
}
$mutatorForArray = $this->getWriteInfo($object::class, $property, []);
if (PropertyWriteInfo::TYPE_PROPERTY === $mutatorForArray->getType()) {
return 'public' === $mutatorForArray->getVisibility();
}
if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType()) {
return true;
}
$mutator = $this->getWriteInfo($object::class, $property, '');
return PropertyWriteInfo::TYPE_NONE !== $mutator->getType();
}
/**
* Gets a PropertyPath instance and caches it.
*/
private function getPropertyPath(string|PropertyPath $propertyPath): PropertyPath
{
if ($propertyPath instanceof PropertyPathInterface) {
// Don't call the copy constructor has it is not needed here
return $propertyPath;
}
if (isset($this->propertyPathCache[$propertyPath])) {
return $this->propertyPathCache[$propertyPath];
}
if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_PROPERTY_PATH.rawurlencode($propertyPath));
if ($item->isHit()) {
return $this->propertyPathCache[$propertyPath] = $item->get();
}
}
$propertyPathInstance = new PropertyPath($propertyPath);
if (isset($item)) {
$item->set($propertyPathInstance);
$this->cacheItemPool->save($item);
}
return $this->propertyPathCache[$propertyPath] = $propertyPathInstance;
}
/**
* Creates the APCu adapter if applicable.
*
* @throws \LogicException When the Cache Component isn't available
*/
public static function createCache(string $namespace, int $defaultLifetime, string $version, ?LoggerInterface $logger = null): AdapterInterface
{
if (!class_exists(ApcuAdapter::class)) {
throw new \LogicException(\sprintf('The Symfony Cache component must be installed to use "%s()".', __METHOD__));
}
if (!ApcuAdapter::isSupported()) {
return new NullAdapter();
}
$apcu = new ApcuAdapter($namespace, $defaultLifetime / 5, $version);
if ('cli' === \PHP_SAPI && !filter_var(\ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOL)) {
$apcu->setLogger(new NullLogger());
} elseif (null !== $logger) {
$apcu->setLogger($logger);
}
return $apcu;
}
}
|