如何读取PHP项目中多处的defineCustomTemplate函数并生成XML文件?
批量提取项目中defineCustomTemplate函数数据并生成XML文件
解决方案思路
- 递归扫描项目目录下的所有PHP文件,筛选出包含
defineCustomTemplate函数的文件 - 对每个目标文件,解析函数内的配置数据(
setNmfile/setNmoperation/setNmimport的值,以及customTemplate数组的字段) - 将提取到的所有数据统一生成标准XML文件
实现脚本
以下是适配你提供的函数结构的可直接运行PHP脚本:
<?php // 项目根目录路径,根据实际情况修改 $projectRoot = '/path/to/your/project'; // 最终生成的XML文件路径 $outputXmlPath = './custom_templates.xml'; // 存储所有提取到的模板数据 $allTemplates = []; // 递归扫描目录下的PHP文件 function scanPhpFiles($dir, &$allTemplates) { $files = scandir($dir); foreach ($files as $file) { if ($file === '.' || $file === '..') continue; $fullPath = $dir . DIRECTORY_SEPARATOR . $file; if (is_dir($fullPath)) { scanPhpFiles($fullPath, $allTemplates); continue; } if (pathinfo($fullPath, PATHINFO_EXTENSION) !== 'php') continue; // 读取文件内容 $content = file_get_contents($fullPath); // 匹配defineCustomTemplate函数 if (preg_match('/public function defineCustomTemplate\(\)\s*\{\s*(.*?)\s*return \$customTemplate;\s*\}/s', $content, $matches)) { $functionBody = $matches[1]; $templateData = [ 'file' => $fullPath, 'nmfile' => null, 'nmoperation' => null, 'nmimport' => null, 'fields' => [] ]; // 提取setNmfile的值 if (preg_match('/\$this->setNmfile\((\d+)\);/', $functionBody, $m)) { $templateData['nmfile'] = $m[1]; } // 提取setNmoperation的值 if (preg_match('/\$this->setNmoperation\("([^"]+)"\);/', $functionBody, $m)) { $templateData['nmoperation'] = $m[1]; } // 提取setNmimport的值 if (preg_match('/\$this->setNmimport\((\d+)\);/', $functionBody, $m)) { $templateData['nmimport'] = $m[1]; } // 提取customTemplate数组中的字段 preg_match_all('/array\((.*?)\)/s', $functionBody, $fieldMatches); foreach ($fieldMatches[1] as $fieldStr) { $field = []; // 解析每个字段的键值对 preg_match_all('/\'([^\']+)\' => (.*?)(?:,|$)/s', $fieldStr, $kvMatches); for ($i=0; $i<count($kvMatches[1]); $i++) { $key = $kvMatches[1][$i]; $value = trim($kvMatches[2][$i], '\'" '); $field[$key] = $value; } if (!empty($field)) { $templateData['fields'][] = $field; } } $allTemplates[] = $templateData; } } } // 执行扫描 scanPhpFiles($projectRoot, $allTemplates); // 生成XML $xml = new SimpleXMLElement('<custom_templates/>'); foreach ($allTemplates as $template) { $templateNode = $xml->addChild('template'); $templateNode->addChild('source_file', htmlspecialchars($template['file'])); if ($template['nmfile']) $templateNode->addChild('nmfile', $template['nmfile']); if ($template['nmoperation']) $templateNode->addChild('nmoperation', htmlspecialchars($template['nmoperation'])); if ($template['nmimport']) $templateNode->addChild('nmimport', $template['nmimport']); $fieldsNode = $templateNode->addChild('fields'); foreach ($template['fields'] as $field) { $fieldNode = $fieldsNode->addChild('field'); foreach ($field as $key => $value) { $fieldNode->addChild($key, htmlspecialchars($value)); } } } // 保存XML文件 $xml->asXML($outputXmlPath); echo "XML文件已生成:{$outputXmlPath}\n"; echo "共提取到 " . count($allTemplates) . " 个自定义模板\n"; ?>
使用说明
- 修改脚本中的
$projectRoot为你的项目根目录绝对路径 - 修改
$outputXmlPath为你期望保存XML的路径 - 在命令行运行该脚本:
php extract_templates.php - 生成的XML会包含每个模板的来源文件、配置参数以及所有字段的详细信息
注意事项
- 脚本基于你提供的函数结构编写,如果函数内代码格式有较大变动(比如换行、注释位置不同),可能需要调整正则表达式
- 若项目中存在大量PHP文件,扫描过程可能需要一定时间,请耐心等待
- 建议先在测试环境运行脚本,确认提取数据准确后再在正式环境使用
内容的提问来源于stack exchange,提问作者Maik Costa
相关产品推荐
相关产品推荐

