|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\SkeletonMapper; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryFactoryInterface; |
|
8
|
|
|
use function count; |
|
9
|
|
|
use function get_class; |
|
10
|
|
|
use function serialize; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class for maintaining an object identity map. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ObjectIdentityMap |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var object[][] */ |
|
18
|
|
|
private $identityMap = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ObjectRepositoryFactoryInterface */ |
|
21
|
|
|
private $objectRepositoryFactory; |
|
22
|
|
|
|
|
23
|
22 |
|
public function __construct(ObjectRepositoryFactoryInterface $objectRepositoryFactory) |
|
24
|
|
|
{ |
|
25
|
22 |
|
$this->objectRepositoryFactory = $objectRepositoryFactory; |
|
26
|
22 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param object $object |
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function contains($object) : bool |
|
32
|
|
|
{ |
|
33
|
11 |
|
$className = get_class($object); |
|
34
|
|
|
|
|
35
|
11 |
|
$objectIdentifier = $this->getObjectIdentifier($object); |
|
36
|
|
|
|
|
37
|
11 |
|
$serialized = serialize($objectIdentifier); |
|
38
|
|
|
|
|
39
|
11 |
|
return isset($this->identityMap[$className][$serialized]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param mixed[] $data |
|
44
|
|
|
* |
|
45
|
|
|
* @return object|null |
|
46
|
|
|
*/ |
|
47
|
18 |
|
public function tryGetById(string $className, array $data) |
|
48
|
|
|
{ |
|
49
|
18 |
|
$serialized = serialize($this->extractIdentifierFromData($className, $data)); |
|
50
|
|
|
|
|
51
|
18 |
|
if (isset($this->identityMap[$className][$serialized])) { |
|
52
|
8 |
|
return $this->identityMap[$className][$serialized]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
17 |
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param object $object |
|
60
|
|
|
* @param mixed[] $data |
|
61
|
|
|
*/ |
|
62
|
20 |
|
public function addToIdentityMap($object, array $data) : void |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
20 |
|
$className = get_class($object); |
|
65
|
|
|
|
|
66
|
20 |
|
if (! isset($this->identityMap[$className])) { |
|
67
|
20 |
|
$this->identityMap[get_class($object)] = []; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
20 |
|
$serialized = serialize($this->getObjectIdentifier($object)); |
|
71
|
|
|
|
|
72
|
20 |
|
$this->identityMap[get_class($object)][$serialized] = $object; |
|
73
|
20 |
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
public function clear(?string $objectName = null) : void |
|
76
|
|
|
{ |
|
77
|
9 |
|
if ($objectName !== null) { |
|
78
|
1 |
|
unset($this->identityMap[$objectName]); |
|
79
|
|
|
} else { |
|
80
|
9 |
|
$this->identityMap = []; |
|
81
|
|
|
} |
|
82
|
9 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param object $object |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function detach($object) : void |
|
88
|
|
|
{ |
|
89
|
4 |
|
$objectIdentifier = $this->getObjectIdentifier($object); |
|
90
|
|
|
|
|
91
|
4 |
|
$serialized = serialize($objectIdentifier); |
|
92
|
4 |
|
unset($this->identityMap[get_class($object)][$serialized]); |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function count() : int |
|
96
|
|
|
{ |
|
97
|
|
|
return count($this->identityMap); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param object $object |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed[] $identifier |
|
104
|
|
|
*/ |
|
105
|
20 |
|
private function getObjectIdentifier($object) : array |
|
106
|
|
|
{ |
|
107
|
20 |
|
return $this->objectRepositoryFactory |
|
108
|
20 |
|
->getRepository(get_class($object)) |
|
109
|
20 |
|
->getObjectIdentifier($object); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param mixed[] $data |
|
114
|
|
|
* |
|
115
|
|
|
* @return mixed[] $identifier |
|
116
|
|
|
*/ |
|
117
|
18 |
|
private function extractIdentifierFromData(string $className, array $data) : array |
|
118
|
|
|
{ |
|
119
|
18 |
|
return $this->objectRepositoryFactory |
|
120
|
18 |
|
->getRepository($className) |
|
121
|
18 |
|
->getObjectIdentifierFromData($data); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.