HydrationCompleteHandler.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Internal;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Doctrine\ORM\Event\LifecycleEventArgs;
  22. use Doctrine\ORM\Event\ListenersInvoker;
  23. use Doctrine\ORM\Events;
  24. use Doctrine\ORM\Mapping\ClassMetadata;
  25. /**
  26. * Class, which can handle completion of hydration cycle and produce some of tasks.
  27. * In current implementation triggers deferred postLoad event.
  28. */
  29. final class HydrationCompleteHandler
  30. {
  31. /** @var ListenersInvoker */
  32. private $listenersInvoker;
  33. /** @var EntityManagerInterface */
  34. private $em;
  35. /** @var mixed[][] */
  36. private $deferredPostLoadInvocations = [];
  37. /**
  38. * Constructor for this object
  39. */
  40. public function __construct(ListenersInvoker $listenersInvoker, EntityManagerInterface $em)
  41. {
  42. $this->listenersInvoker = $listenersInvoker;
  43. $this->em = $em;
  44. }
  45. /**
  46. * Method schedules invoking of postLoad entity to the very end of current hydration cycle.
  47. *
  48. * @param object $entity
  49. */
  50. public function deferPostLoadInvoking(ClassMetadata $class, $entity)
  51. {
  52. $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postLoad);
  53. if ($invoke === ListenersInvoker::INVOKE_NONE) {
  54. return;
  55. }
  56. $this->deferredPostLoadInvocations[] = [$class, $invoke, $entity];
  57. }
  58. /**
  59. * This method should me called after any hydration cycle completed.
  60. *
  61. * Method fires all deferred invocations of postLoad events
  62. */
  63. public function hydrationComplete()
  64. {
  65. $toInvoke = $this->deferredPostLoadInvocations;
  66. $this->deferredPostLoadInvocations = [];
  67. foreach ($toInvoke as $classAndEntity) {
  68. [$class, $invoke, $entity] = $classAndEntity;
  69. $this->listenersInvoker->invoke(
  70. $class,
  71. Events::postLoad,
  72. $entity,
  73. new LifecycleEventArgs($entity, $this->em),
  74. $invoke
  75. );
  76. }
  77. }
  78. }