* * 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 * @author Jonathan H. Wage */ class LoadDataFixturesDoctrineODMCommand extends DoctrineODMCommand { protected function configure() { $this ->setName('doctrine:mongodb: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('dm', null, InputOption::PARAMETER_REQUIRED, 'The document manager to use for this command.') ->setHelp(<<doctrine:mongodb:data:load command loads data fixtures from your bundles: ./symfony doctrine:mongodb:data:load You can also optionally specify the path to fixtures with the --fixtures option: ./symfony doctrine:mongodb:data: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: ./symfony doctrine:mongodb:data:load --append EOT ); } protected function execute(InputInterface $input, OutputInterface $output) { $dmName = $input->getOption('dm'); $dmName = $dmName ? $dmName : 'default'; $dmServiceName = sprintf('doctrine.odm.mongodb.%s_document_manager', $dmName); $dm = $this->container->get($dmServiceName); $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/MongoDB')) { $paths[] = $dir; } } } $loader = new \Doctrine\Common\DataFixtures\Loader(); foreach ($paths as $path) { $loader->loadFromDirectory($path); } $fixtures = $loader->getFixtures(); $purger = new \Doctrine\Common\DataFixtures\Purger\MongoDBPurger($dm); $executor = new \Doctrine\Common\DataFixtures\Executor\MongoDBExecutor($dm, $purger); $executor->execute($fixtures, $input->getOption('append')); } }