diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php')
-rw-r--r-- | vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php b/vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php new file mode 100644 index 0000000..e0fe342 --- /dev/null +++ b/vendor/doctrine/orm/src/Internal/HydrationCompleteHandler.php | |||
@@ -0,0 +1,64 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Internal; | ||
6 | |||
7 | use Doctrine\ORM\EntityManagerInterface; | ||
8 | use Doctrine\ORM\Event\ListenersInvoker; | ||
9 | use Doctrine\ORM\Event\PostLoadEventArgs; | ||
10 | use Doctrine\ORM\Events; | ||
11 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
12 | |||
13 | /** | ||
14 | * Class, which can handle completion of hydration cycle and produce some of tasks. | ||
15 | * In current implementation triggers deferred postLoad event. | ||
16 | */ | ||
17 | final class HydrationCompleteHandler | ||
18 | { | ||
19 | /** @var mixed[][] */ | ||
20 | private array $deferredPostLoadInvocations = []; | ||
21 | |||
22 | public function __construct( | ||
23 | private readonly ListenersInvoker $listenersInvoker, | ||
24 | private readonly EntityManagerInterface $em, | ||
25 | ) { | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Method schedules invoking of postLoad entity to the very end of current hydration cycle. | ||
30 | */ | ||
31 | public function deferPostLoadInvoking(ClassMetadata $class, object $entity): void | ||
32 | { | ||
33 | $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postLoad); | ||
34 | |||
35 | if ($invoke === ListenersInvoker::INVOKE_NONE) { | ||
36 | return; | ||
37 | } | ||
38 | |||
39 | $this->deferredPostLoadInvocations[] = [$class, $invoke, $entity]; | ||
40 | } | ||
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 | |||
52 | foreach ($toInvoke as $classAndEntity) { | ||
53 | [$class, $invoke, $entity] = $classAndEntity; | ||
54 | |||
55 | $this->listenersInvoker->invoke( | ||
56 | $class, | ||
57 | Events::postLoad, | ||
58 | $entity, | ||
59 | new PostLoadEventArgs($entity, $this->em), | ||
60 | $invoke, | ||
61 | ); | ||
62 | } | ||
63 | } | ||
64 | } | ||