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