Created
May 19, 2011 11:13
-
-
Save anonymous/980546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Doctrine\ORM\Tools\Console\Command; | |
use Symfony\Component\Console\Input\InputArgument, | |
Symfony\Component\Console\Input\InputOption, | |
Symfony\Component\Console; | |
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | |
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | |
use Doctrine\Common\DataFixtures\Loader as DataFixturesLoader; | |
use InvalidArgumentException; | |
class DataFixturesCommand extends Console\Command\Command | |
{ | |
/** | |
* @see Console\Command\Command | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('orm: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 flushing the database first.') | |
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') | |
->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 path to fixtures with the <info>--fixtures</info> option: | |
<info>./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</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> | |
EOT | |
); | |
} | |
/** | |
* @see Console\Command\Command | |
*/ | |
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) | |
{ | |
$em = $this->getHelper('em')->getEntityManager(); | |
$dirOrFile = $input->getOption('fixtures'); | |
if ($dirOrFile) { | |
$paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); | |
} | |
if (!$paths) { | |
throw new InvalidArgumentException( | |
sprintf('You must specify fixtures path') | |
); | |
} | |
$loader = new DataFixturesLoader($this->container); | |
foreach ($paths as $path) { | |
if (is_dir($path)) { | |
$loader->loadFromDirectory($path); | |
} | |
} | |
$fixtures = $loader->getFixtures(); | |
if (!$fixtures) { | |
throw new InvalidArgumentException( | |
sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths)) | |
); | |
} | |
$purger = new ORMPurger($em); | |
$executor = new ORMExecutor($em, $purger); | |
$executor->setLogger(function($message) use ($output) { | |
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); | |
}); | |
$executor->execute($fixtures, $input->getOption('append')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment