InfoDoctrineCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\DoctrineBundle\Command;
  11. use Doctrine\ORM\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 entities
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. */
  21. class InfoDoctrineCommand extends DoctrineCommand
  22. {
  23. protected function configure()
  24. {
  25. $this
  26. ->setName('doctrine:mapping:info')
  27. ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
  28. ->setDescription('Show basic information about all mapped entities.')
  29. ->setHelp(<<<EOT
  30. The <info>doctrine:mapping:info</info> shows basic information about which
  31. entities exist and possibly if their mapping information contains errors or not.
  32. <info>./app/console doctrine:mapping:info</info>
  33. If you are using multiple entity managers you can pick your choice with the <info>--em</info> option:
  34. <info>./app/console doctrine:mapping:info --em=default</info>
  35. EOT
  36. );
  37. }
  38. protected function execute(InputInterface $input, OutputInterface $output)
  39. {
  40. $entityManagerName = $input->getOption('em') ? $input->getOption('em') : $this->container->getParameter('doctrine.orm.default_entity_manager');
  41. $this->setApplicationEntityManager($input->getOption('em'));
  42. /* @var $entityManager Doctrine\ORM\EntityManager */
  43. $entityManager = $this->getHelper('em')->getEntityManager();
  44. $entityClassNames = $entityManager->getConfiguration()
  45. ->getMetadataDriverImpl()
  46. ->getAllClassNames();
  47. if (!$entityClassNames) {
  48. throw new \Exception(
  49. 'You do not have any mapped Doctrine ORM entities for any of your bundles. '.
  50. 'Create a class inside the Entity namespace of any of your bundles and provide '.
  51. 'mapping information for it with Annotations directly in the classes doc blocks '.
  52. 'or with XML/YAML in your bundles Resources/config/doctrine/metadata/orm directory.'
  53. );
  54. }
  55. $output->write(sprintf("Found <info>%d</info> entities mapped in entity manager <info>%s</info>:\n",
  56. count($entityClassNames), $entityManagerName), true);
  57. foreach ($entityClassNames as $entityClassName) {
  58. try {
  59. $cm = $entityManager->getClassMetadata($entityClassName);
  60. $output->write("<info>[OK]</info> " . $entityClassName, true);
  61. } catch (MappingException $e) {
  62. $output->write("<error>[FAIL]</error> " . $entityClassName, true);
  63. $output->write("<comment>" . $e->getMessage()."</comment>", true);
  64. $output->write("", true);
  65. }
  66. }
  67. }
  68. }