RuntimePublicReflectionProperty.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Doctrine\Persistence\Reflection;
  3. use Doctrine\Common\Proxy\Proxy;
  4. use ReflectionProperty;
  5. /**
  6. * PHP Runtime Reflection Public Property - special overrides for public properties.
  7. */
  8. class RuntimePublicReflectionProperty extends ReflectionProperty
  9. {
  10. /**
  11. * {@inheritDoc}
  12. *
  13. * Checks is the value actually exist before fetching it.
  14. * This is to avoid calling `__get` on the provided $object if it
  15. * is a {@see \Doctrine\Common\Proxy\Proxy}.
  16. */
  17. public function getValue($object = null)
  18. {
  19. $name = $this->getName();
  20. if ($object instanceof Proxy && ! $object->__isInitialized()) {
  21. $originalInitializer = $object->__getInitializer();
  22. $object->__setInitializer(null);
  23. $val = $object->$name ?? null;
  24. $object->__setInitializer($originalInitializer);
  25. return $val;
  26. }
  27. return isset($object->$name) ? parent::getValue($object) : null;
  28. }
  29. /**
  30. * {@inheritDoc}
  31. *
  32. * Avoids triggering lazy loading via `__set` if the provided object
  33. * is a {@see \Doctrine\Common\Proxy\Proxy}.
  34. *
  35. * @link https://bugs.php.net/bug.php?id=63463
  36. */
  37. public function setValue($object, $value = null)
  38. {
  39. if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
  40. parent::setValue($object, $value);
  41. return;
  42. }
  43. $originalInitializer = $object->__getInitializer();
  44. $object->__setInitializer(null);
  45. parent::setValue($object, $value);
  46. $object->__setInitializer($originalInitializer);
  47. }
  48. }