From 9b1c6ca63a10ba4a42949ecb928214ae8f40d9d8 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 11 Jul 2024 17:10:33 +0800 Subject: [PATCH] feat[php]: php merger protocol --- .../serializer/php/CodeGeneratePhp.java | 71 ++++++------------- .../resources/php/ProtocolManagerTemplate.php | 1 + .../main/resources/php/ProtocolTemplate.php | 2 - 3 files changed, 23 insertions(+), 51 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/php/CodeGeneratePhp.java b/protocol/src/main/java/com/zfoo/protocol/serializer/php/CodeGeneratePhp.java index 822afe828..e2ffefe0e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/php/CodeGeneratePhp.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/php/CodeGeneratePhp.java @@ -18,12 +18,10 @@ import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolNote; import com.zfoo.protocol.generate.GenerateProtocolPath; -import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.serializer.CodeLanguage; import com.zfoo.protocol.serializer.CodeTemplatePlaceholder; import com.zfoo.protocol.serializer.ICodeGenerate; -import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; import com.zfoo.protocol.serializer.reflect.*; import com.zfoo.protocol.util.ClassUtils; import com.zfoo.protocol.util.FileUtils; @@ -82,44 +80,39 @@ public void mergerProtocol(List registrations) throws IOEx createTemplateFile(); - // 生成ProtocolManager.ts文件 - var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolManagerTemplate.ts"); + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolManagerTemplate.php"); var protocol_imports_manager = new StringBuilder(); var protocol_manager_registrations = new StringBuilder(); - protocol_imports_manager.append("import * as Protocols from './Protocols';").append(LS); + protocol_imports_manager.append("include_once 'Protocols.php';").append(LS); for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new Protocols.{}Registration());", protocol_id, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set(Protocols.{}, {});", protocol_name, protocol_id)).append(LS); + protocol_manager_registrations.append(StringUtils.format("self::$protocols[{}] = new {}Registration();", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("self::$protocolIdMap[{}::class] = {};", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports_manager.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap); - var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.ts")); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.php")); FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); - logger.info("Generated TypeScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + logger.info("Generated Php protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); - var protocol_imports_protocols = new StringBuilder(); - protocol_imports_protocols.append("import IByteBuffer from './IByteBuffer';").append(LS); - protocol_imports_protocols.append("import IProtocolRegistration from './IProtocolRegistration';").append(LS); var protocol_class = new StringBuilder(); var protocol_registration = new StringBuilder(); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolTemplate.php"); for (var registration : registrations) { - protocol_class.append(protocol_class(registration).replace("class ", "export class ")).append(LS); + protocol_class.append(protocol_class(registration)).append(LS); protocol_registration.append(protocol_registration(registration)).append(LS); } - var protocolTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolsTemplate.ts"); var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( - CodeTemplatePlaceholder.protocol_imports, protocol_imports_protocols.toString() - , CodeTemplatePlaceholder.protocol_class, protocol_class.toString() + CodeTemplatePlaceholder.protocol_class, protocol_class.toString() , CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString() )); - var outputPath = StringUtils.format("{}/Protocols.ts", protocolOutputPath); + var outputPath = StringUtils.format("{}/Protocols.php", protocolOutputPath); var file = new File(outputPath); FileUtils.writeStringToFile(file, formatProtocolTemplate, true); - logger.info("Generated TypeScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + logger.info("Generated Php protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); } @Override @@ -127,40 +120,37 @@ public void foldProtocol(List registrations) throws IOExce createTemplateFile(); - // 生成ProtocolManager.ts文件 - var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolManagerTemplate.ts"); + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolManagerTemplate.php"); var protocol_imports = new StringBuilder(); var protocol_manager_registrations = new StringBuilder(); for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - protocol_imports.append(StringUtils.format("import {} from './{}/{}';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name)).append(LS); - protocol_imports.append(StringUtils.format("import { {}Registration } from './{}/{}';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS); - protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS); + protocol_imports.append(StringUtils.format("include_once '{}/{}.php';", GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("self::$protocols[{}] = new {}Registration();", protocol_id, protocol_name)).append(LS); + protocol_manager_registrations.append(StringUtils.format("self::$protocolIdMap[{}::class] = {};", protocol_name, protocol_id)).append(LS); } var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap); - var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.ts")); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.php")); FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); - logger.info("Generated TypeScript protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + logger.info("Generated Php protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); for (var registration : registrations) { var protocol_id = registration.protocolId(); var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); - var protocolTemplate = ClassUtils.getFileFromClassPathToString("typescript/ProtocolTemplate.ts"); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolTemplate.php"); var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( - CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) - , CodeTemplatePlaceholder.protocol_name, protocol_name + CodeTemplatePlaceholder.protocol_name, protocol_name , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); - var outputPath = StringUtils.format("{}/{}/{}.ts", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name); + var outputPath = StringUtils.format("{}/{}/{}.php", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name); var file = new File(outputPath); FileUtils.writeStringToFile(file, formatProtocolTemplate, true); - logger.info("Generated TypeScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + logger.info("Generated Php protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); } } @@ -169,7 +159,6 @@ public void defaultProtocol(List registrations) throws IOE createTemplateFile(); - // 生成ProtocolManager.ts文件 var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolManagerTemplate.php"); var protocol_imports = new StringBuilder(); var protocol_manager_registrations = new StringBuilder(); @@ -194,9 +183,8 @@ public void defaultProtocol(List registrations) throws IOE var protocolTemplate = ClassUtils.getFileFromClassPathToString("php/ProtocolTemplate.php"); var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( CodeTemplatePlaceholder.protocol_name, protocol_name - , CodeTemplatePlaceholder.protocol_imports, protocol_imports_default(registration) , CodeTemplatePlaceholder.protocol_class, protocol_class(registration) - , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) + , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) )); var outputPath = StringUtils.format("{}/{}.php", protocolOutputPath, protocol_name); var file = new File(outputPath); @@ -240,21 +228,6 @@ private String protocol_registration(ProtocolRegistration registration) { return formatProtocolTemplate; } - private String protocol_imports_default(ProtocolRegistration registration) { - // import IByteBuffer first - var protocolId = registration.getId(); - var importBuilder = new StringBuilder(); - importBuilder.append(StringUtils.format("include_once 'IProtocolRegistration.php';")).append(LS); - - // import other sub protocols - var subProtocols = ProtocolAnalysis.getFirstSubProtocolIds(protocolId); - for (var subProtocolId : subProtocols) { - var protocolName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId); - importBuilder.append(StringUtils.format("include_once '{}.php';", protocolName, protocolName)).append(LS); - } - return importBuilder.toString(); - } - private String protocol_field_definition(ProtocolRegistration registration) { var protocolId = registration.protocolId(); var fields = registration.getFields(); diff --git a/protocol/src/main/resources/php/ProtocolManagerTemplate.php b/protocol/src/main/resources/php/ProtocolManagerTemplate.php index aebc0b9e0..5597fa975 100644 --- a/protocol/src/main/resources/php/ProtocolManagerTemplate.php +++ b/protocol/src/main/resources/php/ProtocolManagerTemplate.php @@ -2,6 +2,7 @@ namespace zfoophp; +include_once 'IProtocolRegistration.php'; ${protocol_imports} class ProtocolManager diff --git a/protocol/src/main/resources/php/ProtocolTemplate.php b/protocol/src/main/resources/php/ProtocolTemplate.php index 28f919a7c..a7b75e4b0 100644 --- a/protocol/src/main/resources/php/ProtocolTemplate.php +++ b/protocol/src/main/resources/php/ProtocolTemplate.php @@ -2,8 +2,6 @@ namespace zfoophp; -${protocol_imports} - ${protocol_class} ${protocol_registration} \ No newline at end of file