Skip to content

Commit 2ff49b7

Browse files
committed
Add completion values to --purge option
1 parent e0f2587 commit 2ff49b7

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Command/LoadDataFixturesDoctrineODMCommand.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoaderInterface;
88
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
99
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
10+
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
1011
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
1112
use Doctrine\ODM\MongoDB\DocumentManager;
13+
use InvalidArgumentException;
1214
use Psr\Log\AbstractLogger;
1315
use Symfony\Component\Console\Input\InputInterface;
1416
use Symfony\Component\Console\Input\InputOption;
1517
use Symfony\Component\Console\Output\OutputInterface;
1618
use Symfony\Component\Console\Question\ConfirmationQuestion;
1719
use Symfony\Component\Console\Style\SymfonyStyle;
1820

21+
use function array_column;
1922
use function assert;
23+
use function class_exists;
2024
use function implode;
21-
use function method_exists;
2225
use function sprintf;
2326

2427
/**
@@ -41,7 +44,7 @@ protected function configure(): void
4144
->addOption('group', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')
4245
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of flushing the database first.')
4346
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
44-
->addOption('purge-with-delete', null, InputOption::VALUE_NONE, 'Use deleteMany() to purge the collections instead of dropping them. This is useful to keep the collections, their metadata and their indexes, but it is slower than dropping them.')
47+
->addOption('purge', null, InputOption::VALUE_OPTIONAL, 'Purge the database before loading the fixtures. If set to "delete", collections will be kept and documents deleted instead of dropping the collections.', null, self::getPurgeModes(...))
4548
->setHelp(<<<'EOT'
4649
The <info>doctrine:mongodb:fixtures:load</info> command loads data fixtures from your application:
4750
@@ -55,9 +58,9 @@ protected function configure(): void
5558
5659
<info>php %command.full_name%</info> <comment>--group=group1</comment>
5760
58-
If the collection uses search indexes or encryption, you can use the <info>--purge-with-delete</info> option to keep the collections instead of dropping them when purging the database:
61+
If the collection uses search indexes or encryption, you can use the <info>--purge=delete</info> option to keep the collections instead of dropping them when purging the database:
5962
60-
<info>php %command.full_name%</info> --purge-with-delete
63+
<info>php %command.full_name%</info> --purge=delete
6164
EOT
6265
);
6366
}
@@ -69,9 +72,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6972

7073
$ui = new SymfonyStyle($input, $output);
7174

72-
if ($input->getOption('purge-with-delete')) {
75+
if ($input->hasOption('purge')) {
7376
if ($input->getOption('append')) {
74-
$ui->error('The --purge-with-delete option cannot be used with the --append option.');
77+
$ui->error('The --purge option cannot be used with the --append option.');
7578

7679
return self::INVALID;
7780
}
@@ -99,14 +102,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
99102
}
100103

101104
$purger = new MongoDBPurger($dm);
102-
if ($input->getOption('purge-with-delete')) {
103-
if (! method_exists($purger, 'setPurgeMode')) {
105+
$purge = $input->getOption('purge');
106+
if ($purge) {
107+
if (! class_exists(MongoDBPurgeMode::class)) {
104108
$ui->error('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');
105109

106110
return self::INVALID;
107111
}
108112

109-
$purger->setPurgeMode(MongoDBPurger::PURGE_MODE_DELETE);
113+
$purger->setPurgeMode(MongoDBPurgeMode::tryFrom($purge) ?? throw new InvalidArgumentException('Invalid purge mode: ' . $purge));
110114
}
111115

112116
$executor = new MongoDBExecutor($dm, $purger);
@@ -126,4 +130,9 @@ public function log($level, $message, array $context = []): void
126130

127131
return 0;
128132
}
133+
134+
private static function getPurgeModes(): array
135+
{
136+
return class_exists(MongoDBPurgeMode::class) ? array_column(MongoDBPurgeMode::cases(), 'value') : [];
137+
}
129138
}

0 commit comments

Comments
 (0)