InfoDoctrineCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\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') ?
  41. $input->getOption('em') :
  42. $this->container->getParameter('doctrine.orm.default_entity_manager');
  43. $entityManagerService = sprintf('doctrine.orm.%s_entity_manager', $entityManagerName);
  44. /* @var $entityManager Doctrine\ORM\EntityManager */
  45. $entityManager = $this->container->get($entityManagerService);
  46. $entityClassNames = $entityManager->getConfiguration()
  47. ->getMetadataDriverImpl()
  48. ->getAllClassNames();
  49. $output->write(sprintf("Found %d entities mapped in entity manager '%s':",
  50. count($entityClassNames), $entityManagerName), true);
  51. foreach ($entityClassNames as $entityClassName) {
  52. try {
  53. $cm = $entityManager->getClassMetadata($entityClassName);
  54. $output->write("<info>[OK]</info> " . $entityClassName, true);
  55. } catch (MappingException $e) {
  56. $output->write("<error>[FAIL]</error> " . $entityClassName, true);
  57. $output->write("<comment>" . $e->getMessage()."</comment>", true);
  58. $output->write("", true);
  59. }
  60. }
  61. }
  62. }