|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Object; |
|
4
|
|
|
|
|
5
|
|
|
class InMemoryRegistry implements Registry |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var SnapshotMaker |
|
9
|
|
|
*/ |
|
10
|
|
|
private $snapshotMaker; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var PropertyCloner |
|
14
|
|
|
*/ |
|
15
|
|
|
private $recoveryPoint; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $objects; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $snapshots; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $removed; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param SnapshotMaker $snapshotMaker |
|
34
|
|
|
* @param PropertyCloner $recoveryPoint |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(SnapshotMaker $snapshotMaker, PropertyCloner $recoveryPoint) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->snapshotMaker = $snapshotMaker; |
|
39
|
|
|
$this->recoveryPoint = $recoveryPoint; |
|
40
|
|
|
$this->objects = []; |
|
41
|
|
|
$this->snapshots = []; |
|
42
|
|
|
$this->removed = []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function isRegistered($object) |
|
49
|
|
|
{ |
|
50
|
|
|
return array_key_exists($this->getId($object), $this->objects); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function register($object) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->objects[$this->getId($object)] = $object; |
|
59
|
|
|
$this->snapshots[$this->getId($object)] = $this->snapshotMaker->makeSnapshotOf($object); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getSnapshot($object) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->snapshots[$this->getId($object)]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function makeNewSnapshots() |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($this->objects as $id => $entity) { |
|
76
|
|
|
$this->snapshots[$id] = $this->snapshotMaker->makeSnapshotOf($entity); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function isRemoved($object) |
|
84
|
|
|
{ |
|
85
|
|
|
return array_key_exists($this->getId($object), $this->removed); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function remove($object) |
|
92
|
|
|
{ |
|
93
|
|
|
if (!$this->isRegistered($object)) { |
|
94
|
|
|
$this->register($object); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->removed[$this->getId($object)] = true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function cleanRemoved() |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ($this->removed as $id => $object) { |
|
106
|
|
|
unset($this->snapshots[$id]); |
|
107
|
|
|
unset($this->objects[$id]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->removed = []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function all() |
|
117
|
|
|
{ |
|
118
|
|
|
return array_values($this->objects); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function reset() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->removed = []; |
|
127
|
|
|
|
|
128
|
|
|
foreach ($this->snapshots as $id => $objectSnapshot) { |
|
129
|
|
|
$this->recoveryPoint->cloneProperties($this->objects[$id], $objectSnapshot); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $entity |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
private function getId($entity) |
|
138
|
|
|
{ |
|
139
|
|
|
return spl_object_hash($entity); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|