GenerateEntityDoctrineCommand.php 5.4 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. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  16. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  17. /**
  18. * Initialize a new Doctrine entity inside a bundle.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jonathan H. Wage <jonwage@gmail.com>
  22. */
  23. class GenerateEntityDoctrineCommand extends DoctrineCommand
  24. {
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('doctrine:generate:entity')
  29. ->setDescription('Generate a new Doctrine entity inside a bundle')
  30. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the entity in')
  31. ->addArgument('entity', InputArgument::REQUIRED, 'The entity class to initialize')
  32. ->addOption('mapping-type', null, InputOption::VALUE_OPTIONAL, 'The mapping type to to use for the entity', 'xml')
  33. ->addOption('fields', null, InputOption::VALUE_OPTIONAL, 'The fields to create with the new entity')
  34. ->setHelp(<<<EOT
  35. The <info>doctrine:generate:entity</info> task initializes a new Doctrine
  36. entity inside a bundle:
  37. <info>./app/console doctrine:generate:entity "MyCustomBundle" "User\Group"</info>
  38. The above would initialize a new entity in the following entity namespace
  39. <info>Bundle\MyCustomBundle\Entity\User\Group</info>.
  40. You can also optionally specify the fields you want to generate in the new
  41. entity:
  42. <info>./app/console doctrine:generate:entity "MyCustomBundle" "User\Group" --fields="name:string(255) description:text"</info>
  43. EOT
  44. );
  45. }
  46. /**
  47. * @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle")
  48. */
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
  52. $entity = $input->getArgument('entity');
  53. $fullEntityClassName = $bundle->getNamespace().'\\Entity\\'.$entity;
  54. $mappingType = $input->getOption('mapping-type');
  55. $class = new ClassMetadataInfo($fullEntityClassName);
  56. $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  57. $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  58. // Map the specified fields
  59. $fields = $input->getOption('fields');
  60. if ($fields) {
  61. $e = explode(' ', $fields);
  62. foreach ($e as $value) {
  63. $e = explode(':', $value);
  64. $name = $e[0];
  65. if (strlen($name)) {
  66. $type = isset($e[1]) ? $e[1] : 'string';
  67. preg_match_all('/(.*)\((.*)\)/', $type, $matches);
  68. $type = isset($matches[1][0]) ? $matches[1][0] : $type;
  69. $length = isset($matches[2][0]) ? $matches[2][0] : null;
  70. $class->mapField(array(
  71. 'fieldName' => $name,
  72. 'type' => $type,
  73. 'length' => $length
  74. ));
  75. }
  76. }
  77. }
  78. // Setup a new exporter for the mapping type specified
  79. $cme = new ClassMetadataExporter();
  80. $exporter = $cme->getExporter($mappingType);
  81. $entityPath = $bundle->getPath().'/Entity/'.$entity.'.php';
  82. if (file_exists($entityPath)) {
  83. throw new \RuntimeException(sprintf("Entity %s already exists.", $class->name));
  84. }
  85. if ('annotation' === $mappingType) {
  86. $exporter->setEntityGenerator($this->getEntityGenerator());
  87. $entityCode = $exporter->exportClassMetadata($class);
  88. $mappingPath = $mappingCode = false;
  89. } else {
  90. $mappingType = 'yaml' == $mappingType ? 'yml' : $mappingType;
  91. $mappingPath = $bundle->getPath().'/Resources/config/doctrine/'.str_replace('\\', '.', $fullEntityClassName).'.orm.'.$mappingType;
  92. $mappingCode = $exporter->exportClassMetadata($class);
  93. $entityGenerator = $this->getEntityGenerator();
  94. $entityCode = $entityGenerator->generateEntityClass($class);
  95. if (file_exists($mappingPath)) {
  96. throw new \RuntimeException(sprintf("Cannot generate entity when mapping <info>%s</info> already exists", $mappingPath));
  97. }
  98. }
  99. $output->writeln(sprintf('Generating entity for "<info>%s</info>"', $bundle->getName()));
  100. $output->writeln(sprintf(' > entity <comment>%s</comment> into <info>%s</info>', $fullEntityClassName, $entityPath));
  101. if (!is_dir($dir = dirname($entityPath))) {
  102. mkdir($dir, 0777, true);
  103. }
  104. file_put_contents($entityPath, $entityCode);
  105. if ($mappingPath) {
  106. $output->writeln(sprintf(' > mapping into <info>%s</info>', $mappingPath));
  107. if (!is_dir($dir = dirname($mappingPath))) {
  108. mkdir($dir, 0777, true);
  109. }
  110. file_put_contents($mappingPath, $mappingCode);
  111. }
  112. }
  113. }