vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php line 66

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Internal;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\Event\ListenersInvoker;
  6. use Doctrine\ORM\Event\PostLoadEventArgs;
  7. use Doctrine\ORM\Events;
  8. use Doctrine\ORM\Mapping\ClassMetadata;
  9. /**
  10. * Class, which can handle completion of hydration cycle and produce some of tasks.
  11. * In current implementation triggers deferred postLoad event.
  12. */
  13. final class HydrationCompleteHandler
  14. {
  15. /** @var ListenersInvoker */
  16. private $listenersInvoker;
  17. /** @var EntityManagerInterface */
  18. private $em;
  19. /** @var mixed[][] */
  20. private $deferredPostLoadInvocations = [];
  21. /**
  22. * Constructor for this object
  23. */
  24. public function __construct(ListenersInvoker $listenersInvoker, EntityManagerInterface $em)
  25. {
  26. $this->listenersInvoker = $listenersInvoker;
  27. $this->em = $em;
  28. }
  29. /**
  30. * Method schedules invoking of postLoad entity to the very end of current hydration cycle.
  31. *
  32. * @param object $entity
  33. */
  34. public function deferPostLoadInvoking(ClassMetadata $class, $entity): void
  35. {
  36. $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postLoad);
  37. if ($invoke === ListenersInvoker::INVOKE_NONE) {
  38. return;
  39. }
  40. $this->deferredPostLoadInvocations[] = [$class, $invoke, $entity];
  41. }
  42. /**
  43. * This method should be called after any hydration cycle completed.
  44. *
  45. * Method fires all deferred invocations of postLoad events
  46. */
  47. public function hydrationComplete(): void
  48. {
  49. $toInvoke = $this->deferredPostLoadInvocations;
  50. $this->deferredPostLoadInvocations = [];
  51. foreach ($toInvoke as $classAndEntity) {
  52. [$class, $invoke, $entity] = $classAndEntity;
  53. $this->listenersInvoker->invoke(
  54. $class,
  55. Events::postLoad,
  56. $entity,
  57. new PostLoadEventArgs($entity, $this->em),
  58. $invoke
  59. );
  60. }
  61. }
  62. }