- 重构LanguageUtils为LanguageService,实现ILanguageService接口 - 移除自定义验证管道和装饰器,使用标准NestJS验证 - 集成框架ValidatorService进行业务验证 - 简化目录结构,移除不必要的子目录 - 支持模块化语言包加载(common、user、order等) - 统一API响应格式(code、msg、data、timestamp) - 添加ValidationExceptionFilter处理多语言验证错误 - 完善多语言示例和文档
208 lines
8.7 KiB
JavaScript
208 lines
8.7 KiB
JavaScript
/**
|
|
* PHP到TypeScript转换规则数据库
|
|
* 为AI自动生成打下基石
|
|
*/
|
|
|
|
class ConversionRulesDatabase {
|
|
constructor() {
|
|
this.rules = {
|
|
// 基础语法转换
|
|
syntax: {
|
|
variables: [
|
|
{ pattern: /\$this->([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: 'this.$1', description: 'PHP对象属性访问' },
|
|
{ pattern: /\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: '$1', description: 'PHP变量声明' },
|
|
{ pattern: /self::\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: 'self.$1', description: 'PHP静态变量访问' },
|
|
{ pattern: /static::\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: 'static.$1', description: 'PHP静态变量访问' }
|
|
],
|
|
|
|
operators: [
|
|
{ pattern: /\?\?/g, replacement: '||', description: 'PHP空值合并操作符' },
|
|
{ pattern: /->/g, replacement: '.', description: 'PHP对象访问操作符' },
|
|
{ pattern: /::/g, replacement: '.', description: 'PHP静态访问操作符' },
|
|
{ pattern: /===/g, replacement: '===', description: '严格相等比较' },
|
|
{ pattern: /====/g, replacement: '===', description: '修复重复等号' }
|
|
],
|
|
|
|
functions: [
|
|
{ pattern: /empty\s*\(\s*([^)]+)\s*\)/g, replacement: '!$1', description: 'PHP empty函数' },
|
|
{ pattern: /isset\s*\(\s*([^)]+)\s*\)/g, replacement: '$1 !== undefined', description: 'PHP isset函数' },
|
|
{ pattern: /is_null\s*\(\s*([^)]+)\s*\)/g, replacement: '$1 === null', description: 'PHP is_null函数' },
|
|
{ pattern: /is_array\s*\(\s*([^)]+)\s*\)/g, replacement: 'Array.isArray($1)', description: 'PHP is_array函数' },
|
|
{ pattern: /is_string\s*\(\s*([^)]+)\s*\)/g, replacement: 'typeof $1 === "string"', description: 'PHP is_string函数' },
|
|
{ pattern: /is_numeric\s*\(\s*([^)]+)\s*\)/g, replacement: '!isNaN($1)', description: 'PHP is_numeric函数' },
|
|
{ pattern: /env\(([^)]+)\)/g, replacement: 'process.env.$1', description: 'PHP env函数' }
|
|
]
|
|
},
|
|
|
|
// 类型转换
|
|
types: {
|
|
parameters: [
|
|
{ pattern: /string\s+\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: '$1: string', description: 'PHP字符串参数' },
|
|
{ pattern: /int\s+\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: '$1: number', description: 'PHP整数参数' },
|
|
{ pattern: /array\s+\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: '$1: any[]', description: 'PHP数组参数' },
|
|
{ pattern: /bool\s+\$([a-zA-Z_][a-zA-Z0-9_]*)/g, replacement: '$1: boolean', description: 'PHP布尔参数' }
|
|
],
|
|
|
|
declarations: [
|
|
{ pattern: /array\s+/g, replacement: '', description: 'PHP数组类型声明' },
|
|
{ pattern: /:\s*array/g, replacement: ': any[]', description: 'PHP数组返回类型' }
|
|
]
|
|
},
|
|
|
|
// 方法转换
|
|
methods: {
|
|
declarations: [
|
|
{ pattern: /public\s+function\s+/g, replacement: 'async ', description: 'PHP公共方法' },
|
|
{ pattern: /private\s+function\s+/g, replacement: 'private async ', description: 'PHP私有方法' },
|
|
{ pattern: /protected\s+function\s+/g, replacement: 'protected async ', description: 'PHP受保护方法' }
|
|
],
|
|
|
|
constructors: [
|
|
{ pattern: /parent::__construct\(\)/g, replacement: 'super()', description: 'PHP父类构造函数调用' },
|
|
{ pattern: /new\s+static\s*\(([^)]*)\)/g, replacement: 'new this.constructor($1)', description: 'PHP静态实例化' }
|
|
]
|
|
},
|
|
|
|
// 数组和对象转换
|
|
collections: {
|
|
arrays: [
|
|
{ pattern: /array\(\)/g, replacement: '[]', description: 'PHP空数组' },
|
|
{ pattern: /array\(([^)]+)\)/g, replacement: '[$1]', description: 'PHP数组语法' },
|
|
{ pattern: /'([a-zA-Z_][a-zA-Z0-9_]*)'\s*=>/g, replacement: '$1:', description: 'PHP关联数组键' },
|
|
{ pattern: /"([a-zA-Z_][a-zA-Z0-9_]*)"\s*=>/g, replacement: '$1:', description: 'PHP关联数组键(双引号)' }
|
|
],
|
|
|
|
objects: [
|
|
{ pattern: /\[\s*\]/g, replacement: '[]', description: '空数组语法' },
|
|
{ pattern: /\(\s*\)/g, replacement: '()', description: '空括号语法' }
|
|
]
|
|
},
|
|
|
|
// 异常处理转换
|
|
exceptions: [
|
|
{ pattern: /CommonException/g, replacement: 'BusinessException', description: 'PHP通用异常' },
|
|
{ pattern: /(?<!Business)Exception/g, replacement: 'BusinessException', description: 'PHP异常类' },
|
|
{ pattern: /BusinessBusinessException/g, replacement: 'BusinessException', description: '修复重复Business前缀' }
|
|
],
|
|
|
|
// 服务调用转换
|
|
services: {
|
|
instantiation: [
|
|
{ pattern: /new\s+([A-Z][a-zA-Z0-9_]*)\(\)/g, replacement: 'this.$1Service', description: 'PHP服务实例化' },
|
|
{ pattern: /\(new\s+([A-Z][a-zA-Z0-9_]*)\(\)\)/g, replacement: 'this.$1Service', description: 'PHP服务实例化(括号)' }
|
|
],
|
|
|
|
calls: [
|
|
{ pattern: /\(([^)]+)\)\s*->\s*(\w+)\(/g, replacement: '($1).$2(', description: 'PHP服务方法调用' },
|
|
{ pattern: /(\w+_service)\s*\.\s*(\w+)\(/g, replacement: '$1.$2(', description: 'PHP服务变量调用' }
|
|
]
|
|
},
|
|
|
|
// 字符串处理
|
|
strings: [
|
|
{ pattern: /\.\s*=/g, replacement: '+=', description: 'PHP字符串连接赋值' },
|
|
{ pattern: /\.(\s*['""])/g, replacement: ' + $1', description: 'PHP字符串连接' },
|
|
{ pattern: /process\.env\.'([^']+)'/g, replacement: 'process.env.$1', description: '修复process.env引号' }
|
|
],
|
|
|
|
// 语法错误修复
|
|
syntaxFixes: {
|
|
brackets: [
|
|
{ pattern: /\(([^)]+)\]/g, replacement: '($1)', description: '修复函数调用中的方括号' },
|
|
{ pattern: /(\w+)\]/g, replacement: '$1)', description: '修复变量后的方括号' },
|
|
{ pattern: /\]\s*;/g, replacement: ');', description: '修复方括号后分号' },
|
|
{ pattern: /\]\s*\)/g, replacement: '))', description: '修复方括号后括号' },
|
|
{ pattern: /\]\s*\{/g, replacement: ') {', description: '修复方括号后大括号' },
|
|
{ pattern: /\]\s*,/g, replacement: '),', description: '修复方括号后逗号' }
|
|
],
|
|
|
|
specific: [
|
|
{ pattern: /(\w+_id)\]/g, replacement: '$1)', description: '修复ID变量方括号' },
|
|
{ pattern: /(\w+_key)\]/g, replacement: '$1)', description: '修复KEY变量方括号' },
|
|
{ pattern: /(\w+_type)\]/g, replacement: '$1)', description: '修复TYPE变量方括号' },
|
|
{ pattern: /(\w+_name)\]/g, replacement: '$1)', description: '修复NAME变量方括号' },
|
|
{ pattern: /(\w+_code)\]/g, replacement: '$1)', description: '修复CODE变量方括号' },
|
|
{ pattern: /(\w+_value)\]/g, replacement: '$1)', description: '修复VALUE变量方括号' }
|
|
],
|
|
|
|
functions: [
|
|
{ pattern: /(\w+)\(([^)]+)\]/g, replacement: '$1($2)', description: '修复函数调用方括号' },
|
|
{ pattern: /(\w+)\.(\w+)\(([^)]+)\]/g, replacement: '$1.$2($3)', description: '修复方法调用方括号' }
|
|
]
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取转换规则
|
|
*/
|
|
getRules(category = null) {
|
|
if (category) {
|
|
return this.rules[category] || {};
|
|
}
|
|
return this.rules;
|
|
}
|
|
|
|
/**
|
|
* 添加新规则
|
|
*/
|
|
addRule(category, rule) {
|
|
if (!this.rules[category]) {
|
|
this.rules[category] = [];
|
|
}
|
|
this.rules[category].push(rule);
|
|
}
|
|
|
|
/**
|
|
* 应用转换规则
|
|
*/
|
|
applyRules(code, category = null) {
|
|
let convertedCode = code;
|
|
const rulesToApply = category ? this.getRules(category) : this.rules;
|
|
|
|
// 递归应用所有规则
|
|
const applyCategoryRules = (rules) => {
|
|
if (Array.isArray(rules)) {
|
|
rules.forEach(rule => {
|
|
convertedCode = convertedCode.replace(rule.pattern, rule.replacement);
|
|
});
|
|
} else if (typeof rules === 'object') {
|
|
Object.values(rules).forEach(categoryRules => {
|
|
applyCategoryRules(categoryRules);
|
|
});
|
|
}
|
|
};
|
|
|
|
applyCategoryRules(rulesToApply);
|
|
return convertedCode;
|
|
}
|
|
|
|
/**
|
|
* 获取规则统计信息
|
|
*/
|
|
getStats() {
|
|
const stats = {
|
|
total: 0,
|
|
byCategory: {}
|
|
};
|
|
|
|
const countRules = (rules, category = '') => {
|
|
if (Array.isArray(rules)) {
|
|
stats.total += rules.length;
|
|
if (category) {
|
|
stats.byCategory[category] = rules.length;
|
|
}
|
|
} else if (typeof rules === 'object') {
|
|
Object.entries(rules).forEach(([key, value]) => {
|
|
countRules(value, key);
|
|
});
|
|
}
|
|
};
|
|
|
|
countRules(this.rules);
|
|
return stats;
|
|
}
|
|
}
|
|
|
|
module.exports = ConversionRulesDatabase;
|