GenerateEntitiesDoctrineCommand.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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('Generates 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. By default, the unmodified version of each entity is backed up and saved
  50. (e.g. Product.php~). To prevent this task from creating the backup file,
  51. pass the <comment>--no-backup</comment> option:
  52. <info>php app/console doctrine:generate:entities Blog/Entity --no-backup</info>
  53. <error>Important:</error> Even if you specified Inheritance options in your
  54. XML or YAML Mapping files the generator cannot generate the base and
  55. child classes for you correctly, because it doesn't know which
  56. class is supposed to extend which. You have to adjust the entity
  57. code manually for inheritance to work!
  58. EOT
  59. );
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
  64. try {
  65. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
  66. $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
  67. $metadata = $manager->getBundleMetadata($bundle);
  68. } catch (\InvalidArgumentException $e) {
  69. $name = strtr($input->getArgument('name'), '/', '\\');
  70. if (false !== $pos = strpos($name, ':')) {
  71. $name = $this->getContainer()->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1);
  72. }
  73. if (class_exists($name)) {
  74. $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
  75. $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
  76. } else {
  77. $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
  78. $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
  79. }
  80. }
  81. $generator = $this->getEntityGenerator();
  82. $backupExisting = !$input->getOption('no-backup');
  83. $generator->setBackupExisting($backupExisting);
  84. $repoGenerator = new EntityRepositoryGenerator();
  85. foreach ($metadata->getMetadata() as $m) {
  86. if ($backupExisting) {
  87. $basename = substr($m->name, strrpos($m->name, '\\') + 1);
  88. $output->writeln(sprintf(' > backing up <comment>%s.php</comment> to <comment>%s.php~</comment>', $basename, $basename));
  89. }
  90. // Getting the metadata for the entity class once more to get the correct path if the namespace has multiple occurrences
  91. try {
  92. $entityMetadata = $manager->getClassMetadata($m->getName(), $input->getOption('path'));
  93. } catch (\RuntimeException $e) {
  94. // fall back to the bundle metadata when no entity class could be found
  95. $entityMetadata = $metadata;
  96. }
  97. $output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name));
  98. $generator->generate(array($m), $entityMetadata->getPath());
  99. if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
  100. $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
  101. }
  102. }
  103. }
  104. }