|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Actiane\EntityChangeWatchBundle\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
6
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class LifecycleCallableGenerator |
|
11
|
|
|
*/ |
|
12
|
|
|
class LifecycleCallableGenerator |
|
13
|
|
|
{ |
|
14
|
|
|
private array $entityWatch; |
|
15
|
|
|
private PropertyAccessor $propertyAccessor; |
|
16
|
|
|
private CallableGenerator $callableGenerator; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(array $entityWatch, CallableGenerator $callableGenerator, PropertyAccessor $propertyAccessor) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->entityWatch = $entityWatch; |
|
21
|
|
|
$this->propertyAccessor = $propertyAccessor; |
|
22
|
|
|
$this->callableGenerator = $callableGenerator; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param UnitOfWork $unitOfWork |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function generateLifeCycleCallable(UnitOfWork $unitOfWork): array |
|
31
|
|
|
{ |
|
32
|
|
|
$callable = []; |
|
33
|
|
|
$lifeCycleEntities = []; |
|
34
|
|
|
foreach ($unitOfWork->getScheduledEntityInsertions() as $entityInsertion) { |
|
35
|
|
|
$this->generateCreateDeleteCallables($callable, $lifeCycleEntities, $entityInsertion, 'create'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
foreach ($unitOfWork->getScheduledEntityDeletions() as $entityDeletion) { |
|
39
|
|
|
$this->generateCreateDeleteCallables($callable, $lifeCycleEntities, $entityDeletion, 'delete'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
foreach ($unitOfWork->getScheduledCollectionUpdates() as $collectionUpdate) { |
|
43
|
|
|
if (array_key_exists(spl_object_hash($collectionUpdate->getOwner()), $lifeCycleEntities)) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
$this->generateUpdateCallables( |
|
47
|
|
|
$callable, |
|
48
|
|
|
[$collectionUpdate->getMapping()['fieldName'] => $collectionUpdate->getValues()], |
|
49
|
|
|
$collectionUpdate->getOwner() |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
foreach ($unitOfWork->getScheduledEntityUpdates() as $entityUpdate) { |
|
54
|
|
|
$this->generateUpdateCallables( |
|
55
|
|
|
$callable, |
|
56
|
|
|
$unitOfWork->getEntityChangeSet($entityUpdate), |
|
57
|
|
|
$entityUpdate |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $callable; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $callable |
|
66
|
|
|
* @param $entity |
|
67
|
|
|
* @param $state |
|
68
|
|
|
*/ |
|
69
|
|
|
private function generateCreateDeleteCallables(&$callable, &$lifeCycleEntities, $entity, $state): void |
|
70
|
|
|
{ |
|
71
|
|
|
$lifeCycleEntities[spl_object_hash($entity)] = $entity; |
|
72
|
|
|
// we might have a doctrine proxyfied object |
|
73
|
|
|
$className = ClassUtils::getClass($entity); |
|
74
|
|
|
|
|
75
|
|
|
if (!array_key_exists($className, $this->entityWatch)) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
foreach ($this->entityWatch[$className][$state] as $action) { |
|
80
|
|
|
$callable += $this->callableGenerator->generateCallable($action, $entity); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $callable |
|
86
|
|
|
* @param $changedProperties |
|
87
|
|
|
* @param $entity |
|
88
|
|
|
*/ |
|
89
|
|
|
private function generateUpdateCallables(&$callable, $changedProperties, $entity): void |
|
90
|
|
|
{ |
|
91
|
|
|
// we might have a doctrine proxyfied object |
|
92
|
|
|
$className = ClassUtils::getClass($entity); |
|
93
|
|
|
|
|
94
|
|
|
if (!array_key_exists($className, $this->entityWatch) |
|
95
|
|
|
|| !array_key_exists('update', $this->entityWatch[$className]) |
|
96
|
|
|
) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$entityWatch = $this->entityWatch[$className]['update']; |
|
101
|
|
|
foreach ($entityWatch['all'] as $action) { |
|
102
|
|
|
$callable += $this->callableGenerator->generateCallable($action, $entity, $changedProperties); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
foreach ($entityWatch['properties'] as $propertyName => $actions) { |
|
106
|
|
|
if (array_key_exists($propertyName, $changedProperties)) { |
|
107
|
|
|
foreach ($actions as $action) { |
|
108
|
|
|
$callable += $this->callableGenerator->generateCallable( |
|
109
|
|
|
$action, |
|
110
|
|
|
$entity, |
|
111
|
|
|
$changedProperties |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|