-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityRegistry.php
232 lines (202 loc) · 6.94 KB
/
EntityRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Orm;
use Opulence\Orm\ChangeTracking\IChangeTracker;
use Opulence\Orm\Ids\Accessors\IIdAccessorRegistry;
/**
* Defines an entity registry
*/
class EntityRegistry implements IEntityRegistry
{
/** @var IIdAccessorRegistry The Id accessory registry */
protected $idAccessorRegistry = null;
/** @var IChangeTracker The change tracker */
protected $changeTracker = null;
/** @var array The mapping of entities' object hash Ids to their various states */
private $entityStates = [];
/** @var array The mapping of class names to a list of entities of that class */
private $entities = [];
/**
* Maps aggregate root children to their roots as well as functions that can set the child's aggregate root Id
* Each entry is an array of arrays with the following keys:
* "aggregateRoot" => The aggregate root
* "child" => The entity whose aggregate root Id will be set to the Id of the aggregate root
* "function" => The function to execute that actually sets the aggregate root Id in the child
* Note: The function MUST have two parameters: first for the aggregate root and a second for the child
*
* @var array
*/
private $aggregateRootChildren = [];
/**
* @param IIdAccessorRegistry $idAccessorRegistry The Id accessor registry
* @param IChangeTracker $changeTracker The change tracker
*/
public function __construct(IIdAccessorRegistry $idAccessorRegistry, IChangeTracker $changeTracker)
{
$this->idAccessorRegistry = $idAccessorRegistry;
$this->changeTracker = $changeTracker;
}
/**
* @inheritdoc
*/
public function clear()
{
$this->changeTracker->stopTrackingAll();
$this->entities = [];
$this->entityStates = [];
$this->clearAggregateRoots();
}
/**
* @inheritdoc
*/
public function clearAggregateRoots()
{
$this->aggregateRootChildren = [];
}
/**
* @inheritdoc
*/
public function deregisterEntity($entity)
{
$entityState = $this->getEntityState($entity);
unset($this->aggregateRootChildren[$this->getObjectHashId($entity)]);
if ($entityState === EntityStates::QUEUED || $entityState === EntityStates::REGISTERED) {
$className = $this->getClassName($entity);
$objectHashId = $this->getObjectHashId($entity);
$entityId = (string) $this->idAccessorRegistry->getEntityId($entity);
$this->entityStates[$objectHashId] = EntityStates::UNREGISTERED;
unset($this->entities[$className][$entityId]);
$this->changeTracker->stopTracking($entity);
}
}
/**
* @inheritdoc
*/
public function getClassName($object) : string
{
return get_class($object);
}
/**
* @inheritdoc
*/
public function getEntities() : array
{
if (count($this->entities) === 0) {
return [];
}
// Flatten the list of entities
$entities = [];
array_walk_recursive($this->entities, function ($entity) use (&$entities) {
$entities[] = $entity;
});
return $entities;
}
/**
* @inheritdoc
*/
public function getEntity(string $className, $id)
{
$index = (string) $id;
if (!isset($this->entities[$className][$index])) {
return null;
}
return $this->entities[$className][$index];
}
/**
* @inheritdoc
*/
public function getEntityState($entity) : int
{
$objectHashId = $this->getObjectHashId($entity);
if (!isset($this->entityStates[$objectHashId])) {
return EntityStates::NEVER_REGISTERED;
}
return $this->entityStates[$objectHashId];
}
/**
* @inheritdoc
*/
public function getObjectHashId($object) : string
{
return spl_object_hash($object);
}
/**
* @inheritdoc
*/
public function isRegistered($entity) : bool
{
try {
$entityId = (string) $this->idAccessorRegistry->getEntityId($entity);
return $this->getEntityState($entity) === EntityStates::REGISTERED
|| isset($this->entities[$this->getClassName($entity)][$entityId]);
} catch (OrmException $ex) {
return false;
}
}
/**
* Registers a function to set the aggregate root Id in a child entity after the aggregate root has been inserted
* Since the child depends on the aggregate root's Id being set, make sure the root is inserted before the child
*
* @param object $aggregateRoot The aggregate root
* @param object $child The child of the aggregate root
* @param callable $function The function that contains the logic to set the aggregate root Id in the child
*/
public function registerAggregateRootCallback($aggregateRoot, $child, callable $function)
{
$childObjectHashId = $this->getObjectHashId($child);
if (!isset($this->aggregateRootChildren[$childObjectHashId])) {
$this->aggregateRootChildren[$childObjectHashId] = [];
}
$this->aggregateRootChildren[$childObjectHashId][] = [
'aggregateRoot' => $aggregateRoot,
'child' => $child,
'function' => $function
];
}
/**
* @inheritdoc
*/
public function registerEntity(&$entity)
{
$className = $this->getClassName($entity);
$entityId = (string) $this->idAccessorRegistry->getEntityId($entity);
if (!isset($this->entities[$className])) {
$this->entities[$className] = [];
}
if (isset($this->entities[$className][$entityId])) {
// Change the reference of the input entity to the one that's already registered
$entity = $this->getEntity($className, $entityId);
} else {
// Register this entity
$this->changeTracker->startTracking($entity);
$this->entities[$className][$entityId] = $entity;
$this->entityStates[$this->getObjectHashId($entity)] = EntityStates::REGISTERED;
}
}
/**
* @inheritdoc
*/
public function runAggregateRootCallbacks($child)
{
$objectHashId = $this->getObjectHashId($child);
if (isset($this->aggregateRootChildren[$objectHashId])) {
foreach ($this->aggregateRootChildren[$objectHashId] as $aggregateRootData) {
$aggregateRoot = $aggregateRootData['aggregateRoot'];
$aggregateRootData['function']($aggregateRoot, $child);
}
}
}
/**
* @inheritdoc
*/
public function setState($entity, int $entityState)
{
$this->entityStates[$this->getObjectHashId($entity)] = $entityState;
}
}