mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
gii command line WIP
This commit is contained in:
50
extensions/gii/console/Action.php
Normal file
50
extensions/gii/console/Action.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\gii\console;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Action extends \yii\base\Action
|
||||
{
|
||||
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";
|
||||
if ($this->generator->validate()) {
|
||||
$files = $this->generator->generate();
|
||||
$answers = [];
|
||||
foreach ($files AS $file) {
|
||||
$answers[$file->id] = true;
|
||||
}
|
||||
$params['hasError'] = $this->generator->save($files, (array)$answers, $results);
|
||||
$params['results'] = $results;
|
||||
echo $params['hasError'];
|
||||
echo "\n";
|
||||
echo $results;
|
||||
} else {
|
||||
echo "Attribute Errors\n";
|
||||
echo "----------------\n";
|
||||
foreach ($this->generator->errors AS $attribute => $errors) {
|
||||
echo "$attribute: " . implode('; ', $errors) . "\n";
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
81
extensions/gii/console/GenerateController.php
Normal file
81
extensions/gii/console/GenerateController.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\gii\commands;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @var array stores generator attributes
|
||||
*/
|
||||
private $_attributes = [];
|
||||
|
||||
public function __set($key, $value)
|
||||
{
|
||||
// todo: check if $key is a valid option
|
||||
$this->_attributes[$key] = $value;
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
// todo: check if $key is a valid option
|
||||
if (isset($this->_attributes[$key])) {
|
||||
return $this->_attributes[$key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function actions()
|
||||
{
|
||||
$actions = [];
|
||||
foreach ($this->module->generators as $name => $generator) {
|
||||
// create a generate action for every generator
|
||||
$actions[$name] = [
|
||||
'class' => 'yii\gii\console\Action',
|
||||
'generator' => $generator,
|
||||
];
|
||||
}
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function options($id)
|
||||
{
|
||||
$generator = $this->module->generators[$id];
|
||||
return array_merge(
|
||||
parent::options($id),
|
||||
array_keys($generator->attributes) // global for all actions
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user