Commit 11a4d6de by Qiang Xue

console gii WIP

parent dd6f4442
...@@ -47,7 +47,7 @@ abstract class Generator extends Model ...@@ -47,7 +47,7 @@ abstract class Generator extends Model
* @var string the name of the code template that the user has selected. * @var string the name of the code template that the user has selected.
* The value of this property is internally managed by this class. * 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. * @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 ...@@ -84,11 +84,19 @@ class Module extends \yii\base\Module implements BootstrapInterface
*/ */
public function bootstrap($app) public function bootstrap($app)
{ {
if ($app instanceof \yii\web\Application) {
$app->getUrlManager()->addRules([ $app->getUrlManager()->addRules([
$this->id => $this->id . '/default/index', $this->id => $this->id . '/default/index',
$this->id . '/<id:\w+>' => $this->id . '/default/view', $this->id . '/<id:\w+>' => $this->id . '/default/view',
$this->id . '/<controller:\w+>/<action:\w+>' => $this->id . '/<controller>/<action>', $this->id . '/<controller:\w+>/<action:\w+>' => $this->id . '/<controller>/<action>',
], false); ], 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 ...@@ -100,7 +108,7 @@ class Module extends \yii\base\Module implements BootstrapInterface
return false; 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.'); throw new ForbiddenHttpException('You are not allowed to access this page.');
} }
...@@ -118,8 +126,10 @@ class Module extends \yii\base\Module implements BootstrapInterface ...@@ -118,8 +126,10 @@ class Module extends \yii\base\Module implements BootstrapInterface
*/ */
protected function resetGlobalSettings() protected function resetGlobalSettings()
{ {
if (Yii::$app instanceof \yii\web\Application) {
Yii::$app->assetManager->bundles = []; Yii::$app->assetManager->bundles = [];
} }
}
/** /**
* @return boolean whether the module can be accessed by the current user * @return boolean whether the module can be accessed by the current user
......
...@@ -13,20 +13,17 @@ namespace yii\gii\console; ...@@ -13,20 +13,17 @@ namespace yii\gii\console;
*/ */
class Action extends \yii\base\Action class Action extends \yii\base\Action
{ {
/**
* @var \yii\gii\Generator
*/
public $generator; public $generator;
// TODO: is there are better way, needed for `./yii help gii`
public function getUniqueId()
{
return 'gii/' . $this->generatorName;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function run() public function run()
{ {
echo "Loading generator '$this->generatorName'...\n\n"; echo "Loading generator '$this->id'...\n\n";
if ($this->generator->validate()) { if ($this->generator->validate()) {
$files = $this->generator->generate(); $files = $this->generator->generate();
$answers = []; $answers = [];
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\gii\commands; namespace yii\gii\console;
use Yii; use Yii;
use yii\console\Controller; use yii\console\Controller;
...@@ -31,34 +31,78 @@ class GenerateController extends Controller ...@@ -31,34 +31,78 @@ class GenerateController extends Controller
* @var boolean whether to generate all files and overwrite existing files * @var boolean whether to generate all files and overwrite existing files
*/ */
public $generate = false; 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->_options[$name] = $value;
$this->_attributes[$key] = $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 parent::init();
if (isset($this->_attributes[$key])) { foreach ($this->generators as $id => $config) {
return $this->_attributes[$key]; $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 * @inheritdoc
*/ */
public function actions() public function actions()
{ {
$actions = []; $actions = [];
foreach ($this->module->generators as $name => $generator) { foreach ($this->generators as $name => $generator) {
// create a generate action for every generator
$actions[$name] = [ $actions[$name] = [
'class' => 'yii\gii\console\Action', 'class' => 'yii\gii\console\Action',
'generator' => $generator, 'generator' => $generator,
...@@ -67,15 +111,70 @@ class GenerateController extends Controller ...@@ -67,15 +111,70 @@ class GenerateController extends Controller
return $actions; return $actions;
} }
public function getUniqueID()
{
return $this->id;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function options($id) public function options($id)
{ {
$generator = $this->module->generators[$id]; if (isset($this->generators[$id])) {
return array_merge( return array_merge(
parent::options($id), 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