GenerateController.php 4.89 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
 * @since  2.0
 */
class GenerateController extends Controller
{
    /**
     * @var \yii\gii\Module
     */
    public $module;
34 35 36 37 38 39
    /**
     * @var boolean whether to overwrite all existing code files when in non-interactive mode.
     * Defaults to false, meaning none of the existing code files will be overwritten.
     * This option is used only when `--interactive=0`.
     */
    public $overwrite = false;
Qiang Xue committed
40 41 42
    /**
     * @var array a list of the available code generators
     */
Qiang Xue committed
43
    public $generators = [];
Qiang Xue committed
44 45

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

Qiang Xue committed
50 51 52 53 54

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

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

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

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

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

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

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

Qiang Xue committed
119 120 121 122 123
    /**
     * @inheritdoc
     */
    public function options($id)
    {
124 125 126 127 128
        $options = parent::options($id);
        $options[] = 'overwrite';

        if (!isset($this->generators[$id])) {
            return $options;
Qiang Xue committed
129
        }
130 131 132 133 134 135 136

        $attributes = $this->generators[$id]->attributes;
        unset($attributes['templates']);
        return array_merge(
            $options,
            array_keys($attributes)
        );
Qiang Xue committed
137 138 139 140 141 142 143
    }

    /**
     * @inheritdoc
     */
    public function getActionHelpSummary($action)
    {
Qiang Xue committed
144 145 146 147 148 149
        if ($action instanceof InlineAction) {
            return parent::getActionHelpSummary($action);
        } else {
            /** @var $action GenerateAction */
            return $action->generator->getName();
        }
Qiang Xue committed
150 151 152 153 154 155 156
    }

    /**
     * @inheritdoc
     */
    public function getActionHelp($action)
    {
Qiang Xue committed
157 158 159 160 161 162 163
        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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    }

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

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

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

        return $options;
Qiang Xue committed
199
    }
Qiang Xue committed
200 201 202 203 204 205 206

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