Skip to content
Merged
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
84 changes: 14 additions & 70 deletions Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
use const E_USER_DEPRECATED;
use function class_exists;
use function implode;
use function is_array;
use function is_dir;
use function is_file;
use function sprintf;
use function trigger_error;

Expand All @@ -30,17 +27,13 @@
*/
class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand
{
/** @var KernelInterface|null */
private $kernel;

/** @var SymfonyFixturesLoaderInterface */
private $fixturesLoader;

public function __construct(?ManagerRegistry $registry = null, ?KernelInterface $kernel = null, ?SymfonyFixturesLoaderInterface $fixturesLoader = null)
{
parent::__construct($registry);

$this->kernel = $kernel;
$this->fixturesLoader = $fixturesLoader;
}

Expand All @@ -57,8 +50,6 @@ protected function configure()
$this
->setName('doctrine:mongodb:fixtures:load')
->setDescription('Load data fixtures to your database.')
->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory or file to load data fixtures from.')
->addOption('bundles', 'b', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The bundles to load data fixtures from.')
->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.')
Expand Down Expand Up @@ -93,11 +84,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dm = $this->getManagerRegistry()->getManager($input->getOption('dm'));
$ui = new SymfonyStyle($input, $output);

$dirOrFile = $input->getOption('fixtures');
$bundles = $input->getOption('bundles');
$services = (bool) $input->getOption('services');
if (($services && $bundles) || ($services && $dirOrFile) || ($bundles && $dirOrFile)) {
throw new InvalidArgumentException('Can only use one of "--bundles", "--fixtures", "--services".');
if ((bool) $input->getOption('services')) {
@trigger_error(sprintf('The "services" option to the "%s" command is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.', $this->getName()), E_USER_DEPRECATED);
}

if ($input->isInteractive() && ! $input->getOption('append')) {
Expand All @@ -109,57 +97,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if (! $services) {
@trigger_error('--bundles and --fixtures are deprecated since doctrine/mongodb-odm-bundle 3.6 and will be removed in 4.0. Use --services instead.', E_USER_DEPRECATED);

if ($dirOrFile) {
$paths = is_array($dirOrFile) ? $dirOrFile : [$dirOrFile];
} elseif ($bundles) {
$paths = [$this->getKernel()->getProjectDir() . '/DataFixtures/MongoDB'];
foreach ($bundles as $bundle) {
$paths[] = $this->getKernel()->getBundle($bundle)->getPath();
}
} else {
$paths = $this->container->getParameter('doctrine_mongodb.odm.fixtures_dirs');
$paths = is_array($paths) ? $paths : [$paths];
$paths[] = $this->getKernel()->getProjectDir() . '/DataFixtures/MongoDB';
foreach ($this->getKernel()->getBundles() as $bundle) {
$paths[] = $bundle->getPath() . '/DataFixtures/MongoDB';
}
}
$loaderClass = $this->container->getParameter('doctrine_mongodb.odm.fixture_loader');
$loader = new $loaderClass($this->container);
foreach ($paths as $path) {
if (is_dir($path)) {
$loader->loadFromDirectory($path);
} elseif (is_file($path)) {
$loader->loadFromFile($path);
}
}
$fixtures = $loader->getFixtures();
if (! $fixtures) {
throw new InvalidArgumentException(
sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))
);
}
} else {
if (! $this->fixturesLoader) {
throw new RuntimeException('Cannot use fixture services without injecting a fixtures loader.');
}
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) {
$message = 'Could not find any fixture services to load';
$groups = $input->getOption('group');
$fixtures = $this->fixturesLoader->getFixtures($groups);
if (! $fixtures) {
$message = 'Could not find any fixture services to load';

if (! empty($groups)) {
$message .= sprintf(' in the groups (%s)', implode(', ', $groups));
}
if (! empty($groups)) {
$message .= sprintf(' in the groups (%s)', implode(', ', $groups));
}

$ui->error($message . '.');
$ui->error($message . '.');

return 1;
}
return 1;
}

$purger = new MongoDBPurger($dm);
Expand All @@ -169,13 +122,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
});
$executor->execute($fixtures, $input->getOption('append'));
}

private function getKernel()
{
if ($this->kernel === null) {
$this->kernel = $this->container->get('kernel');
}

return $this->kernel;
}
}