Skip to content
Closed
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
45 changes: 33 additions & 12 deletions src/Command/LoadDataFixturesDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand;
use Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\PurgerFactoryCompilerPass;
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\Bundle\FixturesBundle\Purger\ORMPurgerFactory;
use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
Expand Down Expand Up @@ -49,26 +50,28 @@ protected function configure(): void
->addOption('purger', null, InputOption::VALUE_REQUIRED, 'The purger to use for this command', 'default')
->addOption('purge-exclusions', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'List of database tables to ignore while purging')
->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement')
->addOption('list', 'l', InputOption::VALUE_NONE, 'list fixtures in a table without running them, respects the --group filter')
->addOption('filter-name', 'f', InputOption::VALUE_REQUIRED, 'provide all or part of a fully-qualified class name of a fixture to filter to')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command loads data fixtures from your application:

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

Fixtures are services that are tagged with <comment>doctrine.fixture.orm</comment>.

If you want to append the fixtures instead of flushing the database first you can use the <comment>--append</comment> option:

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

By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from the database.
If you want to use a TRUNCATE statement instead you can use the <comment>--purge-with-truncate</comment> flag:

<info>php %command.full_name%</info> <comment>--purge-with-truncate</comment>

To execute only fixtures that live in a certain group, use:

<info>php %command.full_name%</info> <comment>--group=group1</comment>

EOT);
}

Expand All @@ -79,14 +82,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$em = $this->getDoctrine()->getManager($input->getOption('em'));
assert($em instanceof EntityManagerInterface);

if (! $input->getOption('append')) {
if (! $input->getOption('list') && ! $input->getOption('append')) {
if (! $ui->confirm(sprintf('Careful, database "%s" will be purged. Do you want to continue?', $em->getConnection()->getDatabase()), ! $input->isInteractive())) {
return 0;
}
}

$groups = $input->getOption('group');
$fixtures = $this->fixturesLoader->getFixtures($groups);
$groups = $input->getOption('group');
$filterName = $input->hasOption('filter-name') ? $input->getOption('filter-name') : null;
$fixtures = $this->fixturesLoader->getFixtures($groups, $filterName);

if ($input->getOption('list')) {
$ui->table(
['name', 'group', 'dependencies'],
array_map(
/** @param ORMFixtureInterface|FixtureGroupInterface|DependentFixtureInterface $fixture */
fn(ORMFixtureInterface $fixture) => [
$fixture::class,
implode(',', method_exists($fixture, 'getGroups') ? $fixture->getGroups() : []),
implode(',', method_exists($fixture, 'getDependencies') ? $fixture->getDependencies() : [])
],
$fixtures
)
);
return 0;
}

if (! $fixtures) {
$message = 'Could not find any fixture services to load';

Expand Down
9 changes: 6 additions & 3 deletions src/Loader/SymfonyFixturesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ protected function createFixture(string $class): FixtureInterface
*
* @return FixtureInterface[]
*/
public function getFixtures(array $groups = []): array
public function getFixtures(array $groups = [], ?string $filterName = null): array
{
$fixtures = parent::getFixtures();

if (empty($groups)) {
if (empty($groups) && is_null($filterName)) {
return $fixtures;
}

Expand All @@ -111,7 +111,10 @@ public function getFixtures(array $groups = []): array
$filteredFixtures = [];
foreach ($fixtures as $order => $fixture) {
$fixtureClass = $fixture::class;
if (isset($requiredFixtures[$fixtureClass])) {
if (
isset($requiredFixtures[$fixtureClass])
&& (is_null($filterName) || str_contains($fixtureClass, $filterName))
) {
$filteredFixtures[$order] = $fixture;
continue;
}
Expand Down