DefaultController.php 3.9 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\gii\controllers;

10
use Yii;
Qiang Xue committed
11
use yii\web\Controller;
12
use yii\web\NotFoundHttpException;
Qiang Xue committed
13 14 15 16 17 18 19 20

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DefaultController extends Controller
{
	public $layout = 'generator';
Qiang Xue committed
21 22 23 24 25 26 27
	/**
	 * @var \yii\gii\Module
	 */
	public $module;
	/**
	 * @var \yii\gii\Generator
	 */
Qiang Xue committed
28 29 30 31 32 33 34 35 36 37 38
	public $generator;

	public function actionIndex()
	{
		$this->layout = 'main';
		return $this->render('index');
	}

	public function actionView($id)
	{
		$generator = $this->loadGenerator($id);
Alexander Makarov committed
39
		$params = ['generator' => $generator, 'id' => $id];
Qiang Xue committed
40 41
		if (isset($_POST['preview']) || isset($_POST['generate'])) {
			if ($generator->validate()) {
Qiang Xue committed
42
				$generator->saveStickyAttributes();
43
				$files = $generator->generate();
Qiang Xue committed
44
				if (isset($_POST['generate']) && !empty($_POST['answers'])) {
45 46
					$params['hasError'] = $generator->save($files, (array)$_POST['answers'], $results);
					$params['results'] = $results;
Qiang Xue committed
47 48
				} else {
					$params['files'] = $files;
Qiang Xue committed
49
					$params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
Qiang Xue committed
50 51 52 53 54
				}
			}
		}

		return $this->render('view', $params);
Qiang Xue committed
55 56
	}

Qiang Xue committed
57
	public function actionPreview($id, $file)
Qiang Xue committed
58
	{
Qiang Xue committed
59 60
		$generator = $this->loadGenerator($id);
		if ($generator->validate()) {
61
			foreach ($generator->generate() as $f) {
Qiang Xue committed
62
				if ($f->id === $file) {
63 64 65 66 67 68
					$content = $f->preview();
					if ($content !== false) {
						return  '<div class="content">' . $content . '</content>';
					} else {
						return '<div class="error">Preview is not available for this file type.</div>';
					}
Qiang Xue committed
69 70 71
				}
			}
		}
72
		throw new NotFoundHttpException("Code file not found: $file");
Qiang Xue committed
73 74
	}

Qiang Xue committed
75
	public function actionDiff($id, $file)
Qiang Xue committed
76
	{
Qiang Xue committed
77 78
		$generator = $this->loadGenerator($id);
		if ($generator->validate()) {
79
			foreach ($generator->generate() as $f) {
Qiang Xue committed
80
				if ($f->id === $file) {
Alexander Makarov committed
81
					return $this->renderPartial('diff', [
Qiang Xue committed
82
						'diff' => $f->diff(),
Alexander Makarov committed
83
					]);
Qiang Xue committed
84 85 86
				}
			}
		}
87
		throw new NotFoundHttpException("Code file not found: $file");
Qiang Xue committed
88
	}
Qiang Xue committed
89

90 91 92 93 94 95 96
	/**
	 * Runs an action defined in the generator.
	 * Given an action named "xyz", the method "actionXyz()" in the generator will be called.
	 * If the method does not exist, a 400 HTTP exception will be thrown.
	 * @param string $id the ID of the generator
	 * @param string $name the action name
	 * @return mixed the result of the action.
97
	 * @throws NotFoundHttpException if the action method does not exist.
98 99 100 101 102 103 104 105
	 */
	public function actionAction($id, $name)
	{
		$generator = $this->loadGenerator($id);
		$method = 'action' . $name;
		if (method_exists($generator, $method)) {
			return $generator->$method();
		} else {
106
			throw new NotFoundHttpException("Unknown generator action: $name");
107 108 109
		}
	}

Alexander Makarov committed
110
	public function createUrl($route, $params = [])
Qiang Xue committed
111 112 113 114 115 116 117 118 119 120
	{
		if (!isset($params['id']) && $this->generator !== null) {
			foreach ($this->module->generators as $id => $generator) {
				if ($generator === $this->generator) {
					$params['id'] = $id;
					break;
				}
			}
		}
		return parent::createUrl($route, $params);
Qiang Xue committed
121 122
	}

Alexander Makarov committed
123
	public function createActionUrl($name, $params = [])
124 125 126 127 128 129 130 131 132 133 134
	{
		foreach ($this->module->generators as $id => $generator) {
			if ($generator === $this->generator) {
				$params['id'] = $id;
				break;
			}
		}
		$params['name'] = $name;
		return parent::createUrl('action', $params);
	}

Qiang Xue committed
135 136 137 138
	/**
	 * Loads the generator with the specified ID.
	 * @param string $id the ID of the generator to be loaded.
	 * @return \yii\gii\Generator the loaded generator
139
	 * @throws NotFoundHttpException
Qiang Xue committed
140
	 */
Qiang Xue committed
141 142 143
	protected function loadGenerator($id)
	{
		if (isset($this->module->generators[$id])) {
Qiang Xue committed
144
			$this->generator = $this->module->generators[$id];
Qiang Xue committed
145
			$this->generator->loadStickyAttributes();
Qiang Xue committed
146 147
			$this->generator->load($_POST);
			return $this->generator;
Qiang Xue committed
148
		} else {
149
			throw new NotFoundHttpException("Code generator not found: $id");
Qiang Xue committed
150 151 152
		}
	}
}