GenerateEntitiesDoctrineCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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>php app/console doctrine:generate:entities MyCustomBundle</info>
  40. * To a single entity:
  41. <info>php app/console doctrine:generate:entities MyCustomBundle:User</info>
  42. <info>php app/console doctrine:generate:entities MyCustomBundle/Entity/User</info>
  43. * To a namespace
  44. <info>php 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>php 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>php app/console doctrine:generate:entities Blog/Entity --no-backup</info>
  52. <error>Important:</error> Even if you specified Inheritance options in your
  53. XML or YAML Mapping files the generator cannot generate the base and
  54. child classes for you correctly, because it doesn't know which
  55. class is supposed to extend which. You have to adjust the entity
  56. code manually for inheritance to work!
  57. EOT
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output)
  61. {
  62. $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
  63. try {
  64. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
  65. $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
  66. $metadata = $manager->getBundleMetadata($bundle);
  67. } catch (\InvalidArgumentException $e) {
  68. $name = strtr($input->getArgument('name'), '/', '\\');
  69. if (false !== $pos = strpos($name, ':')) {
  70. $name = $this->getContainer()->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1);
  71. }
  72. if (class_exists($name)) {
  73. $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
  74. $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
  75. } else {
  76. $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
  77. $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
  78. }
  79. }
  80. $generator = $this->getEntityGenerator();
  81. $generator->setBackupExisting(!$input->getOption('no-backup'));
  82. $repoGenerator = new EntityRepositoryGenerator();
  83. foreach ($metadata->getMetadata() as $m) {
  84. $output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name));
  85. $generator->generate(array($m), $metadata->getPath());
  86. if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
  87. $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
  88. }
  89. }
  90. }
  91. }