vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/InfoDoctrineCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\DoctrineBundle\Command;
       
    13 
       
    14 use Doctrine\ORM\Mapping\MappingException;
       
    15 use Symfony\Component\Console\Input\InputOption;
       
    16 use Symfony\Component\Console\Input\InputInterface;
       
    17 use Symfony\Component\Console\Output\OutputInterface;
       
    18 
       
    19 /**
       
    20  * Show information about mapped entities
       
    21  *
       
    22  * @author Benjamin Eberlei <kontakt@beberlei.de>
       
    23  */
       
    24 class InfoDoctrineCommand extends DoctrineCommand
       
    25 {
       
    26     protected function configure()
       
    27     {
       
    28         $this
       
    29             ->setName('doctrine:mapping:info')
       
    30             ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
       
    31             ->setDescription('Show basic information about all mapped entities')
       
    32             ->setHelp(<<<EOT
       
    33 The <info>doctrine:mapping:info</info> shows basic information about which
       
    34 entities exist and possibly if their mapping information contains errors or
       
    35 not.
       
    36 
       
    37 <info>php app/console doctrine:mapping:info</info>
       
    38 
       
    39 If you are using multiple entity managers you can pick your choice with the
       
    40 <info>--em</info> option:
       
    41 
       
    42 <info>php app/console doctrine:mapping:info --em=default</info>
       
    43 EOT
       
    44         );
       
    45     }
       
    46 
       
    47     protected function execute(InputInterface $input, OutputInterface $output)
       
    48     {
       
    49         $entityManagerName = $input->getOption('em') ? $input->getOption('em') : $this->getContainer()->get('doctrine')->getDefaultEntityManagerName();
       
    50 
       
    51         /* @var $entityManager Doctrine\ORM\EntityManager */
       
    52         $entityManager = $this->getEntityManager($input->getOption('em'));
       
    53 
       
    54         $entityClassNames = $entityManager->getConfiguration()
       
    55                                           ->getMetadataDriverImpl()
       
    56                                           ->getAllClassNames();
       
    57 
       
    58         if (!$entityClassNames) {
       
    59             throw new \LogicException(
       
    60                 'You do not have any mapped Doctrine ORM entities for any of your bundles. '.
       
    61                 'Create a class inside the Entity namespace of any of your bundles and provide '.
       
    62                 'mapping information for it with Annotations directly in the classes doc blocks '.
       
    63                 'or with XML/YAML in your bundles Resources/config/doctrine/ directory.'
       
    64             );
       
    65         }
       
    66 
       
    67         $output->writeln(sprintf("Found <info>%d</info> entities mapped in entity manager <info>%s</info>:", count($entityClassNames), $entityManagerName));
       
    68 
       
    69         foreach ($entityClassNames as $entityClassName) {
       
    70             try {
       
    71                 $cm = $entityManager->getClassMetadata($entityClassName);
       
    72                 $output->writeln(sprintf("<info>[OK]</info>   %s", $entityClassName));
       
    73             } catch (MappingException $e) {
       
    74                 $output->writeln("<error>[FAIL]</error> ".$entityClassName);
       
    75                 $output->writeln(sprintf("<comment>%s</comment>", $e->getMessage()));
       
    76                 $output->writeln('');
       
    77             }
       
    78         }
       
    79     }
       
    80 }