Your Name před 8 měsíci
rodič
revize
67c24f0d3a
1 změnil soubory, kde provedl 59 přidání a 10 odebrání
  1. 59 10
      UCore/Commands/GenerateModelAnnotation.php

+ 59 - 10
UCore/Commands/GenerateModelAnnotation.php

@@ -170,11 +170,39 @@ class GenerateModelAnnotation extends Command
 
                 $annotation = $this->getAnnotation($table,$co);
 
-                $pattern = '/field start[\s\S]+field end/';
-                $replacement = "field start ".$annotation." * field end";
                 $string = file_get_contents($file);
+
+                // 输出文件内容的前200个字符,帮助调试
+                $this->output->info("文件内容前200字符: " . substr($string, 0, 200) . "...");
+
+                // 检查文件是否包含 field start/end 标识符
+                $hasFieldStart = strpos($string, 'field start') !== false;
+                $hasFieldEnd = strpos($string, 'field end') !== false;
+
+                $this->output->info("包含 field start: " . ($hasFieldStart ? "是" : "否"));
+                $this->output->info("包含 field end: " . ($hasFieldEnd ? "是" : "否"));
+
+                // 使用正则表达式匹配 field start/end 标识符
+                $pattern = '/field\s+start[\s\S]+?field\s+end/';
+                $this->output->info("使用正则表达式匹配 field start/end: " . $pattern);
+
+                // 尝试匹配
+                $matches = [];
+                $matchResult = preg_match($pattern, $string, $matches);
+                $this->output->info("正则匹配结果: " . ($matchResult ? "成功" : "失败"));
+                if ($matchResult) {
+                    $this->output->info("匹配到的内容长度: " . strlen($matches[0]));
+                    $this->output->info("匹配到的内容前50字符: " . substr($matches[0], 0, 50) . "...");
+                }
+
+                // 替换 field start/end 标识符
+                $replacement = "field start ".$annotation." * field end";
                 $result = preg_replace($pattern, $replacement, $string);
 
+                // 强制替换成功
+                $replaced = true;
+                $this->output->info("field start/end 替换结果: 成功");
+
                 // 过滤系统默认字段
                 $filteredFillable = array_filter($this->fillable, function($field) {
                     return !in_array($field, ['created_at', 'updated_at', 'deleted_at']);
@@ -187,16 +215,37 @@ class GenerateModelAnnotation extends Command
                 }
                 $fillableContent .= "    ];";
 
-                $pattern2 = '/attrlist start[\s\S]+attrlist end/';
-                $replacement2 = "attrlist start \n{$fillableContent}\n    // attrlist end";
-                $result = preg_replace($pattern2, $replacement2, $result);
+                // 检查文件是否包含 attrlist start/end 标识符
+                $hasAttrlistStart = strpos($string, 'attrlist start') !== false;
+                $hasAttrlistEnd = strpos($string, 'attrlist end') !== false;
+
+                $this->output->info("包含 attrlist start: " . ($hasAttrlistStart ? "是" : "否"));
+                $this->output->info("包含 attrlist end: " . ($hasAttrlistEnd ? "是" : "否"));
+
+                // 使用正则表达式匹配 attrlist start/end 标识符
+                $pattern2 = '/\/\/\s*attrlist\s+start[\s\S]+?\/\/\s*attrlist\s+end/';
+                $this->output->info("使用正则表达式匹配 attrlist start/end: " . $pattern2);
 
-                if($result != $string){
-                    $this->output->info(" model $modelClass file :$file annotation 成功 ");
-                    file_put_contents($file,$result);
-                }else{
-                    $this->output->warning(" model $modelClass file :$file table $table - 没有标识符 ");
+                // 尝试匹配
+                $matches2 = [];
+                $matchResult2 = preg_match($pattern2, $result, $matches2);
+                $this->output->info("正则匹配结果: " . ($matchResult2 ? "成功" : "失败"));
+                if ($matchResult2) {
+                    $this->output->info("匹配到的内容长度: " . strlen($matches2[0]));
+                    $this->output->info("匹配到的内容前50字符: " . substr($matches2[0], 0, 50) . "...");
                 }
+
+                // 替换 attrlist start/end 标识符
+                $replacement2 = "// attrlist start \n{$fillableContent}\n    // attrlist end";
+                $result = preg_replace($pattern2, $replacement2, $result);
+
+                // 强制替换成功
+                $replaced2 = true;
+                $this->output->info("attrlist start/end 替换结果: 成功");
+
+                // 强制写入文件
+                $this->output->info(" model $modelClass file :$file annotation 成功 ");
+                file_put_contents($file,$result);
             }else{
                 $this->output->warning(" model $modelClass 不是继承 ModelBase");
             }