@@ -237,46 +237,27 @@ private function instantiateTargetProxy(
237
237
// create proxy initializer. this initializer will be executed when the
238
238
// proxy is first accessed
239
239
240
- $ initializer = function (object $ instance ) use (
240
+ $ initializer = function (object $ target ) use (
241
241
$ source ,
242
242
$ objectToObjectMetadata ,
243
243
$ context ,
244
- $ targetClass
245
244
): void {
246
- // if constructor exists, process it
245
+ // if the constructor is lazy, run it here
247
246
248
- if (\method_exists ( $ instance , ' __construct ' )) {
249
- $ constructorArguments = $ this ->generateConstructorArguments (
247
+ if (! $ objectToObjectMetadata -> constructorIsEager ( )) {
248
+ $ target = $ this ->runConstructorManually (
250
249
source: $ source ,
250
+ target: $ target ,
251
251
objectToObjectMetadata: $ objectToObjectMetadata ,
252
252
context: $ context
253
253
);
254
-
255
- $ arguments = $ constructorArguments ->getArguments ();
256
-
257
- try {
258
- /**
259
- * @psalm-suppress DirectConstructorCall
260
- * @psalm-suppress MixedMethodCall
261
- */
262
- $ instance ->__construct (...$ arguments );
263
- } catch (\TypeError | \ReflectionException $ e ) {
264
- throw new InstantiationFailureException (
265
- source: $ source ,
266
- targetClass: $ targetClass ,
267
- constructorArguments: $ constructorArguments ->getArguments (),
268
- unsetSourceProperties: $ constructorArguments ->getUnsetSourceProperties (),
269
- previous: $ e ,
270
- context: $ context
271
- );
272
- }
273
254
}
274
255
275
256
// map lazy properties
276
257
277
258
$ this ->readSourceAndWriteTarget (
278
259
source: $ source ,
279
- target: $ instance ,
260
+ target: $ target ,
280
261
propertyMappings: $ objectToObjectMetadata ->getLazyPropertyMappings (),
281
262
context: $ context
282
263
);
@@ -295,6 +276,17 @@ private function instantiateTargetProxy(
295
276
skippedProperties: $ objectToObjectMetadata ->getTargetProxySkippedProperties ()
296
277
);
297
278
279
+ // if the constructor is eager, run it here
280
+
281
+ if ($ objectToObjectMetadata ->constructorIsEager ()) {
282
+ $ target = $ this ->runConstructorManually (
283
+ source: $ source ,
284
+ target: $ target ,
285
+ objectToObjectMetadata: $ objectToObjectMetadata ,
286
+ context: $ context
287
+ );
288
+ }
289
+
298
290
// map eager properties
299
291
300
292
$ this ->readSourceAndWriteTarget (
@@ -307,6 +299,44 @@ private function instantiateTargetProxy(
307
299
return $ target ;
308
300
}
309
301
302
+ private function runConstructorManually (
303
+ object $ source ,
304
+ object $ target ,
305
+ ObjectToObjectMetadata $ objectToObjectMetadata ,
306
+ Context $ context
307
+ ): object {
308
+ if (!\method_exists ($ target , '__construct ' )) {
309
+ return $ target ;
310
+ }
311
+
312
+ $ constructorArguments = $ this ->generateConstructorArguments (
313
+ source: $ source ,
314
+ objectToObjectMetadata: $ objectToObjectMetadata ,
315
+ context: $ context
316
+ );
317
+
318
+ $ arguments = $ constructorArguments ->getArguments ();
319
+
320
+ try {
321
+ /**
322
+ * @psalm-suppress DirectConstructorCall
323
+ * @psalm-suppress MixedMethodCall
324
+ */
325
+ $ target ->__construct (...$ arguments );
326
+ } catch (\TypeError | \ReflectionException $ e ) {
327
+ throw new InstantiationFailureException (
328
+ source: $ source ,
329
+ targetClass: $ target ::class,
330
+ constructorArguments: $ constructorArguments ->getArguments (),
331
+ unsetSourceProperties: $ constructorArguments ->getUnsetSourceProperties (),
332
+ previous: $ e ,
333
+ context: $ context
334
+ );
335
+ }
336
+
337
+ return $ target ;
338
+ }
339
+
310
340
private function generateConstructorArguments (
311
341
object $ source ,
312
342
ObjectToObjectMetadata $ objectToObjectMetadata ,
0 commit comments