修改快速创建命令
This commit is contained in:
@@ -6,6 +6,7 @@ use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\DB;
|
||||
|
||||
class MakeCurd extends Command
|
||||
{
|
||||
@@ -18,7 +19,7 @@ class MakeCurd extends Command
|
||||
{
|
||||
parent::__construct();
|
||||
$this->appPath = env('app_path');
|
||||
$this->stubPath = $this->appPath . 'commands' . DIRECTORY_SEPARATOR . 'stub' .DIRECTORY_SEPARATOR;
|
||||
$this->stubPath = $this->appPath . 'command' . DIRECTORY_SEPARATOR . 'stub' .DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
@@ -70,14 +71,32 @@ class MakeCurd extends Command
|
||||
// 创建模型文件
|
||||
public function makeModel($modelName, $moduleName)
|
||||
{
|
||||
$modelStub = $this->stubPath . 'Model.stub';
|
||||
$modelPath = $this->appPath . DIRECTORY_SEPARATOR . 'models';
|
||||
$modelPath = $this->appPath . DIRECTORY_SEPARATOR . 'model';
|
||||
if (!is_dir($modelPath)) {
|
||||
mkdir($modelPath, 0777, true);
|
||||
}
|
||||
$modelStub = str_replace('$model', ucfirst($modelName), file_get_contents($modelStub));
|
||||
return file_put_contents($modelPath . DIRECTORY_SEPARATOR . $modelName . 'Model.php', $modelStub);
|
||||
$modelContents = "<?php \r\n \r\n";
|
||||
$modelContents .= "namespace app\model;\r\n \r\n";
|
||||
$modelContents .= 'class $modelModel extends BaseModel';
|
||||
$modelContents .= "\r\n { \r\n \t";
|
||||
$modelContents .= 'protected $table = \'' . config('database.prefix') . '$_table\';';
|
||||
$modelContents = $this->writeField($modelContents, $modelName);
|
||||
$modelContents = str_replace('$model', ucfirst($modelName), $modelContents);
|
||||
$modelContents = str_replace('$_table', $this->unCamelize($modelName), $modelContents);
|
||||
$modelContents .= "\r\n }";
|
||||
|
||||
return file_put_contents($modelPath . DIRECTORY_SEPARATOR . $modelName . 'Model.php', $modelContents);
|
||||
}
|
||||
|
||||
private function writeField($modelContents, $modelName)
|
||||
{
|
||||
$info = Db::query('show full columns from ' . config('database.prefix') . $this->unCamelize($modelName));
|
||||
foreach ($info as $value) {
|
||||
$modelContents .= sprintf("\r\n %s \t protected $%s = '%s'; \r\n", $this->fieldComment($value['Comment']), $this->combine($value['Field']), $value['Field']);
|
||||
}
|
||||
|
||||
return $modelContents;
|
||||
}
|
||||
// 创建模板
|
||||
public function makeView($controllerName, $moduleName)
|
||||
{
|
||||
@@ -86,8 +105,50 @@ class MakeCurd extends Command
|
||||
if (!is_dir($viewPath)) {
|
||||
mkdir($viewPath, 0777, true);
|
||||
}
|
||||
foreach ($this->views as $view) {
|
||||
file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view .'.html', file_get_contents($viewStub));
|
||||
|
||||
$stub = explode('||', file_get_contents($viewStub));
|
||||
|
||||
foreach ($this->views as $view) {
|
||||
if ($view == 'index') {
|
||||
file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view .'.html', trim($stub[0]));
|
||||
} else {
|
||||
file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view .'.html', trim($stub[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符注释
|
||||
*
|
||||
* @time at 2019年01月08日
|
||||
* @param $comment
|
||||
* @return string
|
||||
*/
|
||||
private function fieldComment($comment)
|
||||
{
|
||||
return sprintf("\t /** \r\n \t * @var string \r\n \t * @desc %s \r\n \t */ \r\n", $comment);
|
||||
}
|
||||
/**
|
||||
* 驼峰分割
|
||||
*
|
||||
* @time at 2019年01月02日
|
||||
* @param string $camelCaps
|
||||
* @param string $separator
|
||||
* @return string
|
||||
*/
|
||||
private function unCamelize(string $string, string $separator = '_')
|
||||
{
|
||||
return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', $separator . '$1', $string));
|
||||
}
|
||||
|
||||
private function combine(string $string)
|
||||
{
|
||||
$s = explode('_', $string);
|
||||
array_walk($s, function (&$value, $key) {
|
||||
if ($key) {
|
||||
$value = ucfirst($value);
|
||||
}
|
||||
});
|
||||
return implode($s, '');
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
{extend name="public:base" /}
|
||||
{block name="menu"}{/block}
|
||||
{block name="search"}{/block}
|
||||
{block name="button-create"}{/block}
|
||||
{block name="table-head"}{/block}
|
||||
{block name="table-body"}{/block}
|
||||
{block name="paginate"}{/block}
|
||||
||
|
||||
{extend name="public:form" /}
|
||||
{block name="menu"}{/block}
|
||||
{block name='action'}{/block}
|
||||
{block name="form"}{/block}
|
||||
|
Reference in New Issue
Block a user