GenerateEntitiesDoctrineCommand.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Framework\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. $bundleDirs = $this->container->getKernelService()->getBundleDirs();
  53. foreach ($this->container->getKernelService()->getBundles() as $bundle) {
  54. $tmp = dirname(str_replace('\\', '/', get_class($bundle)));
  55. $namespace = str_replace('/', '\\', dirname($tmp));
  56. $class = basename($tmp);
  57. if ($filterBundle && $filterBundle != $namespace . '\\' . $class) {
  58. continue;
  59. }
  60. if (isset($bundleDirs[$namespace])) {
  61. $destination = realpath($bundleDirs[$namespace].'/..');
  62. if ($metadatas = $this->getBundleMetadatas($bundle)) {
  63. $output->writeln(sprintf('Generating entities for "<info>%s</info>"', $class));
  64. foreach ($metadatas as $metadata) {
  65. if ($filterEntity && strpos($metadata->name, $filterEntity) !== 0) {
  66. continue;
  67. }
  68. $output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
  69. $entityGenerator->generate(array($metadata), $destination);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }