GenerateDoctrineEntityCommand.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 Sensio\Bundle\GeneratorBundle\Command;
  11. use Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator;
  12. use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  17. use Doctrine\DBAL\Types\Type;
  18. /**
  19. * Initializes a Doctrine entity inside a bundle.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class GenerateDoctrineEntityCommand extends GenerateDoctrineCommand
  24. {
  25. private $generator;
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('doctrine:generate:entity')
  30. ->setAliases(array('generate:doctrine:entity'))
  31. ->setDescription('Generates a new Doctrine entity inside a bundle')
  32. ->addOption('entity', null, InputOption::VALUE_REQUIRED, 'The entity class name to initialize (shortcut notation)')
  33. ->addOption('fields', null, InputOption::VALUE_REQUIRED, 'The fields to create with the new entity')
  34. ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Use the format for configuration files (php, xml, yml, or annotation)', 'annotation')
  35. ->addOption('with-repository', null, InputOption::VALUE_NONE, 'Whether to generate the entity repository or not')
  36. ->setHelp(<<<EOT
  37. The <info>doctrine:generate:entity</info> task generates a new Doctrine
  38. entity inside a bundle:
  39. <info>php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post</info>
  40. The above command would initialize a new entity in the following entity
  41. namespace <info>Acme\BlogBundle\Entity\Blog\Post</info>.
  42. You can also optionally specify the fields you want to generate in the new
  43. entity:
  44. <info>php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post --fields="title:string(255) body:text"</info>
  45. The command can also generate the corresponding entity repository class with the
  46. <comment>--with-repository</comment> option:
  47. <info>php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post --with-repository</info>
  48. By default, the command uses annotations for the mapping information; change it
  49. with <comment>--format</comment>:
  50. <info>php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post --format=yml</info>
  51. To deactivate the interaction mode, simply use the `--no-interaction` option
  52. whitout forgetting to pass all needed options:
  53. <info>php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post --format=annotation --fields="title:string(255) body:text" --with-repository --no-interaction</info>
  54. EOT
  55. );
  56. }
  57. /**
  58. * @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle/MySampleBundle")
  59. */
  60. protected function execute(InputInterface $input, OutputInterface $output)
  61. {
  62. $dialog = $this->getDialogHelper();
  63. if ($input->isInteractive()) {
  64. if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
  65. $output->writeln('<error>Command aborted</error>');
  66. return 1;
  67. }
  68. }
  69. $entity = Validators::validateEntityName($input->getOption('entity'));
  70. list($bundle, $entity) = $this->parseShortcutNotation($entity);
  71. $format = Validators::validateFormat($input->getOption('format'));
  72. $fields = $this->parseFields($input->getOption('fields'));
  73. $dialog->writeSection($output, 'Entity generation');
  74. $bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
  75. $generator = $this->getGenerator();
  76. $generator->generate($bundle, $entity, $format, array_values($fields), $input->getOption('with-repository'));
  77. $output->writeln('Generating the entity code: <info>OK</info>');
  78. $dialog->writeGeneratorSummary($output, array());
  79. }
  80. protected function interact(InputInterface $input, OutputInterface $output)
  81. {
  82. $dialog = $this->getDialogHelper();
  83. $dialog->writeSection($output, 'Welcome to the Doctrine2 entity generator');
  84. // namespace
  85. $output->writeln(array(
  86. '',
  87. 'This command helps you generate Doctrine2 entities.',
  88. '',
  89. 'First, you need to give the entity name you want to generate.',
  90. 'You must use the shortcut notation like <comment>AcmeBlogBundle:Post</comment>.',
  91. ''
  92. ));
  93. while (true) {
  94. $entity = $dialog->askAndValidate($output, $dialog->getQuestion('The Entity shortcut name', $input->getOption('entity')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), false, $input->getOption('entity'));
  95. list($bundle, $entity) = $this->parseShortcutNotation($entity);
  96. // check reserved words
  97. if ($this->getGenerator()->isReservedKeyword($entity)){
  98. $output->writeln(sprintf('<bg=red> "%s" is a reserved word</>.', $entity));
  99. continue;
  100. }
  101. try {
  102. $b = $this->getContainer()->get('kernel')->getBundle($bundle);
  103. if (!file_exists($b->getPath().'/Entity/'.str_replace('\\', '/', $entity).'.php')) {
  104. break;
  105. }
  106. $output->writeln(sprintf('<bg=red>Entity "%s:%s" already exists</>.', $bundle, $entity));
  107. } catch (\Exception $e) {
  108. $output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle));
  109. }
  110. }
  111. $input->setOption('entity', $bundle.':'.$entity);
  112. // format
  113. $output->writeln(array(
  114. '',
  115. 'Determine the format to use for the mapping information.',
  116. '',
  117. ));
  118. $format = $dialog->askAndValidate($output, $dialog->getQuestion('Configuration format (yml, xml, php, or annotation)', $input->getOption('format')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateFormat'), false, $input->getOption('format'));
  119. $input->setOption('format', $format);
  120. // fields
  121. $input->setOption('fields', $this->addFields($input, $output, $dialog));
  122. // repository?
  123. $output->writeln('');
  124. $withRepository = $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate an empty repository class', $input->getOption('with-repository') ? 'yes' : 'no', '?'), $input->getOption('with-repository'));
  125. $input->setOption('with-repository', $withRepository);
  126. // summary
  127. $output->writeln(array(
  128. '',
  129. $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true),
  130. '',
  131. sprintf("You are going to generate a \"<info>%s:%s</info>\" Doctrine2 entity", $bundle, $entity),
  132. sprintf("using the \"<info>%s</info>\" format.", $format),
  133. '',
  134. ));
  135. }
  136. private function parseFields($input)
  137. {
  138. if (is_array($input)) {
  139. return $input;
  140. }
  141. $fields = array();
  142. foreach (explode(' ', $input) as $value) {
  143. $elements = explode(':', $value);
  144. $name = $elements[0];
  145. if (strlen($name)) {
  146. $type = isset($elements[1]) ? $elements[1] : 'string';
  147. preg_match_all('/(.*)\((.*)\)/', $type, $matches);
  148. $type = isset($matches[1][0]) ? $matches[1][0] : $type;
  149. $length = isset($matches[2][0]) ? $matches[2][0] : null;
  150. $fields[$name] = array('fieldName' => $name, 'type' => $type, 'length' => $length);
  151. }
  152. }
  153. return $fields;
  154. }
  155. private function addFields(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
  156. {
  157. $fields = $this->parseFields($input->getOption('fields'));
  158. $output->writeln(array(
  159. '',
  160. 'Instead of starting with a blank entity, you can add some fields now.',
  161. 'Note that the primary key will be added automatically (named <comment>id</comment>).',
  162. '',
  163. ));
  164. $output->write('<info>Available types:</info> ');
  165. $types = array_keys(Type::getTypesMap());
  166. $count = 20;
  167. foreach ($types as $i => $type) {
  168. if ($count > 50) {
  169. $count = 0;
  170. $output->writeln('');
  171. }
  172. $count += strlen($type);
  173. $output->write(sprintf('<comment>%s</comment>', $type));
  174. if (count($types) != $i + 1) {
  175. $output->write(', ');
  176. } else {
  177. $output->write('.');
  178. }
  179. }
  180. $output->writeln('');
  181. $fieldValidator = function ($type) use ($types) {
  182. // FIXME: take into account user-defined field types
  183. if (!in_array($type, $types)) {
  184. throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type));
  185. }
  186. return $type;
  187. };
  188. $lengthValidator = function ($length) {
  189. if (!$length) {
  190. return $length;
  191. }
  192. $result = filter_var($length, FILTER_VALIDATE_INT, array(
  193. 'options' => array('min_range' => 1)
  194. ));
  195. if (false === $result) {
  196. throw new \InvalidArgumentException(sprintf('Invalid length "%s".', $length));
  197. }
  198. return $length;
  199. };
  200. while (true) {
  201. $output->writeln('');
  202. $self = $this;
  203. $name = $dialog->askAndValidate($output, $dialog->getQuestion('New field name (press <return> to stop adding fields)', null), function ($name) use ($fields, $self) {
  204. if (isset($fields[$name]) || 'id' == $name) {
  205. throw new \InvalidArgumentException(sprintf('Field "%s" is already defined.', $name));
  206. }
  207. // check reserved words
  208. if ($self->getGenerator()->isReservedKeyword($name)){
  209. throw new \InvalidArgumentException(sprintf('Name "%s" is a reserved word.', $name));
  210. }
  211. return $name;
  212. });
  213. if (!$name) {
  214. break;
  215. }
  216. $defaultType = 'string';
  217. if (substr($name, -3) == '_at') {
  218. $defaultType = 'datetime';
  219. } else if (substr($name, -3) == '_id') {
  220. $defaultType = 'integer';
  221. }
  222. $type = $dialog->askAndValidate($output, $dialog->getQuestion('Field type', $defaultType), $fieldValidator, false, $defaultType);
  223. $data = array('fieldName' => $name, 'type' => $type);
  224. if ($type == 'string') {
  225. $data['length'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field length', 255), $lengthValidator, false, 255);
  226. }
  227. $fields[$name] = $data;
  228. }
  229. return $fields;
  230. }
  231. public function getGenerator()
  232. {
  233. if (null === $this->generator) {
  234. $this->generator = new DoctrineEntityGenerator($this->getContainer()->get('filesystem'), $this->getContainer()->get('doctrine'));
  235. }
  236. return $this->generator;
  237. }
  238. public function setGenerator(DoctrineEntityGenerator $generator)
  239. {
  240. $this->generator = $generator;
  241. }
  242. protected function getDialogHelper()
  243. {
  244. $dialog = $this->getHelperSet()->get('dialog');
  245. if (!$dialog || get_class($dialog) !== 'Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper') {
  246. $this->getHelperSet()->set($dialog = new DialogHelper());
  247. }
  248. return $dialog;
  249. }
  250. }