Yii2 framework backup
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.

195 lines
4.7 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
10 years ago
namespace yii\gii\console;
use Yii;
use yii\console\Controller;
/**
* Allows you to run Gii from the command line.
* Example command:
*
* ```
* $ ./yii gii/<generator> --property1=foo --property2=bar --generate=true
* ```
*
* @author Tobias Munk <schmunk@usrbin.de>
* @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;
10 years ago
public $generators = [];
/**
10 years ago
* @var array generator option values
*/
10 years ago
private $_options = [];
10 years ago
/**
* @inheritdoc
*/
public function __get($name)
{
10 years ago
return isset($this->_options[$name]) ? $this->_options[$name] : null;
10 years ago
// todo: should determine which options are valid
10 years ago
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
return isset($this->_options[$name]) ? $this->_options[$name] : null;
} else {
return parent::__get($name);
}
} elseif (array_key_exists($name, $this->_options)) {
return $this->_options[$name];
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
$this->_options[$name] = $value;
return;
10 years ago
// todo: should determine which options are valid
10 years ago
if ($this->action) {
$options = $this->options($this->action->id);
if (in_array($name, $options)) {
$this->_options[$name] = $value;
} else {
parent::__set($name, $value);
}
} else {
$this->_options[$name] = $value;
}
}
10 years ago
public function init()
{
10 years ago
parent::init();
foreach ($this->generators as $id => $config) {
$this->generators[$id] = Yii::createObject($config);
}
}
10 years ago
public function createAction($id)
{
$action = parent::createAction($id);
foreach ($this->_options as $name => $value) {
$action->generator->$name = $value;
}
return $action;
}
/**
* @inheritdoc
*/
public function actions()
{
$actions = [];
10 years ago
foreach ($this->generators as $name => $generator) {
$actions[$name] = [
'class' => 'yii\gii\console\Action',
'generator' => $generator,
];
}
return $actions;
}
10 years ago
public function getUniqueID()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function options($id)
{
10 years ago
if (isset($this->generators[$id])) {
10 years ago
$attributes = $this->generators[$id]->attributes;
unset($attributes['templates']);
10 years ago
return array_merge(
parent::options($id),
10 years ago
array_keys($attributes)
10 years ago
);
} else {
return parent::options($id);
}
}
/**
* @inheritdoc
*/
public function getActionHelpSummary($action)
{
/** @var $action Action */
return $action->generator->getName();
}
/**
* @inheritdoc
*/
public function getActionHelp($action)
{
/** @var $action Action */
10 years ago
$description = $action->generator->getDescription();
return wordwrap(preg_replace('/\s+/', ' ', $description));
10 years ago
}
/**
* @inheritdoc
*/
public function getActionArgsHelp($action)
{
return [];
}
/**
* @inheritdoc
*/
public function getActionOptionsHelp($action)
{
/** @var $action Action */
$attributes = $action->generator->attributes;
10 years ago
unset($attributes['templates']);
10 years ago
$hints = $action->generator->hints();
$options = [];
foreach ($attributes as $name => $value) {
10 years ago
$type = gettype($value);
10 years ago
$options[$name] = [
10 years ago
'type' => $type === 'NULL' ? 'string' : $type,
'required' => $action->generator->isAttributeRequired($name),
10 years ago
'default' => $value,
10 years ago
'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
10 years ago
];
}
return $options;
}
10 years ago
protected function formatHint($hint)
{
$hint = preg_replace('%<code>(.*?)</code>%', '\1', $hint);
$hint = preg_replace('/\s+/', ' ', $hint);
return wordwrap($hint);
}
}