GenerateEntitiesDoctrineCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  16. * Generate entity classes from mapping information
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Jonathan H. Wage <jonwage@gmail.com>
  20. */
  21. class GenerateEntitiesDoctrineCommand extends DoctrineCommand
  22. {
  23. protected function configure()
  24. {
  25. $this
  26. ->setName('doctrine:generate:entities')
  27. ->setDescription('Generate entity classes and method stubs from your mapping information')
  28. ->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name')
  29. ->setHelp(<<<EOT
  30. The <info>doctrine:generate:entities</info> command generates entity classes
  31. and method stubs from your mapping information:
  32. You have to limit generation of entities:
  33. * To a bundle:
  34. <info>./app/console doctrine:generate:entities MyCustomBundle</info>
  35. * To a single entity:
  36. <info>./app/console doctrine:generate:entities MyCustomBundle:User</info>
  37. <info>./app/console doctrine:generate:entities MyCustomBundle/Entity/User</info>
  38. * To a namespace
  39. <info>./app/console doctrine:generate:entities MyCustomBundle/Entity</info>
  40. EOT
  41. );
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. try {
  46. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
  47. $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
  48. list($metadatas, $path) = $this->getBundleInfo($bundle);
  49. } catch (\InvalidArgumentException $e) {
  50. $name = strtr($input->getArgument('name'), '/', '\\');
  51. if (false !== strpos($name, ':')) {
  52. $name = $this->getAliasedClassName($name);
  53. }
  54. if (class_exists($name)) {
  55. $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
  56. list($metadatas, $path) = $this->getClassInfo($name);
  57. } else {
  58. $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
  59. list($metadatas, $path) = $this->getNamespaceInfo($name);
  60. }
  61. }
  62. $generator = $this->getEntityGenerator();
  63. foreach ($metadatas as $metadata) {
  64. $output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
  65. $generator->generate(array($metadata), $path);
  66. if ($metadata->customRepositoryClassName) {
  67. if (false === strpos($metadata->customRepositoryClassName, $namespace)) {
  68. continue;
  69. }
  70. $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $path);
  71. }
  72. }
  73. }
  74. private function getBundleInfo($bundle)
  75. {
  76. $namespace = $bundle->getNamespace();
  77. if (!$metadatas = $this->findMetadatasByNamespace($namespace)) {
  78. throw new \RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundle->getName()));
  79. }
  80. $path = $this->findBasePathForClass($bundle->getName(), $bundle->getNamespace(), $bundle->getPath());
  81. return array($metadatas, $path);
  82. }
  83. private function getClassInfo($class)
  84. {
  85. if (!$metadatas = $this->findMetadatasByClass($class)) {
  86. throw new \RuntimeException(sprintf('Entity "%s" is not a mapped entity.', $class));
  87. }
  88. $r = $metadatas[$class]->getReflectionClass();
  89. if (!$r) {
  90. throw new \RuntimeException('Unable to determine where to save the "%s" class.', $class);
  91. }
  92. $path = $this->findBasePathForClass($class, $r->getNamespacename(), dirname($r->getFilename()));
  93. return array($metadatas, $path);
  94. }
  95. private function getNamespaceInfo($namespace)
  96. {
  97. if (!$metadatas = $this->findMetadatasByNamespace($namespace)) {
  98. throw new \RuntimeException(sprintf('Namespace "%s" does not contain any mapped entities.', $namespace));
  99. }
  100. $first = reset($metadatas);
  101. $r = $first->getReflectionClass();
  102. if (!$r) {
  103. throw new \RuntimeException('Unable to determine where to save the "%s" class.', $class);
  104. }
  105. $path = $this->findBasePathForClass($namespace, $r->getNamespacename(), dirname($r->getFilename()));
  106. return array($metadatas, $path);
  107. }
  108. }