1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
declare(strict_types=1); |
5
|
|
|
|
6
|
|
|
namespace Doctrine\ORM\Proxy\Factory; |
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
11
|
|
|
use Doctrine\ORM\Mapping\TransientMetadata; |
12
|
|
|
use Doctrine\ORM\Persisters\Entity\EntityPersister; |
13
|
|
|
use ProxyManager\Factory\LazyLoadingGhostFactory; |
14
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Static factory for proxy objects. |
18
|
|
|
* |
19
|
|
|
* @package Doctrine\ORM\Proxy\Factory |
20
|
|
|
* @since 3.0 |
21
|
|
|
* |
22
|
|
|
* @author Benjamin Eberlei <[email protected]> |
23
|
|
|
* @author Guilherme Blanco <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class StaticProxyFactory implements ProxyFactory |
26
|
|
|
{ |
27
|
|
|
private const SKIPPED_PROPERTIES = 'skippedProperties'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EntityManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $entityManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LazyLoadingGhostFactory |
36
|
|
|
*/ |
37
|
|
|
private $proxyFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClassMetadata[] indexed by metadata class name |
41
|
|
|
*/ |
42
|
|
|
private $cachedMetadata = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Closure[] indexed by metadata class name |
46
|
|
|
*/ |
47
|
|
|
private $cachedInitializers = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var EntityPersister[] indexed by metadata class name |
51
|
|
|
*/ |
52
|
|
|
private $cachedPersisters = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string[][] indexed by metadata class name |
56
|
|
|
*/ |
57
|
|
|
private $cachedSkippedProperties = []; |
58
|
|
|
|
59
|
|
|
public function __construct( |
60
|
|
|
EntityManagerInterface $entityManager, |
61
|
|
|
LazyLoadingGhostFactory $proxyFactory |
62
|
|
|
) { |
63
|
|
|
$this->entityManager = $entityManager; |
64
|
|
|
$this->proxyFactory = $proxyFactory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @param ClassMetadata[] $classMetadataList |
71
|
|
|
*/ |
72
|
|
|
public function generateProxyClasses(array $classMetadataList) : int |
73
|
|
|
{ |
74
|
|
|
$concreteClasses = \array_filter($classMetadataList, function (ClassMetadata $metadata) : bool { |
75
|
|
|
return ! ($metadata->isMappedSuperclass || $metadata->getReflectionClass()->isAbstract()); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
foreach ($concreteClasses as $metadata) { |
79
|
|
|
$className = $metadata->getClassName(); |
80
|
|
|
|
81
|
|
|
$this |
82
|
|
|
->proxyFactory |
83
|
|
|
->createProxy( |
84
|
|
|
$className, |
85
|
|
|
function () { |
86
|
|
|
// empty closure, serves its purpose, for now |
87
|
|
|
}, |
88
|
|
|
$this->cachedSkippedProperties[$className] |
89
|
|
|
?? $this->cachedSkippedProperties[$className] = [ |
90
|
|
|
self::SKIPPED_PROPERTIES => $this->skippedFieldsFqns($metadata) |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return \count($concreteClasses); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
* @throws \Doctrine\ORM\EntityNotFoundException |
101
|
|
|
*/ |
102
|
|
|
public function getProxy(string $className, array $identifier) : GhostObjectInterface |
103
|
|
|
{ |
104
|
|
|
$metadata = $this->cachedMetadata[$className] |
105
|
|
|
?? $this->cachedMetadata[$className] = $this->entityManager->getClassMetadata($className); |
106
|
|
|
$persister = $this->cachedPersisters[$className] |
107
|
|
|
?? $this->cachedPersisters[$className] = $this |
108
|
|
|
->entityManager |
109
|
|
|
->getUnitOfWork() |
110
|
|
|
->getEntityPersister($metadata->getClassName()); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$proxyInstance = $this |
113
|
|
|
->proxyFactory |
114
|
|
|
->createProxy( |
115
|
|
|
$metadata->getClassName(), |
116
|
|
|
$this->cachedInitializers[$className] |
117
|
|
|
?? $this->cachedInitializers[$className] = $this->makeInitializer($metadata, $persister), |
|
|
|
|
118
|
|
|
$this->cachedSkippedProperties[$className] |
119
|
|
|
?? $this->cachedSkippedProperties[$className] = [ |
120
|
|
|
self::SKIPPED_PROPERTIES => $this->skippedFieldsFqns($metadata) |
|
|
|
|
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$persister->setIdentifier($proxyInstance, $identifier); |
125
|
|
|
|
126
|
|
|
return $proxyInstance; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function makeInitializer(ClassMetadata $metadata, EntityPersister $persister) : \Closure |
130
|
|
|
{ |
131
|
|
|
return function ( |
132
|
|
|
GhostObjectInterface $ghostObject, |
133
|
|
|
string $method, // we don't care |
134
|
|
|
array $parameters, // we don't care |
135
|
|
|
& $initializer, |
136
|
|
|
array $properties // we currently do not use this |
|
|
|
|
137
|
|
|
) use ($metadata, $persister) : bool { |
138
|
|
|
$originalInitializer = $initializer; |
139
|
|
|
$initializer = null; |
140
|
|
|
|
141
|
|
|
$identifier = $persister->getIdentifier($ghostObject); |
142
|
|
|
|
143
|
|
|
// @TODO how do we use `$properties` in the persister? That would be a massive optimisation |
144
|
|
|
if (! $persister->loadById($identifier, $ghostObject)) { |
145
|
|
|
$initializer = $originalInitializer; |
146
|
|
|
|
147
|
|
|
throw EntityNotFoundException::fromClassNameAndIdentifier( |
148
|
|
|
$metadata->getClassName(), |
149
|
|
|
$identifier |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
}; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function skippedFieldsFqns(ClassMetadata $metadata) : array |
158
|
|
|
{ |
159
|
|
|
return \array_merge( |
160
|
|
|
$this->identifierFieldFqns($metadata), |
161
|
|
|
$this->transientFieldsFqns($metadata) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function transientFieldsFqns(ClassMetadata $metadata) : array |
166
|
|
|
{ |
167
|
|
|
$transientFieldsFqns = []; |
168
|
|
|
|
169
|
|
|
foreach ($metadata->getDeclaredPropertiesIterator() as $name => $property) { |
170
|
|
|
if (! $property instanceof TransientMetadata) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$transientFieldsFqns[] = $this->propertyFqcn( |
175
|
|
|
$property |
176
|
|
|
->getDeclaringClass() |
177
|
|
|
->getReflectionClass() |
178
|
|
|
->getProperty($name) // @TODO possible NPR. This should never be null, why is it allowed to be? |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $transientFieldsFqns; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function identifierFieldFqns(ClassMetadata $metadata) : array |
186
|
|
|
{ |
187
|
|
|
$idFieldFqcns = []; |
188
|
|
|
|
189
|
|
|
foreach ($metadata->getIdentifierFieldNames() as $idField) { |
190
|
|
|
$idFieldFqcns[] = $this->propertyFqcn( |
191
|
|
|
$metadata |
192
|
|
|
->getProperty($idField) |
193
|
|
|
->getDeclaringClass() |
194
|
|
|
->getReflectionClass() |
195
|
|
|
->getProperty($idField) // @TODO possible NPR. This should never be null, why is it allowed to be? |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $idFieldFqcns; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function propertyFqcn(\ReflectionProperty $property) : string |
203
|
|
|
{ |
204
|
|
|
if ($property->isPrivate()) { |
205
|
|
|
return "\0" . $property->getDeclaringClass()->getName() . "\0" . $property->getName(); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($property->isProtected()) { |
209
|
|
|
return "\0*\0" . $property->getName(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $property->getName(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: