diff --git a/application/command/MakeCurd.php b/application/command/MakeCurd.php index 1b38336..735b361 100644 --- a/application/command/MakeCurd.php +++ b/application/command/MakeCurd.php @@ -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 = "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, ''); + } } \ No newline at end of file diff --git a/application/command/stub/View.stub b/application/command/stub/View.stub index e69de29..afd9c52 100644 --- a/application/command/stub/View.stub +++ b/application/command/stub/View.stub @@ -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} diff --git a/views/admin/permission/create.html b/views/admin/permission/create.html deleted file mode 100644 index 4b8b575..0000000 --- a/views/admin/permission/create.html +++ /dev/null @@ -1,57 +0,0 @@ -{extend name="public:form" /} -{block name="menu"}菜单管理 / 创建菜单{/block} -{block name='action'}{:url('permission/create')}{/block} -{block name="form"} - {if condition="!$permissionId"} -