DoctrineODMCommand.php 1.0 KB

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\Command;
  4. use Symfony\Bundle\FrameworkBundle\Console\Application;
  5. use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
  6. /**
  7. * Base class for Doctrine ODM console commands to extend.
  8. *
  9. * @author Justin Hileman <justin@shopopensky.com>
  10. */
  11. abstract class DoctrineODMCommand extends Command
  12. {
  13. public static function setApplicationDocumentManager(Application $application, $dmName)
  14. {
  15. $container = $application->getKernel()->getContainer();
  16. $dmName = $dmName ? $dmName : 'default';
  17. $dmServiceName = sprintf('doctrine.odm.mongodb.%s_document_manager', $dmName);
  18. if (!$container->has($dmServiceName)) {
  19. throw new \InvalidArgumentException(sprintf('Could not find Doctrine ODM DocumentManager named "%s"', $dmName));
  20. }
  21. $dm = $container->get($dmServiceName);
  22. $helperSet = $application->getHelperSet();
  23. $helperSet->set(new DocumentManagerHelper($dm), 'dm');
  24. }
  25. }