Skip to content

Commit 69cefdd

Browse files
author
hector
committed
First commit
0 parents  commit 69cefdd

File tree

10 files changed

+1732
-0
lines changed

10 files changed

+1732
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
/.idea
3+
/.vscode
4+
composer.lock

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# thinkphp-generator
2+
3+
命令行自动生成数据表模型、校验器、控制器等
4+
5+
## 框架要求
6+
7+
ThinkPHP5.1+
8+
9+
## 安装
10+
11+
~~~ bash
12+
composer require hectorqin/thinkphp-generator
13+
~~~
14+
15+
## 配置
16+
17+
修改项目根目录下config/generator.php中对应的参数
18+
19+
## 使用
20+
21+
~~~ bash
22+
# 帮助
23+
$ php think generate --help
24+
Usage:
25+
generate [options]
26+
27+
Options:
28+
-c, --config[=CONFIG] 配置名称,默认为 generator [default: "generator"]
29+
-t, --table[=TABLE] 要生成的table,多个用,隔开, 默认为所有table
30+
-p, --tablePrefix[=TABLEPREFIX] table前缀,多个用,隔开
31+
-i, --ignoreFields[=IGNOREFIELDS] 忽略的字段,不生成搜索器
32+
-e, --except[=EXCEPT] 要排除的table,多个用,隔开
33+
--type[=TYPE] 要生成的类型,多个用,隔开,如 m,v,c,p,s,d
34+
m -- model, v -- validate, c -- controller, p -- postmanJson, s -- searchAttr, d -- model doc
35+
--templateDir[=TEMPLATEDIR] 自定义模板文件夹路径,必须有 model.tpl,
36+
controller.tpl, validate.tpl等文件,使用tp模板语法
37+
--mModule[=MMODULE] 模型模块名
38+
--vModule[=VMODULE] 校验器模块名
39+
--cModule[=CMODULE] 控制器模块名
40+
--mLayer[=MLAYER] 模型分层
41+
--vLayer[=VLAYER] 校验器分层
42+
--cLayer[=CLAYER] 控制器分层
43+
--mBase[=MBASE] 模型继承类,如 app\common\model\EventModel
44+
--vBase[=VBASE] 校验器继承
45+
--cBase[=CBASE] 控制器继承类
46+
--db[=DB] 数据库配置文件名
47+
--dryRun[=DRYRUN] 只执行,不保存 [default: false]
48+
-f, --force[=FORCE] 覆盖已存在文件 [default: false]
49+
--pName[=PNAME] PostMan 项目名称,默认使用 数据库名
50+
--pHost[=PHOST] PostMan API请求前缀,默认使用 api_prefix 环境变量
51+
-h, --help Display this help message
52+
-V, --version Display this console version
53+
-q, --quiet Do not output any message
54+
--ansi Force ANSI output
55+
--no-ansi Disable ANSI output
56+
-n, --no-interaction Do not ask any interactive question
57+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
58+
59+
# 生成 user 表模型、校验器、控制器
60+
php think generate -t user --type=m,v,c
61+
62+
# 生成数据库全部数据表的模型、校验器、控制器
63+
php think generate --type=m,v,c
64+
65+
# 不生成,预览操作
66+
php think generate --type=m,v,c -d
67+
68+
~~~
69+
70+
## License
71+
72+
Apache-2.0

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "hectorqin/thinkphp-generator",
3+
"description": "Generate model,controller,validate with parsing database.",
4+
"type": "think-extend",
5+
"version": "0.0.2",
6+
"license": "Apache-2.0",
7+
"authors": [
8+
{
9+
"name": "hector",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"think\\generator\\": "src"
16+
},
17+
"files": [
18+
"src/helper.php"
19+
]
20+
},
21+
"require": {
22+
"topthink/framework": "5.1.*"
23+
},
24+
"extra": {
25+
"think-config": {
26+
"generator": "src/config.php"
27+
}
28+
}
29+
}

src/command/AST.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace app\common\command;
3+
4+
use think\console\Command;
5+
use think\console\Input;
6+
use think\console\input\Option;
7+
use think\console\Output;
8+
use think\facade\Env;
9+
10+
class AST extends Command
11+
{
12+
protected function configure()
13+
{
14+
$this->setName('AST')
15+
->addOption('type', null, Option::VALUE_OPTIONAL, "要解析的类型,多个用,隔开,如 m,c\n m -- model, c -- controller")
16+
->addOption('scope', null, Option::VALUE_OPTIONAL, "要解析的范围,多个用,隔开,如 c,m\n c -- const, m -- method")
17+
->addOption('dryRun', null, Option::VALUE_OPTIONAL, "只执行,不保存")
18+
->setDescription('Print AST of file');
19+
}
20+
21+
protected function execute(Input $input, Output $output)
22+
{
23+
$typeList = ['m', 'c'];
24+
$scopeList = ['c', 'm'];
25+
26+
$dryRun = false;
27+
if ($input->hasOption('dryRun')) {
28+
$dryRun = $input->getOption('dryRun');
29+
}
30+
31+
if ($input->hasOption('type')) {
32+
$typeList = explode(',', $input->getOption('type'));
33+
}
34+
35+
if ($input->hasOption('scope')) {
36+
$scopeList = explode(',', $input->getOption('scope'));
37+
}
38+
39+
$date = date("Ymd_Hi");
40+
if (in_array('m', $typeList)) {
41+
$this->generateTypeAST("./application/common/model/**.php", $scopeList, $dryRun ? false : Env::get('app_path') . "modelAST-{$date}.json");
42+
}
43+
if (in_array('c', $typeList)) {
44+
$this->generateTypeAST("./application/index/controller/**.php", $scopeList, $dryRun ? false : Env::get('app_path') . "controllerAST-{$date}.json");
45+
}
46+
}
47+
48+
protected function generateTypeAST($globPattern, $scopeList, $savePath)
49+
{
50+
$ASTData = [];
51+
$generateConstant = in_array('c', $scopeList);
52+
$generateMethod = in_array('m', $scopeList);
53+
foreach (glob($globPattern) as $file) {
54+
$className = str_replace(['./application', '.php', '/'], ['/app', '', '\\'], $file);
55+
$class = new \ReflectionClass($className);
56+
if ($generateConstant) {
57+
$constants = $class->getReflectionConstants();
58+
59+
foreach ($constants as $constant) {
60+
if ($constant->class != $class->name) {
61+
continue;
62+
}
63+
if (!in_array($constant->name, ['EVENT_INSERT', 'EVENT_UPDATE', 'EVENT_DELETE'])) {
64+
$ASTData[$class->name]['constant'][] = [
65+
'name' => $constant->name,
66+
'value' => $constant->getValue(),
67+
'doc' => str_replace(["/**\n", "* ", '*/'], '', $constant->getDocComment()),
68+
];
69+
}
70+
}
71+
}
72+
73+
if ($generateMethod) {
74+
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
75+
76+
foreach ($methods as $method) {
77+
if ($method->class != $class->name) {
78+
continue;
79+
}
80+
$args = [];
81+
foreach ($method->getParameters() as $arg) {
82+
$args[] = [
83+
'name' => $arg->name,
84+
'default' => $arg->isDefaultValueAvailable() ? $arg->getDefaultValue() : '',
85+
];
86+
}
87+
$ASTData[$class->name]['method'][] = [
88+
'name' => $method->name,
89+
'__toString' => $method->__toString(),
90+
'args' => $args,
91+
'doc' => str_replace(["/**\n", "* ", '*/'], '', $method->getDocComment()),
92+
];
93+
}
94+
}
95+
}
96+
// dump($ASTData);
97+
if ($savePath) {
98+
file_put_contents($savePath, json_encode($ASTData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
99+
} else {
100+
dump($ASTData);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)