GenerateEntitiesDoctrineCommand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Doctrine\ORM\Tools\EntityRepositoryGenerator;
  16. use Symfony\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
  17. /**
  18. * Generate entity classes from mapping information
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. */
  23. class GenerateEntitiesDoctrineCommand extends DoctrineCommand
  24. {
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('doctrine:generate:entities')
  29. ->setAliases(array('generate:doctrine:entities'))
  30. ->setDescription('Generate entity classes and method stubs from your mapping information')
  31. ->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name')
  32. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where to generate entities when it cannot be guessed')
  33. ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.')
  34. ->setHelp(<<<EOT
  35. The <info>doctrine:generate:entities</info> command generates entity classes
  36. and method stubs from your mapping information:
  37. You have to limit generation of entities:
  38. * To a bundle:
  39. <info>./app/console doctrine:generate:entities MyCustomBundle</info>
  40. * To a single entity:
  41. <info>./app/console doctrine:generate:entities MyCustomBundle:User</info>
  42. <info>./app/console doctrine:generate:entities MyCustomBundle/Entity/User</info>
  43. * To a namespace
  44. <info>./app/console doctrine:generate:entities MyCustomBundle/Entity</info>
  45. If the entities are not stored in a bundle, and if the classes do not exist,
  46. the command has no way to guess where they should be generated. In this case,
  47. you must provide the <comment>--path</comment> option:
  48. <info>./app/console doctrine:generate:entities Blog/Entity --path=src/</info>
  49. You should provide the <comment>--no-backup</comment> option if you don't mind to back up files
  50. before to generate entities:
  51. <info>./app/console doctrine:generate:entities Blog/Entity --no-backup</info>
  52. EOT
  53. );
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. $manager = new DisconnectedMetadataFactory($this->container->get('doctrine'));
  58. try {
  59. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
  60. $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
  61. $metadata = $manager->getBundleMetadata($bundle);
  62. } catch (\InvalidArgumentException $e) {
  63. $name = strtr($input->getArgument('name'), '/', '\\');
  64. if (false !== $pos = strpos($name, ':')) {
  65. $name = $this->container->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1);
  66. }
  67. if (class_exists($name)) {
  68. $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
  69. $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
  70. } else {
  71. $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
  72. $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
  73. }
  74. }
  75. $generator = $this->getEntityGenerator();
  76. $generator->setBackupExisting(!$input->getOption('no-backup'));
  77. $repoGenerator = new EntityRepositoryGenerator();
  78. foreach ($metadata->getMetadata() as $m) {
  79. $output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name));
  80. $generator->generate(array($m), $metadata->getPath());
  81. if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
  82. $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
  83. }
  84. }
  85. }
  86. }