12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace Symfony\Bundle\DoctrineBundle\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 Doctrine\Common\Cli\Configuration;
- use Doctrine\Common\Cli\CliController as DoctrineCliController;
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Internal\CommitOrderCalculator;
- use Doctrine\ORM\Mapping\ClassMetadata;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * Load data fixtures from bundles.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @author Jonathan H. Wage <jonwage@gmail.com>
- */
- class LoadDataFixturesDoctrineCommand extends DoctrineCommand
- {
- protected function configure()
- {
- $this
- ->setName('doctrine:data:load')
- ->setDescription('Load data fixtures to your database.')
- ->addOption('fixtures', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'The directory or file to load data fixtures from.')
- ->addOption('append', null, InputOption::PARAMETER_OPTIONAL, 'Whether or not to append the data fixtures.', false)
- ->addOption('em', null, InputOption::PARAMETER_REQUIRED, 'The entity manager to use for this command.')
- ->setHelp(<<<EOT
- The <info>doctrine:data:load</info> command loads data fixtures from your bundles:
- <info>./symfony doctrine:data:load</info>
- You can also optionally specify the path to fixtures with the <info>--fixtures</info> option:
- <info>./symfony doctrine:data: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>./symfony doctrine:data:load --append</info>
- EOT
- );
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $emName = $input->getOption('em');
- $emName = $emName ? $emName : 'default';
- $emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName);
- $em = $this->container->get($emServiceName);
- $dirOrFile = $input->getOption('fixtures');
- if ($dirOrFile) {
- $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile);
- } else {
- $paths = array();
- $bundleDirs = $this->container->getKernelService()->getBundleDirs();
- foreach ($this->container->getKernelService()->getBundles() as $bundle) {
- $tmp = dirname(str_replace('\\', '/', get_class($bundle)));
- $namespace = str_replace('/', '\\', dirname($tmp));
- $class = basename($tmp);
- if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/DataFixtures/ORM')) {
- $paths[] = $dir;
- }
- }
- }
- $loader = new \Doctrine\Common\DataFixtures\Loader();
- foreach ($paths as $path) {
- $loader->loadFromDirectory($path);
- }
- $fixtures = $loader->getFixtures();
- $purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($em);
- $executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($em, $purger);
- $executor->execute($fixtures, $input->getOption('append'));
- }
- }
|