Commit 5bb6cc3c authored by 梁俊杰's avatar 梁俊杰

新增 mm命令自动生成功能

Description:
  根据数据表生成model文件和迁移文件和控制器
Usage:
  mm [options] [--] [<name>]
Arguments:
  name                   数据库表名
Options:
  -a, --all              生成所有文件
  -m, --model            生成model文件
  -c, --controller       生成controller文件
  -i, --migrate          生成迁移文件
  -r, --author[=AUTHOR]  生成文件的作者
  -f, --force            文件存在是否覆盖
  -d, --database         全数据库索引自动生成全站文件
parent 5402f3b8
This diff is collapsed.
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
namespace App\Controller;
use App\Repository\Interfaces\%ModelClass%Repository;
use Hyperf\Di\Annotation\Inject;
use Meibuyu\Micro\Annotation\AutoPerm;
use Meibuyu\Micro\Annotation\Perm;
/**
* @AutoPerm()
* Class %ModelClass%Controller
* @package App\Controller
*/
class %ModelClass%Controller extends AbstractController
{
/**
* @Inject()
* @var %ModelClass%Repository
*/
private $repository;
/**
* 获取列表数据
* @return mixed
*/
public function index()
{
$list = $this->repository->list();
return success('获取成功', $list);
}
/**
* 获取列表数据
* @Perm("index")
* @param $id id编号
* @return mixed
*/
public function show($id)
{
$data = $this->repository->show($id);
return success('获取成功', $data);
}
/**
* 添加记录
* @return mixed
*/
public function create()
{
$data = $this->repository->create($this->request->all());
return success('创建成功', $data);
}
/**
* 更新数据
* @return mixed
*/
public function update($id)
{
$data = $this->repository->update($this->request->all(), $id);
return success('更新成功', $data);
}
/**
* 删除单条数据
* @return mixed
*/
public function delete($id)
{
$deleted = $this->repository->delete($id);
return success('删除成功', $deleted);
}
%rs%
}
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class %ClassName% extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::disableForeignKeyConstraints();
Schema::create('%tablename%', function (Blueprint $table) {
%attributes%
});
%tableComment%
Schema::enableForeignKeyConstraints();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('%tablename%');
}
}
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
declare (strict_types=1);
namespace App\Model;
%namespace%
/**
* 模型类 %ClassName%
* @package App\Models
%properties%
*/
class %ClassName% extends Model
{
%SoftDelete%
/**
* 可写入数据的字段.
* @var array
*/
protected $fillable = [
%fillAble%
];
/**
* 字段转换数据类型的字段
* @var array
*/
protected $casts = [
%casts%
];
/**
* 是否使用时间戳管理
* @var bool
*/
public $timestamps = %timestamps%;
%relations%
}
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
declare(strict_types=1);
namespace App\Repository\Eloquent;
use App\Model\%ModelClass%;
use App\Repository\Interfaces\%ModelClass%Repository;
use App\Validators\%ModelClass%Validator;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Repository\Eloquent\BaseRepository;
/**
* %ModelClass%RepositoryEloquent 类.
*
* @package namespace App\Repository\Eloquent;
*/
class %ModelClass%RepositoryEloquent extends BaseRepository implements %ModelClass%Repository
{
/**
* %ModelClass% 模型
* @return %ModelClass%
*/
public function model(): %ModelClass%
{
return %ModelClass%::class;
}
/**
* 数据校验器
* @return %ModelClass%Validator
*/
public function validator(): %ModelClass%Validator
{
return %ModelClass%Validator::class;
}
/**
* 获取数据列表
* @return array
*/
public function list(): array
{
$pageSize = (int)$this->request->input('page_size', env("DEFAULT_PAGE_SIZE",10));
%list%;
}
/**
* 获取单条数据
* @param $id
* @return array
*/
public function show($id): array
{
%show%;
}
/**
* 添加记录
* @param array $attributes
* @return bool
*/
public function create(array $attributes): bool
{
Db::transaction(function () use ($attributes) {
%create%
});
return true;
}
/**
* 更新数据
* @param array $attributes
* @param $id
* @return bool
*/
public function update(array $attributes, $id): bool
{
Db::transaction(function () use ($attributes, $id) {
%update%
});
return true;
}
/**
* 删除单条数据
* @param $id
* @return bool
* @throws HttpResponseException
*/
public function delete($id): bool
{
//已用外键做级联删除 和 删除验证,不需要做逻辑验证
return $this->find($id)->delete();
}
%rs%
}
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
declare(strict_types=1);
namespace App\Repository\Interfaces;
use Meibuyu\Micro\Repository\Contracts\RepositoryInterface;
/**
* %ClassName% 接口类.
*
* @package namespace App\Repository\Interfaces;
*/
interface %ClassName% extends RepositoryInterface
{
}
<?php
/**
* Created by PhpStorm.
* User: %user%
* Date: %date%
* Time: %time%
* Description:
*/
declare(strict_types=1);
namespace App\Validators;
use Meibuyu\Micro\Validator\Contracts\ValidatorInterface;
use Meibuyu\Micro\Validator\HyperfValidator;
class %ModelClass%Validator extends HyperfValidator
{
protected $rules = [
ValidatorInterface::RULE_CREATE => [
%rules%
],
ValidatorInterface::RULE_UPDATE => [
%rules%
],
];
protected $attributes = [
%attributes%
];
protected $messages = [
%messages%
];
}
......@@ -27,6 +27,7 @@ class ConfigProvider
'commands' => [
\Meibuyu\Micro\Command\RepositoryCommand::class,
\Meibuyu\Micro\Command\ValidatorCommand::class,
\Meibuyu\Micro\Command\MakeModelCommand::class,
],
'annotations' => [
'scan' => [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment