GenerateDoctrineEntityCommand.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 --field="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. $keywordList = $this->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform()->getReservedKeywordsList();
  98. if($keywordList->isKeyword($entity)){
  99. $output->writeln(sprintf('<bg=red> "%s" is a reserved word</>.', $entity));
  100. continue;
  101. }
  102. try {
  103. $b = $this->getContainer()->get('kernel')->getBundle($bundle);
  104. if (!file_exists($b->getPath().'/Entity/'.str_replace('\\', '/', $entity).'.php')) {
  105. break;
  106. }
  107. $output->writeln(sprintf('<bg=red>Entity "%s:%s" already exists</>.', $bundle, $entity));
  108. } catch (\Exception $e) {
  109. $output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle));
  110. }
  111. }
  112. $input->setOption('entity', $bundle.':'.$entity);
  113. // format
  114. $output->writeln(array(
  115. '',
  116. 'Determine the format to use for the mapping information.',
  117. '',
  118. ));
  119. $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'));
  120. $input->setOption('format', $format);
  121. // fields
  122. $input->setOption('fields', $this->addFields($input, $output, $dialog));
  123. // repository?
  124. $output->writeln('');
  125. $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'));
  126. $input->setOption('with-repository', $withRepository);
  127. // summary
  128. $output->writeln(array(
  129. '',
  130. $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true),
  131. '',
  132. sprintf("You are going to generate a \"<info>%s:%s</info>\" Doctrine2 entity", $bundle, $entity),
  133. sprintf("using the \"<info>%s</info>\" format.", $format),
  134. '',
  135. ));
  136. }
  137. private function parseFields($input)
  138. {
  139. if (is_array($input)) {
  140. return $input;
  141. }
  142. $fields = array();
  143. foreach (explode(' ', $input) as $value) {
  144. $elements = explode(':', $value);
  145. $name = $elements[0];
  146. if (strlen($name)) {
  147. $type = isset($elements[1]) ? $elements[1] : 'string';
  148. preg_match_all('/(.*)\((.*)\)/', $type, $matches);
  149. $type = isset($matches[1][0]) ? $matches[1][0] : $type;
  150. $length = isset($matches[2][0]) ? $matches[2][0] : null;
  151. $fields[$name] = array('fieldName' => $name, 'type' => $type, 'length' => $length);
  152. }
  153. }
  154. return $fields;
  155. }
  156. private function addFields(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
  157. {
  158. $fields = $this->parseFields($input->getOption('fields'));
  159. $output->writeln(array(
  160. '',
  161. 'Instead of starting with a blank entity, you can add some fields now.',
  162. 'Note that the primary key will be added automatically (named <comment>id</comment>).',
  163. '',
  164. ));
  165. $output->write('<info>Available types:</info> ');
  166. $types = array_keys(Type::getTypesMap());
  167. $count = 20;
  168. foreach ($types as $i => $type) {
  169. if ($count > 50) {
  170. $count = 0;
  171. $output->writeln('');
  172. }
  173. $count += strlen($type);
  174. $output->write(sprintf('<comment>%s</comment>', $type));
  175. if (count($types) != $i + 1) {
  176. $output->write(', ');
  177. } else {
  178. $output->write('.');
  179. }
  180. }
  181. $output->writeln('');
  182. $fieldValidator = function ($type) use ($types) {
  183. // FIXME: take into account user-defined field types
  184. if (!in_array($type, $types)) {
  185. throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type));
  186. }
  187. return $type;
  188. };
  189. $lengthValidator = function ($length) {
  190. if (!$length) {
  191. return $length;
  192. }
  193. $result = filter_var($length, FILTER_VALIDATE_INT, array(
  194. 'options' => array('min_range' => 1)
  195. ));
  196. if (false === $result) {
  197. throw new \InvalidArgumentException(sprintf('Invalid length "%s".', $length));
  198. }
  199. return $length;
  200. };
  201. while (true) {
  202. $output->writeln('');
  203. $keywordList = $this->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform()->getReservedKeywordsList();
  204. $name = $dialog->askAndValidate($output, $dialog->getQuestion('New field name (press <return> to stop adding fields)', null), function ($name) use ($fields,$keywordList) {
  205. if (isset($fields[$name]) || 'id' == $name) {
  206. throw new \InvalidArgumentException(sprintf('Field "%s" is already defined.', $name));
  207. }
  208. // check reserved words
  209. if($keywordList->isKeyword($name)){
  210. throw new \InvalidArgumentException(sprintf('Name "%s" is a reserved word.', $name));
  211. }
  212. return $name;
  213. });
  214. if (!$name) {
  215. break;
  216. }
  217. $defaultType = 'string';
  218. if (substr($name, -3) == '_at') {
  219. $defaultType = 'datetime';
  220. } else if (substr($name, -3) == '_id') {
  221. $defaultType = 'integer';
  222. }
  223. $type = $dialog->askAndValidate($output, $dialog->getQuestion('Field type', $defaultType), $fieldValidator, false, $defaultType);
  224. $data = array('fieldName' => $name, 'type' => $type);
  225. if ($type == 'string') {
  226. $data['length'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field length', 255), $lengthValidator, false, 255);
  227. }
  228. $fields[$name] = $data;
  229. }
  230. return $fields;
  231. }
  232. protected function getGenerator()
  233. {
  234. if (null === $this->generator) {
  235. $this->generator = new DoctrineEntityGenerator($this->getContainer()->get('filesystem'), $this->getContainer()->get('doctrine'));
  236. }
  237. return $this->generator;
  238. }
  239. public function setGenerator(DoctrineEntityGenerator $generator)
  240. {
  241. $this->generator = $generator;
  242. }
  243. protected function getDialogHelper()
  244. {
  245. $dialog = $this->getHelperSet()->get('dialog');
  246. if (!$dialog || get_class($dialog) !== 'Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper') {
  247. $this->getHelperSet()->set($dialog = new DialogHelper());
  248. }
  249. return $dialog;
  250. }
  251. }