Skip to content

Commit 6d6ef0f

Browse files
ackintoshwing328
authored andcommitted
[PHP] Refactor php client generator (#504)
* Extends AbstractPhpCodegen instead of DefaultCodegen * Remove fully duplicated methods with AbstractPhpCodegen * Remove duplicated properties with AbstractPhpCodegen * Remove duplicated codes in constructor with AbstractPhpCodegen * Add typeMapping "date". Moved from PhpClientCodegen refs: 3c34c0b#diff-f1801ef05a7926bf394c90f44ae4ab3dL132 * Remove duplicated codes in processOpts() * Remove unnecessary 'implements' * Remove unnecessary method override * Use setter * Merge getTypeDeclaration() into AbstractPhpCodegen * Merge processOpts() into AbstractPhpCodegen refs: * 296e6d3#diff-f1801ef05a7926bf394c90f44ae4ab3dL139 * 296e6d3#diff-f1801ef05a7926bf394c90f44ae4ab3dL147 * 296e6d3#diff-f1801ef05a7926bf394c90f44ae4ab3dL153 * tweak * Optimize IF statement * Remove duplicated methods * Merge setParameterExampleValue() into AbstractPhpCodegen * Merge toEnumVarName() into AbstractPhpCodegen * Merge toEnumName() into AbstractPhpCodegen * Merge escapeUnsafeCharacters() into AbstractPhpCodegen * Merge postProcessOperationsWithModels() into AbstractPhpCodegen * tweak * Recover missing method refs: 2ad0f6f#diff-f1801ef05a7926bf394c90f44ae4ab3dL91 * Tweak test case refs: 4e7b7af * Remove unnecessary 'import' * Update lumen and ze-ph samples - ./bin/php-lumen-petstore-server.sh > /dev/null 2>&1 - ./bin/php-ze-ph-petstore-server.sh > /dev/null 2>&1 * Update slim samples * Fix script name * Update silex samples * Update kotlin-server
1 parent afb2388 commit 6d6ef0f

10 files changed

Lines changed: 60 additions & 653 deletions

File tree

bin/utils/ensure-up-to-date

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sleep 5
2222
./bin/php-silex-petstore-server.sh > /dev/null 2>&1
2323
./bin/php-symfony-petstore.sh > /dev/null 2>&1
2424
./bin/php-lumen-petstore-server.sh > /dev/null 2>&1
25-
./bin/php-slim-petstore-server.sh > /dev/null 2>&1
25+
./bin/php-slim-server-petstore.sh > /dev/null 2>&1
2626
./bin/php-ze-ph-petstore-server.sh > /dev/null 2>&1
2727
./bin/openapi3/php-petstore.sh > /dev/null 2>&1
2828
./bin/typescript-angular-petstore-all.sh > /dev/null 2>&1

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public AbstractPhpCodegen() {
127127
typeMapping.put("string", "string");
128128
typeMapping.put("byte", "int");
129129
typeMapping.put("boolean", "bool");
130+
typeMapping.put("date", "\\DateTime");
130131
typeMapping.put("Date", "\\DateTime");
131132
typeMapping.put("DateTime", "\\DateTime");
132133
typeMapping.put("file", "\\SplFileObject");
@@ -170,17 +171,25 @@ public void processOpts() {
170171

171172
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
172173
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
174+
175+
// Update the invokerPackage for the default apiPackage and modelPackage
176+
apiPackage = invokerPackage + "\\" + apiDirName;
177+
modelPackage = invokerPackage + "\\" + modelDirName;
173178
} else {
174179
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
175180
}
176181

177-
if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
178-
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
182+
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
183+
// Update model package to contain the specified model package name and the invoker package
184+
modelPackage = invokerPackage + "\\" + (String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE);
179185
}
186+
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
180187

181-
if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
182-
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
188+
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
189+
// Update api package to contain the specified api package name and the invoker package
190+
apiPackage = invokerPackage + "\\" + (String) additionalProperties.get(CodegenConstants.API_PACKAGE);
183191
}
192+
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
184193

185194
// if (additionalProperties.containsKey(COMPOSER_PROJECT_NAME)) {
186195
// this.setComposerProjectName((String) additionalProperties.get(COMPOSER_PROJECT_NAME));
@@ -329,7 +338,7 @@ public String getTypeDeclaration(Schema p) {
329338
} else if (ModelUtils.isMapSchema(p)) {
330339
Schema inner = (Schema) p.getAdditionalProperties();
331340
return getSchemaType(p) + "[string," + getTypeDeclaration(inner) + "]";
332-
} else if (!StringUtils.isEmpty(p.get$ref())) { // model
341+
} else if (StringUtils.isNotBlank(p.get$ref())) { // model
333342
String type = super.getTypeDeclaration(p);
334343
return (!languageSpecificPrimitives.contains(type))
335344
? "\\" + modelPackage + "\\" + type : type;
@@ -542,11 +551,11 @@ public void setParameterExampleValue(CodegenParameter p) {
542551
type = p.dataType;
543552
}
544553

545-
if ("String".equalsIgnoreCase(type)) {
554+
if ("String".equalsIgnoreCase(type) || p.isString) {
546555
if (example == null) {
547-
example = p.paramName + "_example";
556+
example = "'" + p.paramName + "_example'";
548557
}
549-
example = "\"" + escapeText(example) + "\"";
558+
example = escapeText(example);
550559
} else if ("Integer".equals(type) || "int".equals(type)) {
551560
if (example == null) {
552561
example = "56";
@@ -559,21 +568,23 @@ public void setParameterExampleValue(CodegenParameter p) {
559568
if (example == null) {
560569
example = "True";
561570
}
562-
} else if ("\\SplFileObject".equalsIgnoreCase(type)) {
571+
} else if ("\\SplFileObject".equalsIgnoreCase(type) || p.isFile) {
563572
if (example == null) {
564-
example = "/path/to/file";
573+
example = "/path/to/file.txt";
565574
}
566575
example = "\"" + escapeText(example) + "\"";
567-
} else if ("Date".equalsIgnoreCase(type)) {
576+
} else if ("\\Date".equalsIgnoreCase(type)) {
568577
if (example == null) {
569578
example = "2013-10-20";
570579
}
571580
example = "new \\DateTime(\"" + escapeText(example) + "\")";
572-
} else if ("DateTime".equalsIgnoreCase(type)) {
581+
} else if ("\\DateTime".equalsIgnoreCase(type)) {
573582
if (example == null) {
574583
example = "2013-10-20T19:20:30+01:00";
575584
}
576585
example = "new \\DateTime(\"" + escapeText(example) + "\")";
586+
} else if ("object".equals(type)) {
587+
example = "new \\stdClass";
577588
} else if (!languageSpecificPrimitives.contains(type)) {
578589
// type is a model class, e.g. User
579590
example = "new " + getTypeDeclaration(type) + "()";
@@ -631,8 +642,8 @@ public String toEnumVarName(String name, String datatype) {
631642
enumName = enumName.replaceFirst("^_", "");
632643
enumName = enumName.replaceFirst("_$", "");
633644

634-
if (enumName.matches("\\d.*")) { // starts with number
635-
return "_" + enumName;
645+
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
646+
return escapeReservedWord(enumName);
636647
} else {
637648
return enumName;
638649
}
@@ -642,6 +653,9 @@ public String toEnumVarName(String name, String datatype) {
642653
public String toEnumName(CodegenProperty property) {
643654
String enumName = underscore(toModelName(property.name)).toUpperCase();
644655

656+
// remove [] for array or map of enum
657+
enumName = enumName.replace("[]", "");
658+
645659
if (enumName.matches("\\d.*")) { // starts with number
646660
return "_" + enumName;
647661
} else {
@@ -660,6 +674,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
660674
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
661675
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
662676
for (CodegenOperation op : operationList) {
677+
// for API test method name
678+
// e.g. public function test{{vendorExtensions.x-testOperationId}}()
663679
op.vendorExtensions.put("x-testOperationId", camelize(op.operationId));
664680
}
665681
return objs;
@@ -673,7 +689,17 @@ public String escapeQuotationMark(String input) {
673689

674690
@Override
675691
public String escapeUnsafeCharacters(String input) {
676-
return input.replace("*/", "");
692+
return input.replace("*/", "*_/").replace("/*", "/_*");
693+
}
694+
695+
@Override
696+
public String escapeText(String input) {
697+
if (input == null) {
698+
return input;
699+
}
700+
701+
// Trim the string to avoid leading and trailing spaces.
702+
return super.escapeText(input).trim();
677703
}
678704

679705
protected String extractSimpleName(String phpClassName) {

0 commit comments

Comments
 (0)