InfoDoctrineCommand.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Show information about mapped entities
  17. *
  18. * @author Benjamin Eberlei <kontakt@beberlei.de>
  19. */
  20. class InfoDoctrineCommand extends DoctrineCommand
  21. {
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('doctrine:mapping:info')
  26. ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
  27. ->setDescription('Show basic information about all mapped entities')
  28. ->setHelp(<<<EOT
  29. The <info>doctrine:mapping:info</info> shows basic information about which
  30. entities exist and possibly if their mapping information contains errors or
  31. not.
  32. <info>./app/console doctrine:mapping:info</info>
  33. If you are using multiple entity managers you can pick your choice with the
  34. <info>--em</info> option:
  35. <info>./app/console doctrine:mapping:info --em=default</info>
  36. EOT
  37. );
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. $entityManagerName = $input->getOption('em') ? $input->getOption('em') : $this->container->get('doctrine')->getDefaultEntityManagerName();
  42. /* @var $entityManager Doctrine\ORM\EntityManager */
  43. $entityManager = $this->getEntityManager($input->getOption('em'));
  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 directory.'
  53. );
  54. }
  55. $output->writeln(sprintf("Found <info>%d</info> entities mapped in entity manager <info>%s</info>:", count($entityClassNames), $entityManagerName));
  56. foreach ($entityClassNames as $entityClassName) {
  57. try {
  58. $cm = $entityManager->getClassMetadata($entityClassName);
  59. $output->writeln(sprintf("<info>[OK]</info> %s", $entityClassName));
  60. } catch (MappingException $e) {
  61. $output->writeln("<error>[FAIL]</error> ".$entityClassName);
  62. $output->writeln(sprintf("<comment>%s</comment>", $e->getMessage()));
  63. $output->writeln('');
  64. }
  65. }
  66. }
  67. }