GenerateEntityDoctrineCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  9. use Doctrine\ORM\Tools\EntityGenerator;
  10. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  11. /*
  12. * This file is part of the Symfony framework.
  13. *
  14. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  15. *
  16. * This source file is subject to the MIT license that is bundled
  17. * with this source code in the file LICENSE.
  18. */
  19. /**
  20. * Initialize a new Doctrine entity inside a bundle.
  21. *
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. * @author Jonathan H. Wage <jonwage@gmail.com>
  24. */
  25. class GenerateEntityDoctrineCommand extends DoctrineCommand
  26. {
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('doctrine:generate:entity')
  31. ->setDescription('Generate a new Doctrine entity inside a bundle.')
  32. ->addArgument('bundle', null, InputArgument::REQUIRED, 'The bundle to initialize the entity in.')
  33. ->addArgument('entity', null, InputArgument::REQUIRED, 'The entity class to initialize.')
  34. ->addOption('mapping-type', null, InputOption::PARAMETER_OPTIONAL, 'The mapping type to to use for the entity.')
  35. ->addOption('fields', null, InputOption::PARAMETER_OPTIONAL, 'The fields to create with the new entity.')
  36. ->setHelp(<<<EOT
  37. The <info>doctrine:generate:entity</info> task initializes a new Doctrine entity inside a bundle:
  38. <info>./symfony doctrine:generate:entity "Bundle\MyCustomBundle" "User\Group"</info>
  39. The above would initialize a new entity in the following entity namespace <info>Bundle\MyCustomBundle\Entity\User\Group</info>.
  40. You can also optionally specify the fields you want to generate in the new entity:
  41. <info>./symfony doctrine:generate:entity "Bundle\MyCustomBundle" "User\Group" --fields="name:string(255) description:text"</info>
  42. EOT
  43. );
  44. }
  45. /**
  46. * @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle")
  47. */
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. if (!preg_match('/Bundle$/', $bundle = $input->getArgument('bundle'))) {
  51. throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\MySampleBundle".');
  52. }
  53. $dirs = $this->container->getKernelService()->getBundleDirs();
  54. $tmp = str_replace('\\', '/', $bundle);
  55. $namespace = str_replace('/', '\\', dirname($tmp));
  56. $bundle = basename($tmp);
  57. if (!isset($dirs[$namespace])) {
  58. throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace));
  59. }
  60. $entity = $input->getArgument('entity');
  61. $entityNamespace = $namespace.'\\'.$bundle.'\\Entity';
  62. $fullEntityClassName = $entityNamespace.'\\'.$entity;
  63. $tmp = str_replace('\\', '/', $fullEntityClassName);
  64. $tmp = str_replace('/', '\\', dirname($tmp));
  65. $className = basename($tmp);
  66. $mappingType = $input->getOption('mapping-type');
  67. $mappingType = $mappingType ? $mappingType : 'xml';
  68. $class = new ClassMetadataInfo($fullEntityClassName);
  69. $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  70. $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  71. // Map the specified fields
  72. $fields = $input->getOption('fields');
  73. if ($fields)
  74. {
  75. $e = explode(' ', $fields);
  76. foreach ($e as $value) {
  77. $e = explode(':', $value);
  78. $name = $e[0];
  79. $type = isset($e[1]) ? $e[1] : 'string';
  80. preg_match_all('/(.*)\((.*)\)/', $type, $matches);
  81. $type = isset($matches[1][0]) ? $matches[1][0] : 'string';
  82. $length = isset($matches[2][0]) ? $matches[2][0] : null;
  83. $class->mapField(array(
  84. 'fieldName' => $name,
  85. 'type' => $type,
  86. 'length' => $length
  87. ));
  88. }
  89. }
  90. // Setup a new exporter for the mapping type specified
  91. $cme = new ClassMetadataExporter();
  92. $exporter = $cme->getExporter($mappingType);
  93. if ($mappingType === 'annotation') {
  94. $path = $dirs[$namespace].'/'.$bundle.'/Entity/'.str_replace($entityNamespace.'\\', null, $fullEntityClassName).'.php';
  95. $exporter->setEntityGenerator($this->getEntityGenerator());
  96. } else {
  97. $mappingType = $mappingType == 'yaml' ? 'yml' : $mappingType;
  98. $path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata/orm/'.str_replace('\\', '.', $fullEntityClassName).'.dcm.'.$mappingType;
  99. }
  100. $code = $exporter->exportClassMetadata($class);
  101. if (!is_dir($dir = dirname($path))) {
  102. mkdir($dir, 0777, true);
  103. }
  104. $output->writeln(sprintf('Generating entity for "<info>%s</info>"', $bundle));
  105. $output->writeln(sprintf(' > generating <comment>%s</comment>', $fullEntityClassName));
  106. file_put_contents($path, $code);
  107. }
  108. }