Commit 11a4d6de by Qiang Xue

console gii WIP

parent dd6f4442
......@@ -47,7 +47,7 @@ abstract class Generator extends Model
* @var string the name of the code template that the user has selected.
* The value of this property is internally managed by this class.
*/
public $template;
public $template = 'default';
/**
* @var boolean whether the strings will be generated using `Yii::t()` or normal strings.
*/
......
......@@ -84,11 +84,19 @@ class Module extends \yii\base\Module implements BootstrapInterface
*/
public function bootstrap($app)
{
if ($app instanceof \yii\web\Application) {
$app->getUrlManager()->addRules([
$this->id => $this->id . '/default/index',
$this->id . '/<id:\w+>' => $this->id . '/default/view',
$this->id . '/<controller:\w+>/<action:\w+>' => $this->id . '/<controller>/<action>',
], false);
} elseif ($app instanceof \yii\console\Application) {
$app->controllerMap[$this->id] = [
'class' => 'yii\gii\console\GenerateController',
'generators' => array_merge($this->coreGenerators(), $this->generators),
'module' => $this,
];
}
}
/**
......@@ -100,7 +108,7 @@ class Module extends \yii\base\Module implements BootstrapInterface
return false;
}
if (!$this->checkAccess()) {
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
......@@ -118,8 +126,10 @@ class Module extends \yii\base\Module implements BootstrapInterface
*/
protected function resetGlobalSettings()
{
if (Yii::$app instanceof \yii\web\Application) {
Yii::$app->assetManager->bundles = [];
}
}
/**
* @return boolean whether the module can be accessed by the current user
......
......@@ -13,20 +13,17 @@ namespace yii\gii\console;
*/
class Action extends \yii\base\Action
{
/**
* @var \yii\gii\Generator
*/
public $generator;
// TODO: is there are better way, needed for `./yii help gii`
public function getUniqueId()
{
return 'gii/' . $this->generatorName;
}
/**
* @inheritdoc
*/
public function run()
{
echo "Loading generator '$this->generatorName'...\n\n";
echo "Loading generator '$this->id'...\n\n";
if ($this->generator->validate()) {
$files = $this->generator->generate();
$answers = [];
......
......@@ -5,7 +5,7 @@
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\commands;
namespace yii\gii\console;
use Yii;
use yii\console\Controller;
......@@ -31,34 +31,78 @@ class GenerateController extends Controller
* @var boolean whether to generate all files and overwrite existing files
*/
public $generate = false;
public $generators = [];
/**
* @var array stores generator attributes
* @var array generator option values
*/
private $_attributes = [];
private $_options = [];
public function __set($key, $value)
/**
* @inheritdoc
*/
public function __get($name)
{
return isset($this->_options[$name]) ? $this->_options[$name] : null;
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)
{
// todo: check if $key is a valid option
$this->_attributes[$key] = $value;
$this->_options[$name] = $value;
return;
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;
}
}
public function __get($key)
public function init()
{
// todo: check if $key is a valid option
if (isset($this->_attributes[$key])) {
return $this->_attributes[$key];
parent::init();
foreach ($this->generators as $id => $config) {
$this->generators[$id] = Yii::createObject($config);
}
}
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 = [];
foreach ($this->module->generators as $name => $generator) {
// create a generate action for every generator
foreach ($this->generators as $name => $generator) {
$actions[$name] = [
'class' => 'yii\gii\console\Action',
'generator' => $generator,
......@@ -67,15 +111,70 @@ class GenerateController extends Controller
return $actions;
}
public function getUniqueID()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function options($id)
{
$generator = $this->module->generators[$id];
if (isset($this->generators[$id])) {
return array_merge(
parent::options($id),
array_keys($generator->attributes) // global for all actions
array_keys($this->generators[$id]->attributes)
);
} 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 */
return $action->generator->getDescription();
}
/**
* @inheritdoc
*/
public function getActionArgsHelp($action)
{
return [];
}
/**
* @inheritdoc
*/
public function getActionOptionsHelp($action)
{
/** @var $action Action */
$attributes = $action->generator->attributes;
$hints = $action->generator->hints();
$options = [];
foreach ($attributes as $name => $value) {
$options[$name] = [
'type' => 'string',
'default' => $value,
'comment' => isset($hints[$name]) ? $hints[$name] : '',
];
}
return $options;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment