This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\console;
use yii\helpers\Console;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class GenerateAction extends \yii\base\Action
{
/**
* @var \yii\gii\Generator
*/
public $generator;
/**
* @var GenerateController
*/
public $controller;
/**
* {@inheritdoc}
*/
public function run()
{
echo "Running '{$this->generator->getName()}'...\n\n";
if ($this->generator->validate()) {
$this->generateCode();
} else {
$this->displayValidationErrors();
}
}
protected function displayValidationErrors()
{
$this->controller->stdout("Code not generated. Please fix the following errors:\n\n", Console::FG_RED);
foreach ($this->generator->errors as $attribute => $errors) {
echo ' - ' . $this->controller->ansiFormat($attribute, Console::FG_CYAN) . ': ' . implode('; ', $errors) . "\n";
}
echo "\n";
}
protected function generateCode()
{
$files = $this->generator->generate();
$n = count($files);
if ($n === 0) {
echo "No code to be generated.\n";
return;
}
echo "The following files will be generated:\n";
$skipAll = $this->controller->interactive ? null : !$this->controller->overwrite;
$answers = [];
foreach ($files as $file) {
$path = $file->getRelativePath();
if (is_file($file->path)) {
if (file_get_contents($file->path) === $file->content) {
echo ' ' . $this->controller->ansiFormat('[unchanged]', Console::FG_GREY);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
$answers[$file->id] = false;
} else {
echo ' ' . $this->controller->ansiFormat('[changed]', Console::FG_RED);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
if ($skipAll !== null) {
$answers[$file->id] = !$skipAll;
} else {
$answer = $this->controller->select("Do you want to overwrite this file?", [
'y' => 'Overwrite this file.',
'n' => 'Skip this file.',
'ya' => 'Overwrite this and the rest of the changed files.',
'na' => 'Skip this and the rest of the changed files.',
]);
$answers[$file->id] = $answer === 'y' || $answer === 'ya';
if ($answer === 'ya') {
$skipAll = false;
} elseif ($answer === 'na') {
$skipAll = true;
}
}
}
} else {
echo ' ' . $this->controller->ansiFormat('[new]', Console::FG_GREEN);
echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
$answers[$file->id] = true;
}
}
if (!array_sum($answers)) {
$this->controller->stdout("\nNo files were chosen to be generated.\n", Console::FG_CYAN);
return;
}
if (!$this->controller->confirm("\nReady to generate the selected files?", true)) {
$this->controller->stdout("\nNo file was generated.\n", Console::FG_CYAN);
return;
}
if ($this->generator->save($files, (array) $answers, $results)) {
$this->controller->stdout("\nFiles were generated successfully!\n", Console::FG_GREEN);
} else {
$this->controller->stdout("\nSome errors occurred while generating the files.", Console::FG_RED);
}
echo preg_replace('%<span class="error">(.*?)</span>%', '\1', $results) . "\n";
}
}

View File

@@ -0,0 +1,207 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\console;
use Yii;
use yii\base\InlineAction;
use yii\console\Controller;
/**
* 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:
*
* ```
* $ ./yii gii/model --tableName=city --modelClass=City
* ```
*
* @author Tobias Munk <schmunk@usrbin.de>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class GenerateController extends Controller
{
/**
* @var \yii\gii\Module
*/
public $module;
/**
* @var bool 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;
/**
* @var array a list of the available code generators
*/
public $generators = [];
/**
* @var array generator option values
*/
private $_options = [];
/**
* {@inheritdoc}
*/
public function __get($name)
{
return isset($this->_options[$name]) ? $this->_options[$name] : null;
}
/**
* {@inheritdoc}
*/
public function __set($name, $value)
{
$this->_options[$name] = $value;
}
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
foreach ($this->generators as $id => $config) {
$this->generators[$id] = Yii::createObject($config);
}
}
/**
* {@inheritdoc}
*/
public function createAction($id)
{
/** @var $action GenerateAction */
$action = parent::createAction($id);
foreach ($this->_options as $name => $value) {
$action->generator->$name = $value;
}
return $action;
}
/**
* {@inheritdoc}
*/
public function actions()
{
$actions = [];
foreach ($this->generators as $name => $generator) {
$actions[$name] = [
'class' => 'yii\gii\console\GenerateAction',
'generator' => $generator,
];
}
return $actions;
}
public function actionIndex()
{
$this->run('/help', ['gii']);
}
/**
* {@inheritdoc}
*/
public function getUniqueID()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function options($id)
{
$options = parent::options($id);
$options[] = 'overwrite';
if (!isset($this->generators[$id])) {
return $options;
}
$attributes = $this->generators[$id]->attributes;
unset($attributes['templates']);
return array_merge(
$options,
array_keys($attributes)
);
}
/**
* {@inheritdoc}
*/
public function getActionHelpSummary($action)
{
if ($action instanceof InlineAction) {
return parent::getActionHelpSummary($action);
} else {
/** @var $action GenerateAction */
return $action->generator->getName();
}
}
/**
* {@inheritdoc}
*/
public function getActionHelp($action)
{
if ($action instanceof InlineAction) {
return parent::getActionHelp($action);
} else {
/** @var $action GenerateAction */
$description = $action->generator->getDescription();
return wordwrap(preg_replace('/\s+/', ' ', $description));
}
}
/**
* {@inheritdoc}
*/
public function getActionArgsHelp($action)
{
return [];
}
/**
* {@inheritdoc}
*/
public function getActionOptionsHelp($action)
{
if ($action instanceof InlineAction) {
return parent::getActionOptionsHelp($action);
}
/** @var $action GenerateAction */
$attributes = $action->generator->attributes;
unset($attributes['templates']);
$hints = $action->generator->hints();
$options = parent::getActionOptionsHelp($action);
foreach ($attributes as $name => $value) {
$type = gettype($value);
$options[$name] = [
'type' => $type === 'NULL' ? 'string' : $type,
'required' => $value === null && $action->generator->isAttributeRequired($name),
'default' => $value,
'comment' => isset($hints[$name]) ? $this->formatHint($hints[$name]) : '',
];
}
return $options;
}
protected function formatHint($hint)
{
$hint = preg_replace('%<code>(.*?)</code>%', '\1', $hint);
$hint = preg_replace('/\s+/', ' ', $hint);
return wordwrap($hint);
}
}