Skip to content

Instantly share code, notes, and snippets.

@odracci
Last active December 18, 2015 10:19
Show Gist options
  • Save odracci/5767336 to your computer and use it in GitHub Desktop.
Save odracci/5767336 to your computer and use it in GitHub Desktop.
LoadDataFixturesCommand with Nelmio/Alice
<?php
namespace Vendor\Bundle\Command;
use Nelmio\Alice\Fixtures;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Symfony\Component\Finder\Finder;
class LoadDataFixturesCommand extends DoctrineCommand
{
protected function configure()
{
$this
->setName('doctrine: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(
'append',
null,
InputOption::VALUE_NONE,
'Append the data fixtures instead of deleting all data from the database first.'
)
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.')
->addOption(
'purge-with-truncate',
null,
InputOption::VALUE_NONE,
'Purge data by using a database-level TRUNCATE statement'
)
->setHelp(
<<<EOT
The <info>doctrine:fixtures:load</info> command loads data fixtures from your bundles:
<info>./app/console doctrine:fixtures:load</info>
You can also optionally specify the file to fixtures with the <info>--fixtures</info> option:
<info>./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1.yml --fixtures=/path/to/fixtures2.yml</info>
If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option:
<info>./app/console doctrine:fixtures:load --append</info>
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 <info>--purge-with-truncate</info> flag:
<info>./app/console doctrine:fixtures:load --purge-with-truncate</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $doctrine \Doctrine\Common\Persistence\ManagerRegistry */
$doctrine = $this->getContainer()->get('doctrine');
$em = $doctrine->getManager($input->getOption('em'));
if ($input->isInteractive() && !$input->getOption('append')) {
$dialog = $this->getHelperSet()->get('dialog');
if (!$dialog->askConfirmation(
$output,
'<question>Careful, database will be purged. Do you want to continue Y/N ?</question>',
false
)
) {
return;
}
}
$finder = new Finder();
$dirOrFile = $input->getOption('fixtures');
if ($dirOrFile) {
$fixtureFiles = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile);
} else {
$fixtureFiles = array();
$fixturesPath = __DIR__ . '/../Resources/fixtures';
if (is_dir($fixturesPath)) {
$finder->files()->in($fixturesPath)->name('*.yml');
/** @var \Symfony\Component\Finder\SplFileInfo $fixtureFilename */
foreach($finder as $fixtureFilename) {
$fixtureFiles[] = $fixtureFilename->getRealPath();
}
}
}
if (!$input->getOption('append')) {
$purger = new ORMPurger($em);
$purger->setPurgeMode(
$input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE
);
$purger->purge();
}
Fixtures::load($fixtureFiles, $em);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment