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

Remove --service option from doctrine:mongodb:fixtures:load command #799

Merged
merged 1 commit into from
Dec 10, 2023
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
35 changes: 3 additions & 32 deletions Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoaderInterface;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;

use function class_exists;
use function implode;
use function sprintf;
use function trigger_deprecation;

/**
* Load data fixtures from bundles.
Expand All @@ -29,22 +24,16 @@
*/
class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand
{
public function __construct(?ManagerRegistry $registry = null, ?KernelInterface $kernel = null, private ?SymfonyFixturesLoaderInterface $fixturesLoader = null)
public function __construct(ManagerRegistry $registry, private SymfonyFixturesLoaderInterface $fixturesLoader)
{
parent::__construct($registry);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command is removed in the DI extension instead of being disabled, so the code is more centralized and the dependency to the loader can be required in the constructor.

public function isEnabled(): bool
{
return parent::isEnabled() && class_exists(Loader::class);
}

protected function configure(): void
{
$this
->setName('doctrine:mongodb:fixtures:load')
->setDescription('Load data fixtures to your database.')
->addOption('services', null, InputOption::VALUE_NONE, 'Use services as fixtures')
->addOption('group', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of flushing the database first.')
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
Expand All @@ -57,14 +46,9 @@ protected function configure(): void

<info>php %command.full_name%</info> --append</info>

You can also choose to load only fixtures that live in a certain group:

Alternatively, you can also load fixture services instead of files. Fixture services are tagged with `<comment>doctrine.fixture.odm.mongodb</comment>`.
Using `<comment>--services</comment>` will be the default behaviour in 5.0.
When loading fixture services, you can also choose to load only fixtures that live in a certain group:
`<info>php %command.full_name%</info> <comment>--group=group1</comment> <comment>--services</comment>`



<info>php %command.full_name%</info> <comment>--group=group1</comment>
EOT
);
}
Expand All @@ -74,15 +58,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dm = $this->getManagerRegistry()->getManager($input->getOption('dm'));
$ui = new SymfonyStyle($input, $output);

if ($input->getOption('services')) {
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.0',
'The "services" option to the "%s" command is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
$this->getName()
);
}

if ($input->isInteractive() && ! $input->getOption('append')) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Careful, database will be purged. Do you want to continue (y/N) ?', false);
Expand All @@ -92,10 +67,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if (! $this->fixturesLoader) {
throw new RuntimeException('Cannot use fixture services without injecting a fixtures loader.');
}

$groups = $input->getOption('group');
$fixtures = $this->fixturesLoader->getFixtures($groups);
if (! $fixtures) {
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Bundle\MongoDBBundle\Attribute\AsDocumentListener;
use Doctrine\Bundle\MongoDBBundle\Attribute\MapDocument;
use Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface;
Expand Down Expand Up @@ -98,6 +99,7 @@
->addTag(FixturesCompilerPass::FIXTURE_TAG);
} else {
$container->removeDefinition('doctrine_mongodb.odm.symfony.fixtures.loader');
$container->removeDefinition(LoadDataFixturesDoctrineODMCommand::class);

Check warning on line 102 in DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/DoctrineMongoDBExtension.php#L102

Added line #L102 was not covered by tests
}

// load the connections
Expand Down
4 changes: 2 additions & 2 deletions Loader/SymfonyFixturesLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
interface SymfonyFixturesLoaderInterface
{
/**
* Add multple fixtures
* Add multiple fixtures
*
* @internal
*
* @param array $fixtures
* @param list<array{fixture: FixtureInterface, groups: string[]}> $fixtures
*/
public function addFixtures(array $fixtures);

Expand Down
3 changes: 1 addition & 2 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
</service>
<service id="Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand" class="Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand">
<argument type="service" id="doctrine_mongodb"/>
<argument type="service" id="kernel" on-invalid="null"/>
<argument type="service" id="doctrine_mongodb.odm.symfony.fixtures.loader" on-invalid="null" />
<argument type="service" id="doctrine_mongodb.odm.symfony.fixtures.loader" />

<tag name="console.command" command="doctrine:mongodb:fixtures:load"/>
</service>
Expand Down
3 changes: 2 additions & 1 deletion UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ UPGRADE FROM 4.x to 5.0
removed without replacement.
* The `Doctrine\Bundle\MongoDBBundle\Command\DoctrineODMCommand` class is now
`@internal`, you should not extend from this class.
* Remove support of Annotation mapping, you should use Attributes or XML instead.
* Remove support of Annotation mapping, you should use Attributes or XML instead.
* Remove `--service` option from `doctrine:mongodb:fixtures:load` command