修改快速创建命令
This commit is contained in:
parent
3f7aad2d67
commit
e43a484d11
@ -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,13 +71,31 @@ 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);
|
||||
}
|
||||
|
||||
$stub = explode('||', file_get_contents($viewStub));
|
||||
|
||||
foreach ($this->views as $view) {
|
||||
file_put_contents($viewPath . DIRECTORY_SEPARATOR . $view .'.html', file_get_contents($viewStub));
|
||||
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}
|
@ -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"}
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">选择父级菜单</label>
|
||||
<div class="col-sm-4">
|
||||
<select class="form-control m-b" name="pid">
|
||||
<option value="0">--请选择父级菜单--</option>
|
||||
{foreach $permissions as $permission}
|
||||
<option value="{$permission->id}">{:str_repeat(' ', $permission->level)} {$permission->name}</option>
|
||||
{/foreach}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
{else/}
|
||||
<input type="hidden" name="pid" value="{$permissionId}">
|
||||
{/if}
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">菜单名称</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="name" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">菜单图标</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="icon" >
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">模块</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="module" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">控制器</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="controller" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">方法</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="action" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">展示</label>
|
||||
<div class="col-sm-4 radio i-checks">
|
||||
<label><input type="radio" checked value="1" name="is_show"> <i></i>是</label>
|
||||
<label><input type="radio" value="2" name="is_show"><i></i>否</label>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
@ -1,54 +0,0 @@
|
||||
{extend name="public:form" /}
|
||||
{block name="menu"}菜单管理 / 创建菜单{/block}
|
||||
{block name='action'}{:url('permission/edit')}{/block}
|
||||
{block name="form"}
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">选择父级菜单</label>
|
||||
<div class="col-sm-4">
|
||||
<select class="form-control m-b" name="pid">
|
||||
<option value="0">--请选择父级菜单--</option>
|
||||
{foreach $permissions as $_permission}
|
||||
<option value="{$_permission->id}" {if condition="$permission->pid eq $_permission->id"}selected{/if}>{:str_repeat(' ', $_permission->level)} {$_permission->name}</option>
|
||||
{/foreach}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">菜单名称</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="name" required value="{$permission->name}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">菜单图标</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="icon" value="{$permission->icon}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">模块</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="module" required value="{$permission->module}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">控制器</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="controller" required value="{$permission->controller}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">方法</label>
|
||||
<div class="col-sm-4">
|
||||
<input type="text" class="form-control" name="action" required value="{$permission->action}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">展示</label>
|
||||
<div class="col-sm-4 radio i-checks">
|
||||
<label><input type="radio" value="1" name="is_show" {if condition="$permission->is_show eq 1"}checked{/if}> <i></i>是</label>
|
||||
<label><input type="radio" value="2" name="is_show" {if condition="$permission->is_show eq 2"}checked{/if}><i></i>否</label>
|
||||
</div>
|
||||
</div>
|
||||
<input type="hidden" name="id" value="{$permission->id}">
|
||||
{/block}
|
@ -1,37 +0,0 @@
|
||||
{extend name="public:base" /}
|
||||
{block name="menu"}菜单管理{/block}
|
||||
{block name="search"}
|
||||
{:createButton(url('permission/create'), '创建菜单')}
|
||||
{/block}
|
||||
{block name="table-head"}
|
||||
<tr>
|
||||
<th>菜单名称</th>
|
||||
<th>模块名称</th>
|
||||
<th>路由</th>
|
||||
<th>展示</th>
|
||||
<th>创建时间</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
{/block}
|
||||
{block name="table-body"}
|
||||
{if condition="!$permissions->count()"}
|
||||
<tr>
|
||||
<td colspan="7" class="text-center">没有数据</td>
|
||||
</tr>
|
||||
{else/}
|
||||
{foreach $permissions as $permission}
|
||||
<tr>
|
||||
<td>{:str_repeat(' ', $permission->level)}{$permission->name}</td>
|
||||
<td>{$permission->module}</td>
|
||||
<td>{$permission->controller}@{$permission->action}</td>
|
||||
<td>{$permission->is_show == 1 ? '是' : '否'}</td>
|
||||
<td>{$permission->created_at}</td>
|
||||
<td>
|
||||
{:editButton(url('permission/edit', ['id' => $permission->id ]))}
|
||||
{:deleteButton(url('permission/delete'), $permission->id)}
|
||||
{:createButton(url('permission/create', ['id' => $permission->id ]), '创建子菜单', false)}
|
||||
</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
{/if}
|
||||
{/block}
|
Loading…
x
Reference in New Issue
Block a user