Skip to content

Commit

Permalink
[PHP] Refactor php client generator (#504)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ackintosh authored and wing328 committed Jul 17, 2018
1 parent afb2388 commit 6d6ef0f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 653 deletions.
2 changes: 1 addition & 1 deletion bin/utils/ensure-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sleep 5
./bin/php-silex-petstore-server.sh > /dev/null 2>&1
./bin/php-symfony-petstore.sh > /dev/null 2>&1
./bin/php-lumen-petstore-server.sh > /dev/null 2>&1
./bin/php-slim-petstore-server.sh > /dev/null 2>&1
./bin/php-slim-server-petstore.sh > /dev/null 2>&1
./bin/php-ze-ph-petstore-server.sh > /dev/null 2>&1
./bin/openapi3/php-petstore.sh > /dev/null 2>&1
./bin/typescript-angular-petstore-all.sh > /dev/null 2>&1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public AbstractPhpCodegen() {
typeMapping.put("string", "string");
typeMapping.put("byte", "int");
typeMapping.put("boolean", "bool");
typeMapping.put("date", "\\DateTime");
typeMapping.put("Date", "\\DateTime");
typeMapping.put("DateTime", "\\DateTime");
typeMapping.put("file", "\\SplFileObject");
Expand Down Expand Up @@ -170,17 +171,25 @@ public void processOpts() {

if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setInvokerPackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));

// Update the invokerPackage for the default apiPackage and modelPackage
apiPackage = invokerPackage + "\\" + apiDirName;
modelPackage = invokerPackage + "\\" + modelDirName;
} else {
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
}

if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
// Update model package to contain the specified model package name and the invoker package
modelPackage = invokerPackage + "\\" + (String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE);
}
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);

if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
// Update api package to contain the specified api package name and the invoker package
apiPackage = invokerPackage + "\\" + (String) additionalProperties.get(CodegenConstants.API_PACKAGE);
}
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);

// if (additionalProperties.containsKey(COMPOSER_PROJECT_NAME)) {
// this.setComposerProjectName((String) additionalProperties.get(COMPOSER_PROJECT_NAME));
Expand Down Expand Up @@ -329,7 +338,7 @@ public String getTypeDeclaration(Schema p) {
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = (Schema) p.getAdditionalProperties();
return getSchemaType(p) + "[string," + getTypeDeclaration(inner) + "]";
} else if (!StringUtils.isEmpty(p.get$ref())) { // model
} else if (StringUtils.isNotBlank(p.get$ref())) { // model
String type = super.getTypeDeclaration(p);
return (!languageSpecificPrimitives.contains(type))
? "\\" + modelPackage + "\\" + type : type;
Expand Down Expand Up @@ -542,11 +551,11 @@ public void setParameterExampleValue(CodegenParameter p) {
type = p.dataType;
}

if ("String".equalsIgnoreCase(type)) {
if ("String".equalsIgnoreCase(type) || p.isString) {
if (example == null) {
example = p.paramName + "_example";
example = "'" + p.paramName + "_example'";
}
example = "\"" + escapeText(example) + "\"";
example = escapeText(example);
} else if ("Integer".equals(type) || "int".equals(type)) {
if (example == null) {
example = "56";
Expand All @@ -559,21 +568,23 @@ public void setParameterExampleValue(CodegenParameter p) {
if (example == null) {
example = "True";
}
} else if ("\\SplFileObject".equalsIgnoreCase(type)) {
} else if ("\\SplFileObject".equalsIgnoreCase(type) || p.isFile) {
if (example == null) {
example = "/path/to/file";
example = "/path/to/file.txt";
}
example = "\"" + escapeText(example) + "\"";
} else if ("Date".equalsIgnoreCase(type)) {
} else if ("\\Date".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20";
}
example = "new \\DateTime(\"" + escapeText(example) + "\")";
} else if ("DateTime".equalsIgnoreCase(type)) {
} else if ("\\DateTime".equalsIgnoreCase(type)) {
if (example == null) {
example = "2013-10-20T19:20:30+01:00";
}
example = "new \\DateTime(\"" + escapeText(example) + "\")";
} else if ("object".equals(type)) {
example = "new \\stdClass";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = "new " + getTypeDeclaration(type) + "()";
Expand Down Expand Up @@ -631,8 +642,8 @@ public String toEnumVarName(String name, String datatype) {
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
return escapeReservedWord(enumName);
} else {
return enumName;
}
Expand All @@ -642,6 +653,9 @@ public String toEnumVarName(String name, String datatype) {
public String toEnumName(CodegenProperty property) {
String enumName = underscore(toModelName(property.name)).toUpperCase();

// remove [] for array or map of enum
enumName = enumName.replace("[]", "");

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
Expand All @@ -660,6 +674,8 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
// for API test method name
// e.g. public function test{{vendorExtensions.x-testOperationId}}()
op.vendorExtensions.put("x-testOperationId", camelize(op.operationId));
}
return objs;
Expand All @@ -673,7 +689,17 @@ public String escapeQuotationMark(String input) {

@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "");
return input.replace("*/", "*_/").replace("/*", "/_*");
}

@Override
public String escapeText(String input) {
if (input == null) {
return input;
}

// Trim the string to avoid leading and trailing spaces.
return super.escapeText(input).trim();
}

protected String extractSimpleName(String phpClassName) {
Expand Down
Loading

0 comments on commit 6d6ef0f

Please sign in to comment.