14
14
use Doctrine \Persistence \ObjectRepository ;
15
15
use Symfony \Component \VarExporter \Exception \LogicException as VarExportLogicException ;
16
16
use Zenstruck \Foundry \Configuration ;
17
+ use Zenstruck \Foundry \Exception \FoundryNotBooted ;
17
18
use Zenstruck \Foundry \Exception \PersistenceDisabled ;
18
19
use Zenstruck \Foundry \Exception \PersistenceNotAvailable ;
19
20
use Zenstruck \Foundry \Factory ;
@@ -48,7 +49,7 @@ public function __construct()
48
49
{
49
50
parent ::__construct ();
50
51
51
- $ this ->persist = Configuration:: instance () ->isPersistenceEnabled () ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING ;
52
+ $ this ->persist = $ this ->isPersistenceEnabled () ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING ;
52
53
}
53
54
54
55
/**
@@ -271,17 +272,11 @@ final public function afterPersist(callable $callback): static
271
272
*/
272
273
public function persistMode (): PersistMode
273
274
{
274
- return Configuration:: instance () ->isPersistenceEnabled () ? $ this ->persist : PersistMode::WITHOUT_PERSISTING ;
275
+ return $ this ->isPersistenceEnabled () ? $ this ->persist : PersistMode::WITHOUT_PERSISTING ;
275
276
}
276
277
277
278
final public function isPersisting (): bool
278
279
{
279
- $ config = Configuration::instance ();
280
-
281
- if (!$ config ->isPersistenceEnabled ()) {
282
- return false ;
283
- }
284
-
285
280
return $ this ->persistMode ()->isPersisting ();
286
281
}
287
282
@@ -304,16 +299,17 @@ protected function normalizeParameter(string $field, mixed $value): mixed
304
299
if ($ inversedRelationshipMetadata && !$ inversedRelationshipMetadata ->isCollection ) {
305
300
$ inverseField = $ inversedRelationshipMetadata ->inverseField ;
306
301
307
- // we need to handle the circular dependency involved by inversed one-to-one relationship:
308
- // a placeholder object is used, which will be replaced by the real object, after its instantiation
309
- $ inversedObject = $ value ->withPersistMode (PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT )
310
- ->create ([$ inverseField => $ placeholder = (new \ReflectionClass (static ::class ()))->newInstanceWithoutConstructor ()]);
302
+ $ inversedObject = $ value ->withPersistMode (
303
+ $ this ->isPersisting () ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
304
+ )
311
305
312
- // auto-refresh computes changeset and prevents the placeholder object to be cleanly
313
- // forgotten fom the persistence manager
314
- if ($ inversedObject instanceof Proxy) {
315
- $ inversedObject = $ inversedObject ->_real (withAutoRefresh: false );
316
- }
306
+ // we need to handle the circular dependency involved by inversed one-to-one relationship:
307
+ // a placeholder object is used, which will be replaced by the real object, after its instantiation
308
+ ->create ([
309
+ $ inverseField => $ placeholder = (new \ReflectionClass (static ::class ()))->newInstanceWithoutConstructor (),
310
+ ]);
311
+
312
+ $ inversedObject = unproxy ($ inversedObject , withAutoRefresh: false );
317
313
318
314
$ this ->tempAfterInstantiate [] = static function (object $ object ) use ($ inversedObject , $ inverseField , $ pm , $ placeholder ) {
319
315
$ pm ->forget ($ placeholder );
@@ -324,12 +320,12 @@ protected function normalizeParameter(string $field, mixed $value): mixed
324
320
}
325
321
}
326
322
327
- return unproxy (parent ::normalizeParameter ($ field , $ value ));
323
+ return unproxy (parent ::normalizeParameter ($ field , $ value ), withAutoRefresh: false );
328
324
}
329
325
330
326
protected function normalizeCollection (string $ field , FactoryCollection $ collection ): array
331
327
{
332
- if (!$ this -> isPersisting () || !$ collection ->factory instanceof self) {
328
+ if (!Configuration:: instance ()-> isPersistenceAvailable () || !$ collection ->factory instanceof self) {
333
329
return parent ::normalizeCollection ($ field , $ collection );
334
330
}
335
331
@@ -338,12 +334,15 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
338
334
$ inverseRelationshipMetadata = $ pm ->inverseRelationshipMetadata (static ::class (), $ collection ->factory ::class (), $ field );
339
335
340
336
if ($ inverseRelationshipMetadata && $ inverseRelationshipMetadata ->isCollection ) {
341
- $ this ->tempAfterInstantiate [] = static function (object $ object ) use ($ collection , $ inverseRelationshipMetadata , $ field ) {
337
+ $ this ->tempAfterInstantiate [] = function (object $ object ) use ($ collection , $ inverseRelationshipMetadata , $ field ) {
342
338
$ inverseField = $ inverseRelationshipMetadata ->inverseField ;
343
339
344
- $ inverseObjects = $ collection ->withPersistMode (PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT )->create ([$ inverseField => $ object ]);
340
+ $ inverseObjects = $ collection ->withPersistMode (
341
+ $ this ->isPersisting () ? PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT : PersistMode::WITHOUT_PERSISTING
342
+ )
343
+ ->create ([$ inverseField => $ object ]);
345
344
346
- $ inverseObjects = unproxy ($ inverseObjects );
345
+ $ inverseObjects = unproxy ($ inverseObjects, withAutoRefresh: false );
347
346
348
347
// if the collection is indexed by a field, index the array
349
348
if ($ inverseRelationshipMetadata ->collectionIndexedBy ) {
@@ -379,9 +378,7 @@ protected function normalizeObject(object $object): object
379
378
return $ object ;
380
379
}
381
380
382
- if ($ object instanceof Proxy) {
383
- $ object = $ object ->_real (withAutoRefresh: false );
384
- }
381
+ $ object = unproxy ($ object , withAutoRefresh: false );
385
382
386
383
$ persistenceManager = $ configuration ->persistence ();
387
384
if (!$ persistenceManager ->hasPersistenceFor ($ object )) {
@@ -421,7 +418,7 @@ static function(object $object, array $parameters, PersistentObjectFactory $fact
421
418
}
422
419
);
423
420
424
- if (!Configuration::instance ()->hasEventDispatcher ()) {
421
+ if (!Configuration::isBooted () || !Configuration:: instance ()->hasEventDispatcher ()) {
425
422
return $ factory ;
426
423
}
427
424
@@ -456,4 +453,13 @@ private function throwIfCannotCreateObject(): void
456
453
457
454
throw new \LogicException (\sprintf ('Cannot create object in a data provider for non-proxy factories. Transform your factory into a "%s", or call "create()" method in the test. See https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#phpunit-data-providers ' , PersistentProxyObjectFactory::class));
458
455
}
456
+
457
+ private function isPersistenceEnabled (): bool
458
+ {
459
+ try {
460
+ return Configuration::instance ()->isPersistenceEnabled ();
461
+ } catch (FoundryNotBooted ) {
462
+ return false ;
463
+ }
464
+ }
459
465
}
0 commit comments