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

Qiang Xue committed
8
namespace yii\gii\console;
Qiang Xue committed
9 10

use Yii;
Qiang Xue committed
11
use yii\base\InlineAction;
Qiang Xue committed
12 13 14
use yii\console\Controller;

/**
Qiang Xue committed
15 16 17 18
 * This is the command line version of Gii - a code generator.
 *
 * You can use this command to generate models, controllers, etc. For example,
 * to generate an ActiveRecord model based on a DB table, you can run:
Qiang Xue committed
19 20
 *
 * ```
Qiang Xue committed
21
 * $ ./yii gii/model --tableName=city --modelClass=City
Qiang Xue committed
22 23 24
 * ```
 *
 * @author Tobias Munk <schmunk@usrbin.de>
Qiang Xue committed
25
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
26 27 28 29 30 31 32 33 34 35 36 37
 * @since  2.0
 */
class GenerateController extends Controller
{
    /**
     * @var \yii\gii\Module
     */
    public $module;
    /**
     * @var boolean whether to generate all files and overwrite existing files
     */
    public $generate = false;
Qiang Xue committed
38 39 40
    /**
     * @var array a list of the available code generators
     */
Qiang Xue committed
41
    public $generators = [];
Qiang Xue committed
42 43

    /**
Qiang Xue committed
44
     * @var array generator option values
Qiang Xue committed
45
     */
Qiang Xue committed
46
    private $_options = [];
Qiang Xue committed
47

Qiang Xue committed
48 49 50 51 52

    /**
     * @inheritdoc
     */
    public function __get($name)
Qiang Xue committed
53
    {
Qiang Xue committed
54 55 56 57 58 59 60 61 62
        return isset($this->_options[$name]) ? $this->_options[$name] : null;
    }

    /**
     * @inheritdoc
     */
    public function __set($name, $value)
    {
        $this->_options[$name] = $value;
Qiang Xue committed
63 64
    }

Qiang Xue committed
65 66 67
    /**
     * @inheritdoc
     */
Qiang Xue committed
68
    public function init()
Qiang Xue committed
69
    {
Qiang Xue committed
70 71 72
        parent::init();
        foreach ($this->generators as $id => $config) {
            $this->generators[$id] = Yii::createObject($config);
Qiang Xue committed
73 74 75
        }
    }

Qiang Xue committed
76 77 78
    /**
     * @inheritdoc
     */
Qiang Xue committed
79 80
    public function createAction($id)
    {
Qiang Xue committed
81
        /** @var $action GenerateAction */
Qiang Xue committed
82 83 84 85 86 87 88
        $action = parent::createAction($id);
        foreach ($this->_options as $name => $value) {
            $action->generator->$name = $value;
        }
        return $action;
    }

Qiang Xue committed
89 90 91 92 93 94
    /**
     * @inheritdoc
     */
    public function actions()
    {
        $actions = [];
Qiang Xue committed
95
        foreach ($this->generators as $name => $generator) {
Qiang Xue committed
96
            $actions[$name] = [
Qiang Xue committed
97
                'class' => 'yii\gii\console\GenerateAction',
Qiang Xue committed
98 99 100 101 102 103
                'generator' => $generator,
            ];
        }
        return $actions;
    }

Qiang Xue committed
104 105 106 107 108 109 110 111
    public function actionIndex()
    {
        $this->run('/help', ['gii']);
    }

    /**
     * @inheritdoc
     */
Qiang Xue committed
112 113 114 115 116
    public function getUniqueID()
    {
        return $this->id;
    }

Qiang Xue committed
117 118 119 120 121
    /**
     * @inheritdoc
     */
    public function options($id)
    {
Qiang Xue committed
122
        if (isset($this->generators[$id])) {
Qiang Xue committed
123 124
            $attributes = $this->generators[$id]->attributes;
            unset($attributes['templates']);
Qiang Xue committed
125 126
            return array_merge(
                parent::options($id),
Qiang Xue committed
127
                array_keys($attributes)
Qiang Xue committed
128 129 130 131 132 133 134 135 136 137 138
            );
        } else {
            return parent::options($id);
        }
    }

    /**
     * @inheritdoc
     */
    public function getActionHelpSummary($action)
    {
Qiang Xue committed
139 140 141 142 143 144
        if ($action instanceof InlineAction) {
            return parent::getActionHelpSummary($action);
        } else {
            /** @var $action GenerateAction */
            return $action->generator->getName();
        }
Qiang Xue committed
145 146 147 148 149 150 151
    }

    /**
     * @inheritdoc
     */
    public function getActionHelp($action)
    {
Qiang Xue committed
152 153 154 155 156 157 158
        if ($action instanceof InlineAction) {
            return parent::getActionHelp($action);
        } else {
            /** @var $action GenerateAction */
            $description = $action->generator->getDescription();
            return wordwrap(preg_replace('/\s+/', ' ', $description));
        }
Qiang Xue committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    }

    /**
     * @inheritdoc
     */
    public function getActionArgsHelp($action)
    {
        return [];
    }

    /**
     * @inheritdoc
     */
    public function getActionOptionsHelp($action)
    {
Qiang Xue committed
174 175 176 177
        if ($action instanceof InlineAction) {
            return parent::getActionOptionsHelp($action);
        }
        /** @var $action GenerateAction */
Qiang Xue committed
178
        $attributes = $action->generator->attributes;
Qiang Xue committed
179
        unset($attributes['templates']);
Qiang Xue committed
180 181 182 183
        $hints = $action->generator->hints();

        $options = [];
        foreach ($attributes as $name => $value) {
Qiang Xue committed
184
            $type = gettype($value);
Qiang Xue committed
185
            $options[$name] = [
Qiang Xue committed
186
                'type' => $type === 'NULL' ? 'string' : $type,
Qiang Xue committed
187
                'required' => $value === null && $action->generator->isAttributeRequired($name),
Qiang Xue committed
188
                'default' => $value,
Qiang Xue committed
189
                'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
Qiang Xue committed
190 191 192 193
            ];
        }

        return $options;
Qiang Xue committed
194
    }
Qiang Xue committed
195 196 197 198 199 200 201

    protected function formatHint($hint)
    {
        $hint = preg_replace('%<code>(.*?)</code>%', '\1', $hint);
        $hint = preg_replace('/\s+/', ' ', $hint);
        return wordwrap($hint);
    }
Qiang Xue committed
202
}