GenerateEntitiesDoctrineCommand.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Symfony\Bundle\DoctrineBundle\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Output\Output;
  8. use Symfony\Component\HttpKernel\Bundle\Bundle;
  9. use Doctrine\ORM\Tools\EntityGenerator;
  10. /*
  11. * This file is part of the Symfony framework.
  12. *
  13. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  14. *
  15. * This source file is subject to the MIT license that is bundled
  16. * with this source code in the file LICENSE.
  17. */
  18. /**
  19. * Generate entity classes from mapping information
  20. *
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. * @author Jonathan H. Wage <jonwage@gmail.com>
  23. */
  24. class GenerateEntitiesDoctrineCommand extends DoctrineCommand
  25. {
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('doctrine:generate:entities')
  30. ->setDescription('Generate entity classes and method stubs from your mapping information.')
  31. ->addOption('bundle', null, InputOption::PARAMETER_OPTIONAL, 'The bundle to initialize the entity or entities in.')
  32. ->addOption('entity', null, InputOption::PARAMETER_OPTIONAL, 'The entity class to initialize (requires bundle parameter).')
  33. ->setHelp(<<<EOT
  34. The <info>doctrine:generate:entities</info> command generates entity classes and method stubs from your mapping information:
  35. <info>./symfony doctrine:generate:entities</info>
  36. The above would generate entity classes for all bundles.
  37. You can also optionally limit generation to entities within an individual bundle:
  38. <info>./symfony doctrine:generate:entities --bundle="Bundle/MyCustomBundle"</info>
  39. Alternatively, you can limit generation to a single entity within a bundle:
  40. <info>./symfony doctrine:generate:entities --bundle="Bundle/MyCustomBundle" --entity="User"</info>
  41. EOT
  42. );
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $filterBundle = $input->getOption('bundle') ? str_replace('/', '\\', $input->getOption('bundle')) : false;
  47. $filterEntity = $filterBundle ? $filterBundle . '\\Entity\\' . str_replace('/', '\\', $input->getOption('entity')) : false;
  48. if (!isset($filterBundle) && isset($filterEntity)) {
  49. throw new \InvalidArgumentException(sprintf('Unable to specify an entity without also specifying a bundle.'));
  50. }
  51. $entityGenerator = $this->getEntityGenerator();
  52. foreach ($this->container->get('kernel')->getBundles() as $bundle) {
  53. // retrieve the full bundle classname
  54. $class = $bundle->getReflection()->getName();
  55. if ($filterBundle && $filterBundle != $class) {
  56. continue;
  57. }
  58. // transform classname to a path and substract it to get the destination
  59. $path = dirname(str_replace('\\', '/', $class));
  60. $destination = str_replace('/'.$path, "", $bundle->getPath());
  61. if ($metadatas = $this->getBundleMetadatas($bundle)) {
  62. $output->writeln(sprintf('Generating entities for "<info>%s</info>"', $class));
  63. foreach ($metadatas as $metadata) {
  64. if ($filterEntity && strpos($metadata->name, $filterEntity) !== 0) {
  65. continue;
  66. }
  67. $output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
  68. $entityGenerator->generate(array($metadata), $destination);
  69. }
  70. }
  71. }
  72. }
  73. }