InfoDoctrineODMCommand.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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 Doctrine\ODM\MongoDB\Mapping\MappingException;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * Show information about mapped documents
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. * @author Jonathan H. Wage <jonwage@gmail.com>
  21. */
  22. class InfoDoctrineODMCommand extends DoctrineODMCommand
  23. {
  24. protected function configure()
  25. {
  26. $this
  27. ->setName('doctrine:mongodb:mapping:info')
  28. ->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
  29. ->setDescription('Show basic information about all mapped documents.')
  30. ->setHelp(<<<EOT
  31. The <info>doctrine:mapping:info</info> shows basic information about which
  32. documents exist and possibly if their mapping information contains errors or not.
  33. <info>./app/console doctrine:mapping:info</info>
  34. If you are using multiple document managers you can pick your choice with the <info>--dm</info> option:
  35. <info>./app/console doctrine:mapping:info --dm=default</info>
  36. EOT
  37. );
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. $documentManagerName = $input->getOption('dm') ?
  42. $input->getOption('dm') :
  43. $this->container->getParameter('doctrine.odm.mongodb.default_document_manager');
  44. $documentManagerService = sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName);
  45. /* @var $documentManager Doctrine\ODM\MongoDB\DocumentManager */
  46. $documentManager = $this->container->get($documentManagerService);
  47. $documentClassNames = $documentManager->getConfiguration()
  48. ->getMetadataDriverImpl()
  49. ->getAllClassNames();
  50. if (!$entityClassNames) {
  51. throw new \Exception(
  52. 'You do not have any mapped Doctrine MongoDB ODM documents for any of your bundles. '.
  53. 'Create a class inside the Document namespace of any of your bundles and provide '.
  54. 'mapping information for it with Annotations directly in the classes doc blocks '.
  55. 'or with XML/YAML in your bundles Resources/config/doctrine/metadata/mongodb directory.'
  56. );
  57. }
  58. $output->write(sprintf("Found <info>%d</info> documents mapped in document manager <info>%s</info>:\n",
  59. count($documentClassNames), $documentManagerName), true);
  60. foreach ($documentClassNames AS $documentClassName) {
  61. try {
  62. $cm = $documentManager->getClassMetadata($documentClassName);
  63. $output->write("<info>[OK]</info> " . $documentClassName, true);
  64. } catch(MappingException $e) {
  65. $output->write("<error>[FAIL]</error> " . $documentClassName, true);
  66. $output->write("<comment>" . $e->getMessage()."</comment>", true);
  67. $output->write("", true);
  68. }
  69. }
  70. }
  71. }