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.
153 lines
3.9 KiB
153 lines
3.9 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\gii\controllers;
|
||
|
|
||
11 years ago
|
use Yii;
|
||
11 years ago
|
use yii\web\Controller;
|
||
11 years ago
|
use yii\web\NotFoundHttpException;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class DefaultController extends Controller
|
||
|
{
|
||
|
public $layout = 'generator';
|
||
11 years ago
|
/**
|
||
|
* @var \yii\gii\Module
|
||
|
*/
|
||
|
public $module;
|
||
|
/**
|
||
|
* @var \yii\gii\Generator
|
||
|
*/
|
||
11 years ago
|
public $generator;
|
||
|
|
||
|
public function actionIndex()
|
||
|
{
|
||
|
$this->layout = 'main';
|
||
|
return $this->render('index');
|
||
|
}
|
||
|
|
||
|
public function actionView($id)
|
||
|
{
|
||
|
$generator = $this->loadGenerator($id);
|
||
11 years ago
|
$params = ['generator' => $generator, 'id' => $id];
|
||
11 years ago
|
if (isset($_POST['preview']) || isset($_POST['generate'])) {
|
||
|
if ($generator->validate()) {
|
||
11 years ago
|
$generator->saveStickyAttributes();
|
||
11 years ago
|
$files = $generator->generate();
|
||
11 years ago
|
if (isset($_POST['generate']) && !empty($_POST['answers'])) {
|
||
11 years ago
|
$params['hasError'] = $generator->save($files, (array)$_POST['answers'], $results);
|
||
|
$params['results'] = $results;
|
||
11 years ago
|
} else {
|
||
|
$params['files'] = $files;
|
||
11 years ago
|
$params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $this->render('view', $params);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public function actionPreview($id, $file)
|
||
11 years ago
|
{
|
||
11 years ago
|
$generator = $this->loadGenerator($id);
|
||
|
if ($generator->validate()) {
|
||
11 years ago
|
foreach ($generator->generate() as $f) {
|
||
11 years ago
|
if ($f->id === $file) {
|
||
11 years ago
|
$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>';
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
throw new NotFoundHttpException("Code file not found: $file");
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public function actionDiff($id, $file)
|
||
11 years ago
|
{
|
||
11 years ago
|
$generator = $this->loadGenerator($id);
|
||
|
if ($generator->validate()) {
|
||
11 years ago
|
foreach ($generator->generate() as $f) {
|
||
11 years ago
|
if ($f->id === $file) {
|
||
11 years ago
|
return $this->renderPartial('diff', [
|
||
11 years ago
|
'diff' => $f->diff(),
|
||
11 years ago
|
]);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
throw new NotFoundHttpException("Code file not found: $file");
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* 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.
|
||
11 years ago
|
* @throws NotFoundHttpException if the action method does not exist.
|
||
11 years ago
|
*/
|
||
|
public function actionAction($id, $name)
|
||
|
{
|
||
|
$generator = $this->loadGenerator($id);
|
||
|
$method = 'action' . $name;
|
||
|
if (method_exists($generator, $method)) {
|
||
|
return $generator->$method();
|
||
|
} else {
|
||
11 years ago
|
throw new NotFoundHttpException("Unknown generator action: $name");
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
public function createUrl($route, $params = [])
|
||
11 years ago
|
{
|
||
|
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);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public function createActionUrl($name, $params = [])
|
||
11 years ago
|
{
|
||
|
foreach ($this->module->generators as $id => $generator) {
|
||
|
if ($generator === $this->generator) {
|
||
|
$params['id'] = $id;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
$params['name'] = $name;
|
||
|
return parent::createUrl('action', $params);
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* 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
|
||
11 years ago
|
* @throws NotFoundHttpException
|
||
11 years ago
|
*/
|
||
11 years ago
|
protected function loadGenerator($id)
|
||
|
{
|
||
|
if (isset($this->module->generators[$id])) {
|
||
11 years ago
|
$this->generator = $this->module->generators[$id];
|
||
11 years ago
|
$this->generator->loadStickyAttributes();
|
||
11 years ago
|
$this->generator->load($_POST);
|
||
|
return $this->generator;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
throw new NotFoundHttpException("Code generator not found: $id");
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|