LoadDataFixturesDoctrineODMCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Finder\Finder;
  17. use Symfony\Component\HttpKernel\Util\Filesystem;
  18. use Symfony\Bundle\DoctrineAbstractBundle\Common\DataFixtures\Loader as DataFixturesLoader;
  19. use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
  20. use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
  21. use Doctrine\ODM\MongoDB\DocumentManager;
  22. use Doctrine\ODM\MongoDB\Internal\CommitOrderCalculator;
  23. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
  24. use InvalidArgumentException;
  25. /**
  26. * Load data fixtures from bundles.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com>
  29. * @author Jonathan H. Wage <jonwage@gmail.com>
  30. */
  31. class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand
  32. {
  33. protected function configure()
  34. {
  35. $this
  36. ->setName('doctrine:mongodb:data:load')
  37. ->setDescription('Load data fixtures to your database.')
  38. ->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory or file to load data fixtures from.')
  39. ->addOption('append', null, InputOption::VALUE_OPTIONAL, 'Whether or not to append the data fixtures.', false)
  40. ->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
  41. ->setHelp(<<<EOT
  42. The <info>doctrine:mongodb:data:load</info> command loads data fixtures from your bundles:
  43. <info>./app/console doctrine:mongodb:data:load</info>
  44. You can also optionally specify the path to fixtures with the <info>--fixtures</info> option:
  45. <info>./app/console doctrine:mongodb:data:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info>
  46. If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option:
  47. <info>./app/console doctrine:mongodb:data:load --append</info>
  48. EOT
  49. );
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output)
  52. {
  53. $dmName = $input->getOption('dm');
  54. $dmName = $dmName ? $dmName : 'default';
  55. $dmServiceName = sprintf('doctrine.odm.mongodb.%s_document_manager', $dmName);
  56. if (!$this->container->has($dmServiceName)) {
  57. throw new InvalidArgumentException(
  58. sprintf(
  59. 'Could not find a document manager configured with the name "%s". Check your '.
  60. 'application configuration to configure your Doctrine document managers.', $dmName
  61. )
  62. );
  63. }
  64. $dm = $this->container->get($dmServiceName);
  65. $dirOrFile = $input->getOption('fixtures');
  66. if ($dirOrFile) {
  67. $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile);
  68. } else {
  69. $paths = array();
  70. foreach ($this->container->get('kernel')->getBundles() as $bundle) {
  71. $paths[] = $bundle->getPath().'/DataFixtures/MongoDB';
  72. }
  73. }
  74. $loader = new DataFixturesLoader($this->container);
  75. foreach ($paths as $path) {
  76. if (is_dir($path)) {
  77. $loader->loadFromDirectory($path);
  78. }
  79. }
  80. $fixtures = $loader->getFixtures();
  81. if (!$fixtures) {
  82. throw new InvalidArgumentException(
  83. sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths))
  84. );
  85. }
  86. $purger = new MongoDBPurger($dm);
  87. $executor = new MongoDBExecutor($dm, $purger);
  88. $executor->setLogger(function($message) use ($output) {
  89. $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message));
  90. });
  91. $executor->execute($fixtures, $input->getOption('append'));
  92. }
  93. }