LoadDataFixturesDoctrineCommand.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Symfony\Bundle\DoctrineBundle\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Output\Output;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Bundle\FrameworkBundle\Util\Filesystem;
  10. use Doctrine\Common\Cli\Configuration;
  11. use Doctrine\Common\Cli\CliController as DoctrineCliController;
  12. use Doctrine\ORM\EntityManager;
  13. use Doctrine\ORM\Internal\CommitOrderCalculator;
  14. use Doctrine\ORM\Mapping\ClassMetadata;
  15. /*
  16. * This file is part of the Symfony framework.
  17. *
  18. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  19. *
  20. * This source file is subject to the MIT license that is bundled
  21. * with this source code in the file LICENSE.
  22. */
  23. /**
  24. * Load data fixtures from bundles.
  25. *
  26. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  27. * @author Jonathan H. Wage <jonwage@gmail.com>
  28. */
  29. class LoadDataFixturesDoctrineCommand extends DoctrineCommand
  30. {
  31. protected function configure()
  32. {
  33. $this
  34. ->setName('doctrine:data:load')
  35. ->setDescription('Load data fixtures to your database.')
  36. ->addOption('fixtures', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'The directory or file to load data fixtures from.')
  37. ->addOption('append', null, InputOption::PARAMETER_OPTIONAL, 'Whether or not to append the data fixtures.', false)
  38. ->addOption('em', null, InputOption::PARAMETER_REQUIRED, 'The entity manager to use for this command.')
  39. ->setHelp(<<<EOT
  40. The <info>doctrine:data:load</info> command loads data fixtures from your bundles:
  41. <info>./symfony doctrine:data:load</info>
  42. You can also optionally specify the path to fixtures with the <info>--fixtures</info> option:
  43. <info>./symfony doctrine:data:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info>
  44. If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option:
  45. <info>./symfony doctrine:data:load --append</info>
  46. EOT
  47. );
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $emName = $input->getOption('em');
  52. $emName = $emName ? $emName : 'default';
  53. $emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName);
  54. $em = $this->container->get($emServiceName);
  55. $dirOrFile = $input->getOption('fixtures');
  56. if ($dirOrFile) {
  57. $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile);
  58. } else {
  59. $paths = array();
  60. $bundleDirs = $this->container->getKernelService()->getBundleDirs();
  61. foreach ($this->container->getKernelService()->getBundles() as $bundle) {
  62. $tmp = dirname(str_replace('\\', '/', get_class($bundle)));
  63. $namespace = str_replace('/', '\\', dirname($tmp));
  64. $class = basename($tmp);
  65. if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/DataFixtures/ORM')) {
  66. $paths[] = $dir;
  67. }
  68. }
  69. }
  70. $loader = new \Doctrine\Common\DataFixtures\Loader();
  71. foreach ($paths as $path) {
  72. $loader->loadFromDirectory($path);
  73. }
  74. $fixtures = $loader->getFixtures();
  75. $purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($em);
  76. $executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($em, $purger);
  77. $executor->execute($fixtures, $input->getOption('append'));
  78. }
  79. }