Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor: deprecate auto-persist configuration #818

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
->set('.zenstruck_foundry.persistence_strategy.mongo', MongoPersistenceStrategy::class)
->args([
service('doctrine_mongodb'),
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')

Expand Down
1 change: 0 additions & 1 deletion config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 1 addition & 9 deletions src/Persistence/PersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,8 @@
*/
abstract class PersistenceStrategy
{
/**
* @param array<string,mixed> $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'];
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ abstract class PersistentObjectFactory extends ObjectFactory
/** @var list<callable(T):void> */
private array $tempAfterInstantiate = [];

public function __construct()
{
parent::__construct();

$this->persist = Configuration::instance()->isPersistenceEnabled() ? PersistMode::PERSIST : PersistMode::WITHOUT_PERSISTING;
}

/**
* @phpstan-param mixed|Parameters $criteriaOrId
*
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
10 changes: 2 additions & 8 deletions src/ZenstruckFoundryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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'])
Expand All @@ -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'])
;
Expand Down