diff -r b36a42d260d8 -r 624e5900f5a4 vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Command/LoadDataFixturesDoctrineCommand.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/bundles/Symfony/Bundle/DoctrineFixturesBundle/Command/LoadDataFixturesDoctrineCommand.php Wed Dec 14 23:28:58 2011 +0100 @@ -0,0 +1,114 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\DoctrineFixturesBundle\Command; + +use Symfony\Component\Console\Input\InputArgument; +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 Symfony\Component\Finder\Finder; +use Symfony\Bundle\FrameworkBundle\Util\Filesystem; +use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader as DataFixturesLoader; +use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand; +use Doctrine\Common\DataFixtures\Executor\ORMExecutor; +use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Internal\CommitOrderCalculator; +use Doctrine\ORM\Mapping\ClassMetadata; +use InvalidArgumentException; + +/** + * Load data fixtures from bundles. + * + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class LoadDataFixturesDoctrineCommand 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(<<doctrine:fixtures:load command loads data fixtures from your bundles: + + ./app/console doctrine:fixtures:load + +You can also optionally specify the path to fixtures with the --fixtures option: + + ./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2 + +If you want to append the fixtures instead of flushing the database first you can use the --append option: + + ./app/console doctrine:fixtures:load --append + +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 --purge-with-truncate flag: + + ./app/console doctrine:fixtures:load --purge-with-truncate +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $emName = $input->getOption('em'); + $emName = $emName ? $emName : 'default'; + $emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName); + + if (!$this->getContainer()->has($emServiceName)) { + throw new InvalidArgumentException( + sprintf( + 'Could not find an entity manager configured with the name "%s". Check your '. + 'application configuration to configure your Doctrine entity managers.', $emName + ) + ); + } + + $em = $this->getContainer()->get($emServiceName); + $dirOrFile = $input->getOption('fixtures'); + if ($dirOrFile) { + $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); + } else { + $paths = array(); + foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { + $paths[] = $bundle->getPath().'/DataFixtures/ORM'; + } + } + + $loader = new DataFixturesLoader($this->getContainer()); + 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); + $purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); + $executor = new ORMExecutor($em, $purger); + $executor->setLogger(function($message) use ($output) { + $output->writeln(sprintf(' > %s', $message)); + }); + $executor->execute($fixtures, $input->getOption('append')); + } +}