Skip to content

Commit 4df57f2

Browse files
committed
修正代码生成器linux下文件路径问题
1 parent 8fd7b9e commit 4df57f2

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

src/exception/ErrorException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ErrorException extends HttpException
2525
'file already exists' => ['code' => 5004, 'msg' => '${filepath}文件已存在'],
2626
'file not exists' => ['code' => 5005, 'msg' => '${filepath}文件不存在'],
2727
'datatable already exists' => ['code' => 5004, 'msg' => '数据表${table}已存在'],
28+
'datatable not exists' => ['code' => 5004, 'msg' => '数据表${table}不存在'],
2829

2930
];
3031

src/generator/Index.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function checkFilesAndHandleParams($generatorItem,$params,$currentApps
101101
];
102102

103103
// 验证模板是否存在
104-
$templatePath = App::getRootPath() . $template;
104+
$templatePath =Utils::formatPath( App::getRootPath() . $template,"/");
105105
if (is_readable($templatePath) == false) {
106106
throw new ErrorException("template not found", 412, [
107107
'template' => $template
@@ -130,6 +130,7 @@ protected function checkFilesAndHandleParams($generatorItem,$params,$currentApps
130130
$createFiles[] = [
131131
'fileFullPath' => $fileFullPath,
132132
'template' => $template,
133+
'templatePath'=>$templatePath,
133134
'type' => $type
134135
];
135136

@@ -166,7 +167,7 @@ protected function checkModels($generatorItem,$tplParams){
166167
$path = $tableConfig['items'][$k]['path'];
167168

168169
// 验证模板是否存在
169-
$templatePath = App::getRootPath() . $template;
170+
$templatePath = Utils::formatPath(App::getRootPath() . $template,"/");
170171
if (is_readable($templatePath) == false) {
171172
throw new ErrorException("template not found", 412, [
172173
'template' => $template
@@ -223,7 +224,7 @@ protected function createFiles($createFiles,$tplParams){
223224

224225
if (!empty($createFiles) && count($createFiles)>0){
225226
foreach ($createFiles as $fileItem) {
226-
$html = (new ParseTemplate())->compile($fileItem['template'],$tplParams);
227+
$html = (new ParseTemplate())->compile($fileItem['templatePath'],$tplParams);
227228
if ($fileItem['type'] === "file"){
228229
// 路径为文件,则添加到该文件
229230
$pathFileContent = Utils::getFileContent($fileItem['fileFullPath']);
@@ -248,7 +249,7 @@ protected function createModels($createModels,$tplParams){
248249
$table = $item['table'];
249250
if (!empty($table['model_name'])){
250251
$tplParams['tables'][$k]['class_name'] =$table['model_name'];
251-
$html = (new ParseTemplate())->compile($item['template'],$tplParams);
252+
$html = (new ParseTemplate())->compile($item['templatePath'],$tplParams);
252253
Utils::createFile($item['fileFullPath'],$html);
253254
}
254255
if ($table['table_name']){
@@ -307,21 +308,27 @@ protected function createTable($table){
307308

308309
$tp_version = \think\facade\App::version();
309310
if (substr($tp_version, 0, 2) == '5.'){
310-
Db5::query($sql);
311+
Db5::startTrans();
312+
try {
313+
Db5::query($sql);
314+
Db5::commit();
315+
return true;
316+
} catch (\Exception $e) {
317+
Db5rollback();
318+
return $e->getMessage();
319+
}
311320
}else{
312-
Db::query($sql);
321+
Db::startTrans();
322+
try {
323+
Db::query($sql);
324+
Db::commit();
325+
return true;
326+
} catch (\Exception $e) {
327+
Db::rollback();
328+
return $e->getMessage();
329+
}
313330
}
314-
return true;
315-
//
316-
// Db::startTrans();
317-
// try {
318-
// Db::query($sql);
319-
// Db::commit();
320-
// return true;
321-
// } catch (\Exception $e) {
322-
// Db::rollback();
323-
// return $e->getMessage();
324-
// }
331+
325332

326333
}
327334
}

src/generator/ParseTemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ParseTemplate
1010

1111
public function compile($path,$params)
1212
{
13-
$filePath = App::getRootPath() . $path;
13+
$filePath = $path;
1414
$tplContent = Utils::getFileContent($filePath);
1515
$tplContent = $this->replaceForeach($tplContent,$params);
1616
$tplContent = $this->replaceParams($tplContent,$params);

src/parseApi/ParseModel.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace hg\apidoc\parseApi;
55

66
use Doctrine\Common\Annotations\Reader;
7+
use hg\apidoc\exception\ErrorException;
78
use think\Db as Db5;
89
use think\facade\Db;
910
use hg\apidoc\annotation\Field;
@@ -179,12 +180,20 @@ public function getTableDocument($model,array $propertys):array
179180

180181
$tp_version = \think\facade\App::version();
181182
if (substr($tp_version, 0, 2) == '5.'){
182-
$createSQL = Db5::query("show create table " . $model->getTable())[0]['Create Table'];
183+
$createSQL = Db5::query("show create table " . $model->getTable())[0];
183184
}else{
184-
$createSQL = Db::query("show create table " . $model->getTable())[0]['Create Table'];
185+
$createSQL = Db::query("show create table " . $model->getTable())[0];
185186
}
186-
// $createSQL = Db::query("show create table " . $model->getTable())[0]['Create Table'];
187-
preg_match_all("#[^KEY]`(.*?)` (.*?) (.*?),\n#", $createSQL, $matches);
187+
188+
$createTable = "";
189+
if (!empty($createSQL['Create Table'])){
190+
$createTable = $createSQL['Create Table'];
191+
}else if(!empty($createSQL['create table'])){
192+
$createTable = $createSQL['create table'];
193+
}else{
194+
throw new ErrorException("datatable not exists", 412, $createSQL);
195+
}
196+
preg_match_all("#[^KEY]`(.*?)` (.*?) (.*?),\n#", $createTable, $matches);
188197
$fields = $matches[1];
189198
$types = $matches[2];
190199
$contents = $matches[3];

0 commit comments

Comments
 (0)