@@ -48,13 +48,12 @@ class <proxyShortClassName> extends \<className> implements \<baseProxyInterface
48
48
{
49
49
<useLazyGhostTrait>
50
50
51
- /**
52
- * @internal
53
- */
54
- public bool $__isCloning = false;
55
-
56
- public function __construct(?\Closure $initializer = null)
51
+ public function __construct(?\Closure $initializer = null, ?\Closure $cloner = null)
57
52
{
53
+ if ($cloner !== null) {
54
+ return;
55
+ }
56
+
58
57
self::createLazyGhost($initializer, <skippedProperties>, $this);
59
58
}
60
59
@@ -63,17 +62,6 @@ public function __isInitialized(): bool
63
62
return isset($this->lazyObjectState) && $this->isLazyObjectInitialized();
64
63
}
65
64
66
- public function __clone()
67
- {
68
- $this->__isCloning = true;
69
-
70
- try {
71
- $this->__doClone();
72
- } finally {
73
- $this->__isCloning = false;
74
- }
75
- }
76
-
77
65
public function __serialize(): array
78
66
{
79
67
<serializeImpl>
@@ -98,6 +86,9 @@ public function __serialize(): array
98
86
*/
99
87
private $ identifierFlattener ;
100
88
89
+ /** @var ProxyDefinition[] */
90
+ private $ definitions = [];
91
+
101
92
/**
102
93
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
103
94
* connected to the given <tt>EntityManager</tt>.
@@ -131,6 +122,26 @@ public function __construct(EntityManagerInterface $em, $proxyDir, $proxyNs, $au
131
122
$ this ->identifierFlattener = new IdentifierFlattener ($ this ->uow , $ em ->getMetadataFactory ());
132
123
}
133
124
125
+ /**
126
+ * {@inheritDoc}
127
+ */
128
+ public function getProxy ($ className , array $ identifier )
129
+ {
130
+ $ proxy = parent ::getProxy ($ className , $ identifier );
131
+
132
+ if (! $ this ->em ->getConfiguration ()->isLazyGhostObjectEnabled ()) {
133
+ return $ proxy ;
134
+ }
135
+
136
+ $ initializer = $ this ->definitions [$ className ]->initializer ;
137
+
138
+ $ proxy ->__construct (static function (Proxy $ object ) use ($ initializer , $ proxy ): void {
139
+ $ initializer ($ object , $ proxy );
140
+ });
141
+
142
+ return $ proxy ;
143
+ }
144
+
134
145
/**
135
146
* {@inheritDoc}
136
147
*/
@@ -158,7 +169,7 @@ protected function createProxyDefinition($className)
158
169
$ cloner = $ this ->createCloner ($ classMetadata , $ entityPersister );
159
170
}
160
171
161
- return new ProxyDefinition (
172
+ return $ this -> definitions [ $ className ] = new ProxyDefinition (
162
173
ClassUtils::generateProxyClassName ($ className , $ this ->proxyNs ),
163
174
$ classMetadata ->getIdentifierFieldNames (),
164
175
$ classMetadata ->getReflectionProperties (),
@@ -231,15 +242,15 @@ private function createInitializer(ClassMetadata $classMetadata, EntityPersister
231
242
/**
232
243
* Creates a closure capable of initializing a proxy
233
244
*
234
- * @return Closure(Proxy):void
245
+ * @return Closure(Proxy, Proxy ):void
235
246
*
236
247
* @throws EntityNotFoundException
237
248
*/
238
249
private function createLazyInitializer (ClassMetadata $ classMetadata , EntityPersister $ entityPersister ): Closure
239
250
{
240
- return function (Proxy $ proxy ) use ($ entityPersister , $ classMetadata ): void {
241
- $ identifier = $ classMetadata ->getIdentifierValues ($ proxy );
242
- $ entity = $ entityPersister ->loadById ($ identifier , $ proxy -> __isCloning ? null : $ proxy );
251
+ return function (Proxy $ proxy, Proxy $ original ) use ($ entityPersister , $ classMetadata ): void {
252
+ $ identifier = $ classMetadata ->getIdentifierValues ($ original );
253
+ $ entity = $ entityPersister ->loadById ($ identifier , $ original );
243
254
244
255
if ($ entity === null ) {
245
256
throw EntityNotFoundException::fromClassNameAndIdentifier (
@@ -248,7 +259,7 @@ private function createLazyInitializer(ClassMetadata $classMetadata, EntityPersi
248
259
);
249
260
}
250
261
251
- if (! $ proxy-> __isCloning ) {
262
+ if ($ proxy === $ original ) {
252
263
return ;
253
264
}
254
265
@@ -315,15 +326,14 @@ private function generateUseLazyGhostTrait(ClassMetadata $class): string
315
326
isLazyObjectInitialized as private;
316
327
createLazyGhost as private;
317
328
resetLazyObject as private;
318
- __clone as private __doClone;
319
329
} ' ), $ code );
320
330
321
331
return $ code ;
322
332
}
323
333
324
334
private function generateSkippedProperties (ClassMetadata $ class ): string
325
335
{
326
- $ skippedProperties = [' __isCloning ' => true ];
336
+ $ skippedProperties = [];
327
337
$ identifiers = array_flip ($ class ->getIdentifierFieldNames ());
328
338
$ filter = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE ;
329
339
$ reflector = $ class ->getReflectionClass ();
0 commit comments