diff --git a/config/mongo.php b/config/mongo.php index 172f1bd0..757e50ab 100644 --- a/config/mongo.php +++ b/config/mongo.php @@ -11,7 +11,6 @@ ->set('.zenstruck_foundry.persistence_strategy.mongo', MongoPersistenceStrategy::class) ->args([ service('doctrine_mongodb'), - abstract_arg('config'), ]) ->tag('.foundry.persistence_strategy') diff --git a/config/orm.php b/config/orm.php index 10c2f95b..1fd3e4c4 100644 --- a/config/orm.php +++ b/config/orm.php @@ -15,7 +15,6 @@ ->set('.zenstruck_foundry.persistence_strategy.orm', DoctrineOrmVersionGuesser::isOrmV3() ? OrmV3PersistenceStrategy::class : OrmV2PersistenceStrategy::class) ->args([ service('doctrine'), - abstract_arg('config'), ]) ->tag('.foundry.persistence_strategy') diff --git a/phpstan.neon b/phpstan.neon index 0025a4e0..8b300e02 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -46,7 +46,6 @@ parameters: - identifier: property.uninitializedReadonlyByPhpDoc paths: - src/Factory.php - - src/Persistence/PersistentObjectFactory.php # Hydrator and Factory are annotated @immutable - identifier: property.readOnlyByPhpDocAssignNotInConstructor diff --git a/src/Persistence/PersistenceManager.php b/src/Persistence/PersistenceManager.php index aaf4d621..ba86d4f8 100644 --- a/src/Persistence/PersistenceManager.php +++ b/src/Persistence/PersistenceManager.php @@ -233,18 +233,6 @@ public function truncate(string $class): void $this->strategyFor($class)->truncate($class); } - /** - * @param class-string $class - */ - public function autoPersist(string $class): bool - { - try { - return $this->strategyFor(unproxy($class))->autoPersist(); - } catch (NoPersistenceStrategy) { - return false; - } - } - /** * @template T of object * diff --git a/src/Persistence/PersistenceStrategy.php b/src/Persistence/PersistenceStrategy.php index 85607ccd..0eb89907 100644 --- a/src/Persistence/PersistenceStrategy.php +++ b/src/Persistence/PersistenceStrategy.php @@ -23,16 +23,8 @@ */ abstract class PersistenceStrategy { - /** - * @param array $config - */ - public function __construct(protected readonly ManagerRegistry $registry, protected readonly array $config) - { - } - - public function autoPersist(): bool + public function __construct(protected readonly ManagerRegistry $registry) { - return $this->config['auto_persist']; } /** diff --git a/src/Persistence/PersistentObjectFactory.php b/src/Persistence/PersistentObjectFactory.php index 3ce4e606..767e1aa8 100644 --- a/src/Persistence/PersistentObjectFactory.php +++ b/src/Persistence/PersistentObjectFactory.php @@ -44,6 +44,13 @@ abstract class PersistentObjectFactory extends ObjectFactory /** @var list */ private array $tempAfterInstantiate = []; + public function __construct() + { + parent::__construct(); + + $this->persist = Configuration::instance()->isPersistenceEnabled() ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING; + } + /** * @phpstan-param mixed|Parameters $criteriaOrId * @@ -274,13 +281,7 @@ final public function afterPersist(callable $callback): static */ public function persistMode(): PersistMode { - $config = Configuration::instance(); - - if (!$config->isPersistenceEnabled()) { - return PersistMode::WITHOUT_PERSISTING; - } - - return $this->persist ?? ($config->persistence()->autoPersist(static::class()) ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING); + return Configuration::instance()->isPersistenceEnabled() ? $this->persist : PersistMode::WITHOUT_PERSISTING; } protected function normalizeParameter(string $field, mixed $value): mixed @@ -289,7 +290,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed return unproxy(parent::normalizeParameter($field, $value)); } - if ($value instanceof self && isset($this->persist)) { + if ($value instanceof self) { $value = $value->withPersistMode($this->persist); } diff --git a/src/ZenstruckFoundryBundle.php b/src/ZenstruckFoundryBundle.php index 3036897c..8c8de70d 100644 --- a/src/ZenstruckFoundryBundle.php +++ b/src/ZenstruckFoundryBundle.php @@ -105,6 +105,7 @@ public function configure(DefinitionConfigurator $definition): void ->booleanNode('auto_persist') ->info('Automatically persist entities when created.') ->defaultTrue() + ->setDeprecated('zenstruck/foundry', '2.4', 'Since 2.4 auto_persist defaults to true and this configuration has no effect.') ->end() ->arrayNode('reset') ->addDefaultsIfNotSet() @@ -160,6 +161,7 @@ public function configure(DefinitionConfigurator $definition): void ->booleanNode('auto_persist') ->info('Automatically persist documents when created.') ->defaultTrue() + ->setDeprecated('zenstruck/foundry', '2.4', 'Since 2.4 auto_persist defaults to true and this configuration has no effect.') ->end() ->arrayNode('reset') ->addDefaultsIfNotSet() @@ -251,10 +253,6 @@ public function loadExtension(array $config, ContainerConfigurator $configurator if (isset($bundles['DoctrineBundle'])) { $configurator->import('../config/orm.php'); - $container->getDefinition('.zenstruck_foundry.persistence_strategy.orm') - ->replaceArgument(1, $config['orm']) - ; - $container->getDefinition('.zenstruck_foundry.persistence.database_resetter.orm.abstract') ->replaceArgument('$managers', $config['orm']['reset']['entity_managers']) ->replaceArgument('$connections', $config['orm']['reset']['connections']) @@ -280,10 +278,6 @@ public function loadExtension(array $config, ContainerConfigurator $configurator if (isset($bundles['DoctrineMongoDBBundle'])) { $configurator->import('../config/mongo.php'); - $container->getDefinition('.zenstruck_foundry.persistence_strategy.mongo') - ->replaceArgument(1, $config['mongo']) - ; - $container->getDefinition(MongoResetter::class) ->replaceArgument(0, $config['mongo']['reset']['document_managers']) ;