DefaultController.php 3.99 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

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

30 31 32
    public function actionIndex()
    {
        $this->layout = 'main';
Qiang Xue committed
33

34 35
        return $this->render('index');
    }
Qiang Xue committed
36

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

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

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    public function actionPreview($id, $file)
    {
        $generator = $this->loadGenerator($id);
        if ($generator->validate()) {
            foreach ($generator->generate() as $f) {
                if ($f->id === $file) {
                    $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>';
                    }
                }
            }
        }
        throw new NotFoundHttpException("Code file not found: $file");
    }
Qiang Xue committed
75

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

91 92 93 94
    /**
     * 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.
95 96 97
     * @param string $id the ID of the generator
     * @param string $name the action name
     * @return mixed the result of the action.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
     * @throws NotFoundHttpException if the action method does not exist.
     */
    public function actionAction($id, $name)
    {
        $generator = $this->loadGenerator($id);
        $method = 'action' . $name;
        if (method_exists($generator, $method)) {
            return $generator->$method();
        } else {
            throw new NotFoundHttpException("Unknown generator action: $name");
        }
    }

    /**
     * Loads the generator with the specified ID.
113 114
     * @param string $id the ID of the generator to be loaded.
     * @return \yii\gii\Generator the loaded generator
115 116 117 118 119 120 121 122 123 124 125 126 127 128
     * @throws NotFoundHttpException
     */
    protected function loadGenerator($id)
    {
        if (isset($this->module->generators[$id])) {
            $this->generator = $this->module->generators[$id];
            $this->generator->loadStickyAttributes();
            $this->generator->load($_POST);

            return $this->generator;
        } else {
            throw new NotFoundHttpException("Code generator not found: $id");
        }
    }
Qiang Xue committed
129
}