You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.0KB

  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. if (isset($_POST['preview']) || isset($_POST['generate'])) {
  36. if ($generator->validate()) {
  37. $generator->saveStickyAttributes();
  38. $files = $generator->generate();
  39. if (isset($_POST['generate']) && !empty($_POST['answers'])) {
  40. $params['hasError'] = !$generator->save($files, (array) $_POST['answers'], $results);
  41. $params['results'] = $results;
  42. } else {
  43. $params['files'] = $files;
  44. $params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
  45. }
  46. }
  47. }
  48. return $this->render('view', $params);
  49. }
  50. public function actionPreview($id, $file)
  51. {
  52. $generator = $this->loadGenerator($id);
  53. if ($generator->validate()) {
  54. foreach ($generator->generate() as $f) {
  55. if ($f->id === $file) {
  56. $content = $f->preview();
  57. if ($content !== false) {
  58. return '<div class="content">' . $content . '</content>';
  59. } else {
  60. return '<div class="error">Preview is not available for this file type.</div>';
  61. }
  62. }
  63. }
  64. }
  65. throw new NotFoundHttpException("Code file not found: $file");
  66. }
  67. public function actionDiff($id, $file)
  68. {
  69. $generator = $this->loadGenerator($id);
  70. if ($generator->validate()) {
  71. foreach ($generator->generate() as $f) {
  72. if ($f->id === $file) {
  73. return $this->renderPartial('diff', [
  74. 'diff' => $f->diff(),
  75. ]);
  76. }
  77. }
  78. }
  79. throw new NotFoundHttpException("Code file not found: $file");
  80. }
  81. /**
  82. * Runs an action defined in the generator.
  83. * Given an action named "xyz", the method "actionXyz()" in the generator will be called.
  84. * If the method does not exist, a 400 HTTP exception will be thrown.
  85. * @param string $id the ID of the generator
  86. * @param string $name the action name
  87. * @return mixed the result of the action.
  88. * @throws NotFoundHttpException if the action method does not exist.
  89. */
  90. public function actionAction($id, $name)
  91. {
  92. $generator = $this->loadGenerator($id);
  93. $method = 'action' . $name;
  94. if (method_exists($generator, $method)) {
  95. return $generator->$method();
  96. } else {
  97. throw new NotFoundHttpException("Unknown generator action: $name");
  98. }
  99. }
  100. /**
  101. * Loads the generator with the specified ID.
  102. * @param string $id the ID of the generator to be loaded.
  103. * @return \yii\gii\Generator the loaded generator
  104. * @throws NotFoundHttpException
  105. */
  106. protected function loadGenerator($id)
  107. {
  108. if (isset($this->module->generators[$id])) {
  109. $this->generator = $this->module->generators[$id];
  110. $this->generator->loadStickyAttributes();
  111. $this->generator->load($_POST);
  112. return $this->generator;
  113. } else {
  114. throw new NotFoundHttpException("Code generator not found: $id");
  115. }
  116. }
  117. }