Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GenerateController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\console;
  8. use Yii;
  9. use yii\base\InlineAction;
  10. use yii\console\Controller;
  11. /**
  12. * This is the command line version of Gii - a code generator.
  13. *
  14. * You can use this command to generate models, controllers, etc. For example,
  15. * to generate an ActiveRecord model based on a DB table, you can run:
  16. *
  17. * ```
  18. * $ ./yii gii/model --tableName=city --modelClass=City
  19. * ```
  20. *
  21. * @author Tobias Munk <schmunk@usrbin.de>
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @since 2.0
  24. */
  25. class GenerateController extends Controller
  26. {
  27. /**
  28. * @var \yii\gii\Module
  29. */
  30. public $module;
  31. /**
  32. * @var boolean whether to overwrite all existing code files when in non-interactive mode.
  33. * Defaults to false, meaning none of the existing code files will be overwritten.
  34. * This option is used only when `--interactive=0`.
  35. */
  36. public $overwrite = false;
  37. /**
  38. * @var array a list of the available code generators
  39. */
  40. public $generators = [];
  41. /**
  42. * @var array generator option values
  43. */
  44. private $_options = [];
  45. /**
  46. * @inheritdoc
  47. */
  48. public function __get($name)
  49. {
  50. return isset($this->_options[$name]) ? $this->_options[$name] : null;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function __set($name, $value)
  56. {
  57. $this->_options[$name] = $value;
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function init()
  63. {
  64. parent::init();
  65. foreach ($this->generators as $id => $config) {
  66. $this->generators[$id] = Yii::createObject($config);
  67. }
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function createAction($id)
  73. {
  74. /** @var $action GenerateAction */
  75. $action = parent::createAction($id);
  76. foreach ($this->_options as $name => $value) {
  77. $action->generator->$name = $value;
  78. }
  79. return $action;
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function actions()
  85. {
  86. $actions = [];
  87. foreach ($this->generators as $name => $generator) {
  88. $actions[$name] = [
  89. 'class' => 'yii\gii\console\GenerateAction',
  90. 'generator' => $generator,
  91. ];
  92. }
  93. return $actions;
  94. }
  95. public function actionIndex()
  96. {
  97. $this->run('/help', ['gii']);
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function getUniqueID()
  103. {
  104. return $this->id;
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function options($id)
  110. {
  111. $options = parent::options($id);
  112. $options[] = 'overwrite';
  113. if (!isset($this->generators[$id])) {
  114. return $options;
  115. }
  116. $attributes = $this->generators[$id]->attributes;
  117. unset($attributes['templates']);
  118. return array_merge(
  119. $options,
  120. array_keys($attributes)
  121. );
  122. }
  123. /**
  124. * @inheritdoc
  125. */
  126. public function getActionHelpSummary($action)
  127. {
  128. if ($action instanceof InlineAction) {
  129. return parent::getActionHelpSummary($action);
  130. } else {
  131. /** @var $action GenerateAction */
  132. return $action->generator->getName();
  133. }
  134. }
  135. /**
  136. * @inheritdoc
  137. */
  138. public function getActionHelp($action)
  139. {
  140. if ($action instanceof InlineAction) {
  141. return parent::getActionHelp($action);
  142. } else {
  143. /** @var $action GenerateAction */
  144. $description = $action->generator->getDescription();
  145. return wordwrap(preg_replace('/\s+/', ' ', $description));
  146. }
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function getActionArgsHelp($action)
  152. {
  153. return [];
  154. }
  155. /**
  156. * @inheritdoc
  157. */
  158. public function getActionOptionsHelp($action)
  159. {
  160. if ($action instanceof InlineAction) {
  161. return parent::getActionOptionsHelp($action);
  162. }
  163. /** @var $action GenerateAction */
  164. $attributes = $action->generator->attributes;
  165. unset($attributes['templates']);
  166. $hints = $action->generator->hints();
  167. $options = parent::getActionOptionsHelp($action);
  168. foreach ($attributes as $name => $value) {
  169. $type = gettype($value);
  170. $options[$name] = [
  171. 'type' => $type === 'NULL' ? 'string' : $type,
  172. 'required' => $value === null && $action->generator->isAttributeRequired($name),
  173. 'default' => $value,
  174. 'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
  175. ];
  176. }
  177. return $options;
  178. }
  179. protected function formatHint($hint)
  180. {
  181. $hint = preg_replace('%<code>(.*?)</code>%', '\1', $hint);
  182. $hint = preg_replace('/\s+/', ' ', $hint);
  183. return wordwrap($hint);
  184. }
  185. }