-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathphalapi_build_data_model.php
More file actions
144 lines (116 loc) · 4.02 KB
/
phalapi_build_data_model.php
File metadata and controls
144 lines (116 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* 根据数据库表,生成DataModel代码
* @author dogstar 2020-06-10
*/
if ($argc < 2) {
echo "\n";
echo colorfulString("Usage:\n", 'WARNING');
echo " $argv[0] <dbs_config> [table] [project=app]\n";
echo "\n";
echo colorfulString("Options:\n", 'WARNING');
echo colorfulString(' dbs_config', 'NOTE'), " Require. Database config file name, such as dbs.php\n";
echo colorfulString(' table', 'NOTE'), " NOT Require. Table name, default is ALL tables\n";
echo colorfulString(' project', 'NOTE'), " NOT require. Project name to save PHP code, default is app\n";
echo "\n";
echo colorfulString("Demo:\n", 'WARNING');
echo " $argv[0] dbs.php \n";
echo "\n";
exit(1);
}
require_once dirname(__FILE__) . '/../public/init.php';
$configFile = dirname(__FILE__) . '/../config/' . $argv[1];
if (!file_exists($configFile)) {
echo colorfulString($configFile . ' NOT exists!', 'FAILURE');
exit(1);
}
$table = !empty($argv[2]) ? trim($argv[2]) : '';
$project = !empty($argv[3]) ? trim($argv[3]) : 'app';
$modelFolder =dirname(__FILE__) . '/../src/'.$project.'/Model';
@mkdir($modelFolder, 755, true);
$dbConfig = include($configFile);
$prefix = $dbConfig['tables']['__default__']['prefix'];
$di->notorm_tmp = new PhalApi\Database\NotORMDatabase($dbConfig, true);
$dbRes = $di->notorm_tmp->demo->queryAll('SHOW TABLES;');
foreach ($dbRes as $it) {
$it = array_values($it);
$curTable = $it[0];
if (!empty($table) && $table != $curTable) {
// 指定表,不匹配
continue;
}
echo colorfulString("开始处理表:{$curTable} ...\n");
$curTableWithoutPrefix = $curTable;
if (substr($curTableWithoutPrefix, 0, strlen($prefix)) == $prefix) {
$curTableWithoutPrefix = substr($curTableWithoutPrefix, strlen($prefix));
}
$className = tableName2ClassName($curTableWithoutPrefix);
$classFilePath = $modelFolder . '/' . $className . '.php';
if (file_exists($classFilePath)) {
echo colorfulString("类文件已存在:{$classFilePath} ...\n", 'WARNING');
continue;
}
$code = createDataModelPHPCode($project, $className, $curTableWithoutPrefix);
file_put_contents($classFilePath, $code);
echo colorfulString("Model代码已生成到:{$classFilePath} ...\n", 'SUCCESS');
}
$basePath = $modelFolder . '/Base.php';
if (file_exists($basePath)) {
echo colorfulString("类文件已存在:{$basePath} ...\n", 'WARNING');
} else {
file_put_contents($modelFolder . '/Base.php', createBaseClassCode($project));
echo colorfulString("Model基类代码已生成到:{$basePath} ...\n", 'SUCCESS');
}
function createBaseClassCode($project) {
$project = ucfirst($project);
return <<<EOT
<?php
namespace {$project}\Model;
/**
* 连接其他数据库
* - 当需要连接和操作其他数据库时,请在Model继续此基类,以便切换数据库
* - 或在此基类进行通用操作的封装
*/
class Base extends \PhalApi\Model\DataModel {
/**
* 切换数据库
* @return \PhalApi\Database\NotORMDatabase
*/
protected function getNotORM() {
return \PhalApi\DI()->notorm;
}
}
EOT;
}
function createDataModelPHPCode($project, $className, $curTableWithoutPrefix) {
$project = ucfirst($project);
return <<<EOT
<?php
namespace {$project}\Model;
class {$className} extends Base {
public function getTableName(\$id) {
return '{$curTableWithoutPrefix}';
}
}
EOT;
}
function tableName2ClassName($curTableWithoutPrefix) {
$arr = explode('_', $curTableWithoutPrefix);
$str = '';
foreach ($arr as $it) {
$str .= ucfirst($it);
}
return $str;
}
function colorfulString($text, $type = NULL) {
$colors = array(
'WARNING' => '1;33',
'NOTE' => '1;36',
'SUCCESS' => '1;32',
'FAILURE' => '1;35',
);
if (empty($type) || !isset($colors[$type])){
return $text;
}
return "\033[" . $colors[$type] . "m" . $text . "\033[0m";
}