GenerateRepositoriesDoctrineODMCommand.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Doctrine\ODM\MongoDB\Tools\DocumentRepositoryGenerator;
  17. /**
  18. * Command to generate repository classes for mapping information.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. */
  23. class GenerateRepositoriesDoctrineODMCommand extends DoctrineODMCommand
  24. {
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('doctrine:mongodb:generate:repositories')
  29. ->setDescription('Generate repository classes from your mapping information.')
  30. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in.')
  31. ->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to generate the repository for (shortname without namespace).')
  32. ->setHelp(<<<EOT
  33. The <info>doctrine:mongodb:generate:repositories</info> command generates the configured document repository classes from your mapping information:
  34. <info>./app/console doctrine:mongodb:generate:repositories</info>
  35. EOT
  36. );
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output)
  39. {
  40. $bundleName = $input->getArgument('bundle');
  41. $filterDocument = $input->getOption('document');
  42. $foundBundle = $this->findBundle($bundleName);
  43. if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
  44. $output->writeln(sprintf('Generating document repositories for "<info>%s</info>"', $foundBundle->getName()));
  45. $generator = new DocumentRepositoryGenerator();
  46. foreach ($metadatas as $metadata) {
  47. if ($filterDocument && $filterDocument !== $metadata->reflClass->getShortname()) {
  48. continue;
  49. }
  50. if ($metadata->customRepositoryClassName) {
  51. if (strpos($metadata->customRepositoryClassName, $foundBundle->getNamespace()) === false) {
  52. throw new \RuntimeException(
  53. "Repository " . $metadata->customRepositoryClassName . " and bundle don't have a common namespace, ".
  54. "generation failed because the target directory cannot be detected.");
  55. }
  56. $output->writeln(sprintf(' > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
  57. $generator->writeDocumentRepositoryClass($metadata->customRepositoryClassName, $this->findBasePathForBundle($foundBundle));
  58. } else {
  59. $output->writeln(sprintf(' > <error>SKIP</error> no custom repository for <comment>%s</comment>', $metadata->name));
  60. }
  61. }
  62. } else {
  63. throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents.");
  64. }
  65. }
  66. }