您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DefaultController.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\controllers;
  8. use Yii;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. /**
  12. * @author Qiang Xue <qiang.xue@gmail.com>
  13. * @since 2.0
  14. */
  15. class DefaultController extends Controller
  16. {
  17. public $layout = 'generator';
  18. /**
  19. * @var \yii\gii\Module
  20. */
  21. public $module;
  22. /**
  23. * @var \yii\gii\Generator
  24. */
  25. public $generator;
  26. public function actionIndex()
  27. {
  28. $this->layout = 'main';
  29. return $this->render('index');
  30. }
  31. public function actionView($id)
  32. {
  33. $generator = $this->loadGenerator($id);
  34. $params = ['generator' => $generator, 'id' => $id];
  35. $preview = Yii::$app->request->post('preview');
  36. $generate = Yii::$app->request->post('generate');
  37. $answers = Yii::$app->request->post('answers');
  38. if ($preview !== null || $generate !== null) {
  39. if ($generator->validate()) {
  40. $generator->saveStickyAttributes();
  41. $files = $generator->generate();
  42. if ($generate !== null && !empty($answers)) {
  43. $params['hasError'] = !$generator->save($files, (array) $answers, $results);
  44. $params['results'] = $results;
  45. } else {
  46. $params['files'] = $files;
  47. $params['answers'] = $answers;
  48. }
  49. }
  50. }
  51. return $this->render('view', $params);
  52. }
  53. public function actionPreview($id, $file)
  54. {
  55. $generator = $this->loadGenerator($id);
  56. if ($generator->validate()) {
  57. foreach ($generator->generate() as $f) {
  58. if ($f->id === $file) {
  59. $content = $f->preview();
  60. if ($content !== false) {
  61. return '<div class="content">' . $content . '</content>';
  62. } else {
  63. return '<div class="error">Preview is not available for this file type.</div>';
  64. }
  65. }
  66. }
  67. }
  68. throw new NotFoundHttpException("Code file not found: $file");
  69. }
  70. public function actionDiff($id, $file)
  71. {
  72. $generator = $this->loadGenerator($id);
  73. if ($generator->validate()) {
  74. foreach ($generator->generate() as $f) {
  75. if ($f->id === $file) {
  76. return $this->renderPartial('diff', [
  77. 'diff' => $f->diff(),
  78. ]);
  79. }
  80. }
  81. }
  82. throw new NotFoundHttpException("Code file not found: $file");
  83. }
  84. /**
  85. * Runs an action defined in the generator.
  86. * Given an action named "xyz", the method "actionXyz()" in the generator will be called.
  87. * If the method does not exist, a 400 HTTP exception will be thrown.
  88. * @param string $id the ID of the generator
  89. * @param string $name the action name
  90. * @return mixed the result of the action.
  91. * @throws NotFoundHttpException if the action method does not exist.
  92. */
  93. public function actionAction($id, $name)
  94. {
  95. $generator = $this->loadGenerator($id);
  96. $method = 'action' . $name;
  97. if (method_exists($generator, $method)) {
  98. return $generator->$method();
  99. } else {
  100. throw new NotFoundHttpException("Unknown generator action: $name");
  101. }
  102. }
  103. /**
  104. * Loads the generator with the specified ID.
  105. * @param string $id the ID of the generator to be loaded.
  106. * @return \yii\gii\Generator the loaded generator
  107. * @throws NotFoundHttpException
  108. */
  109. protected function loadGenerator($id)
  110. {
  111. if (isset($this->module->generators[$id])) {
  112. $this->generator = $this->module->generators[$id];
  113. $this->generator->loadStickyAttributes();
  114. $this->generator->load(Yii::$app->request->post());
  115. return $this->generator;
  116. } else {
  117. throw new NotFoundHttpException("Code generator not found: $id");
  118. }
  119. }
  120. }