From 8a034ce06388c08f09ce2552986224cc55d05ed1 Mon Sep 17 00:00:00 2001 From: Yuriy Belenko Date: Sun, 2 Sep 2018 18:21:53 +0500 Subject: [PATCH] [PHP] Add interface/abstract/trait helpers (#906) * [PHP] Enhance interfaces, abstracts and traits It would be helpful to set prefix/suffix for all interfaces, abstracts and traits in one place. Defaults are set to follow "PSR Naming Conventions". If user will ask we can add prefix/suffix generator options in future. I don't see modelPrefix generator option, so I assume it's not important now. Ref: https://www.php-fig.org/bylaws/psr-naming-conventions/ * [Slim] Refactor to use new helpers * [Slim] Refresh samples --- .../codegen/languages/AbstractPhpCodegen.java | 41 +++++++++++++++++++ .../languages/PhpSlimServerCodegen.java | 2 +- .../AbstractApiController.mustache | 2 +- .../resources/php-slim-server/api.mustache | 6 +-- .../php-slim/lib/Api/AnotherFakeApi.php | 2 +- .../petstore/php-slim/lib/Api/FakeApi.php | 22 +++++----- .../lib/Api/FakeClassnameTags123Api.php | 2 +- .../petstore/php-slim/lib/Api/PetApi.php | 18 ++++---- .../petstore/php-slim/lib/Api/StoreApi.php | 8 ++-- .../petstore/php-slim/lib/Api/UserApi.php | 16 ++++---- 10 files changed, 80 insertions(+), 39 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java index 2b27fdbae191..2490293204d8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java @@ -69,6 +69,9 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg protected String variableNamingConvention = "snake_case"; protected String apiDocPath = docsBasePath + File.separator + apiDirName; protected String modelDocPath = docsBasePath + File.separator + modelDirName; + protected String interfaceNamePrefix = "", interfaceNameSuffix = "Interface"; + protected String abstractNamePrefix = "Abstract", abstractNameSuffix = ""; + protected String traitNamePrefix = "", traitNameSuffix = "Trait"; public AbstractPhpCodegen() { super(); @@ -241,6 +244,14 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg // make test path available in mustache template additionalProperties.put("testBasePath", testBasePath); + // make class prefixes and suffixes available in mustache templates + additionalProperties.put("interfaceNamePrefix", interfaceNamePrefix); + additionalProperties.put("interfaceNameSuffix", interfaceNameSuffix); + additionalProperties.put("abstractNamePrefix", abstractNamePrefix); + additionalProperties.put("abstractNameSuffix", abstractNameSuffix); + additionalProperties.put("traitNamePrefix", traitNamePrefix); + additionalProperties.put("traitNameSuffix", traitNameSuffix); + // apache v2 license // supportingFiles.add(new SupportingFile("LICENSE", "", "LICENSE")); @@ -487,6 +498,36 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg return toModelName(name) + "Test"; } + /** + * Output the proper interface name (capitalized). + * + * @param name the name of the interface + * @return capitalized model name + */ + public String toInterfaceName(final String name) { + return org.openapitools.codegen.utils.StringUtils.camelize(interfaceNamePrefix + name + interfaceNameSuffix); + } + + /** + * Output the proper abstract class name (capitalized). + * + * @param name the name of the class + * @return capitalized abstract class name + */ + public String toAbstractName(final String name) { + return org.openapitools.codegen.utils.StringUtils.camelize(abstractNamePrefix + name + abstractNameSuffix); + } + + /** + * Output the proper trait name (capitalized). + * + * @param name the name of the trait + * @return capitalized trait name + */ + public String toTraitName(final String name) { + return org.openapitools.codegen.utils.StringUtils.camelize(traitNamePrefix + name + traitNameSuffix); + } + @Override public String toOperationId(String operationId) { // throw exception if method name is empty diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java index ea3549c7af20..abf676d4fec3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java @@ -125,7 +125,7 @@ public class PhpSlimServerCodegen extends AbstractPhpCodegen { supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json")); supportingFiles.add(new SupportingFile("index.mustache", "", "index.php")); supportingFiles.add(new SupportingFile(".htaccess", "", ".htaccess")); - supportingFiles.add(new SupportingFile("AbstractApiController.mustache", toSrcPath(invokerPackage, srcBasePath), "AbstractApiController.php")); + supportingFiles.add(new SupportingFile("AbstractApiController.mustache", toSrcPath(invokerPackage, srcBasePath), toAbstractName("ApiController") + ".php")); supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php")); supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist")); } diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/AbstractApiController.mustache b/modules/openapi-generator/src/main/resources/php-slim-server/AbstractApiController.mustache index 4ba2ef121cb7..ab214f671fa2 100644 --- a/modules/openapi-generator/src/main/resources/php-slim-server/AbstractApiController.mustache +++ b/modules/openapi-generator/src/main/resources/php-slim-server/AbstractApiController.mustache @@ -44,7 +44,7 @@ namespace {{invokerPackage}}; * @author OpenAPI Generator team * @link https://github.com/openapitools/openapi-generator */ -abstract class AbstractApiController +abstract class {{abstractNamePrefix}}ApiController{{abstractNameSuffix}} { /** diff --git a/modules/openapi-generator/src/main/resources/php-slim-server/api.mustache b/modules/openapi-generator/src/main/resources/php-slim-server/api.mustache index 1bc94ad3cdbe..832374d761b3 100644 --- a/modules/openapi-generator/src/main/resources/php-slim-server/api.mustache +++ b/modules/openapi-generator/src/main/resources/php-slim-server/api.mustache @@ -34,7 +34,7 @@ */ namespace {{apiPackage}}; -use {{invokerPackage}}\AbstractApiController; +use {{invokerPackage}}\{{abstractNamePrefix}}ApiController{{abstractNameSuffix}}; /** * {{classname}} Class Doc Comment @@ -46,11 +46,11 @@ use {{invokerPackage}}\AbstractApiController; * @author OpenAPI Generator team * @link https://github.com/openapitools/openapi-generator */ -class {{classname}} extends AbstractApiController +class {{classname}} extends {{abstractNamePrefix}}ApiController{{abstractNameSuffix}} { {{#operations}} {{#operation}} - + /** * {{httpMethod}} {{operationId}} {{#summary}} diff --git a/samples/server/petstore/php-slim/lib/Api/AnotherFakeApi.php b/samples/server/petstore/php-slim/lib/Api/AnotherFakeApi.php index 80b1a67e4275..31a641e55283 100644 --- a/samples/server/petstore/php-slim/lib/Api/AnotherFakeApi.php +++ b/samples/server/petstore/php-slim/lib/Api/AnotherFakeApi.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class AnotherFakeApi extends AbstractApiController { - + /** * PATCH call123TestSpecialTags * Summary: To test special tags diff --git a/samples/server/petstore/php-slim/lib/Api/FakeApi.php b/samples/server/petstore/php-slim/lib/Api/FakeApi.php index 3d69b510d035..c93048cdfe53 100644 --- a/samples/server/petstore/php-slim/lib/Api/FakeApi.php +++ b/samples/server/petstore/php-slim/lib/Api/FakeApi.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class FakeApi extends AbstractApiController { - + /** * POST fakeOuterBooleanSerialize * Notes: Test serialization of outer boolean types @@ -55,7 +55,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing fakeOuterBooleanSerialize as a POST method ?'); return $response; } - + /** * POST fakeOuterCompositeSerialize * Notes: Test serialization of object with outer number type @@ -71,7 +71,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing fakeOuterCompositeSerialize as a POST method ?'); return $response; } - + /** * POST fakeOuterNumberSerialize * Notes: Test serialization of outer number types @@ -87,7 +87,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing fakeOuterNumberSerialize as a POST method ?'); return $response; } - + /** * POST fakeOuterStringSerialize * Notes: Test serialization of outer string types @@ -103,7 +103,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing fakeOuterStringSerialize as a POST method ?'); return $response; } - + /** * PUT testBodyWithFileSchema * Notes: For this test, the body for this request much reference a schema named `File`. @@ -118,7 +118,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testBodyWithFileSchema as a PUT method ?'); return $response; } - + /** * PUT testBodyWithQueryParams * @@ -134,7 +134,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testBodyWithQueryParams as a PUT method ?'); return $response; } - + /** * PATCH testClientModel * Summary: To test \"client\" model @@ -151,7 +151,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testClientModel as a PATCH method ?'); return $response; } - + /** * POST testEndpointParameters * Summary: Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 @@ -180,7 +180,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testEndpointParameters as a POST method ?'); return $response; } - + /** * GET testEnumParameters * Summary: To test enum parameters @@ -205,7 +205,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testEnumParameters as a GET method ?'); return $response; } - + /** * POST testInlineAdditionalProperties * Summary: test inline additionalProperties @@ -220,7 +220,7 @@ class FakeApi extends AbstractApiController $response->write('How about implementing testInlineAdditionalProperties as a POST method ?'); return $response; } - + /** * GET testJsonFormData * Summary: test json serialization of form data diff --git a/samples/server/petstore/php-slim/lib/Api/FakeClassnameTags123Api.php b/samples/server/petstore/php-slim/lib/Api/FakeClassnameTags123Api.php index a245034eaa18..c46999940499 100644 --- a/samples/server/petstore/php-slim/lib/Api/FakeClassnameTags123Api.php +++ b/samples/server/petstore/php-slim/lib/Api/FakeClassnameTags123Api.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class FakeClassnameTags123Api extends AbstractApiController { - + /** * PATCH testClassname * Summary: To test class name in snake case diff --git a/samples/server/petstore/php-slim/lib/Api/PetApi.php b/samples/server/petstore/php-slim/lib/Api/PetApi.php index b8ddd815a89c..08fd51b46e92 100644 --- a/samples/server/petstore/php-slim/lib/Api/PetApi.php +++ b/samples/server/petstore/php-slim/lib/Api/PetApi.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class PetApi extends AbstractApiController { - + /** * POST addPet * Summary: Add a new pet to the store @@ -54,7 +54,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing addPet as a POST method ?'); return $response; } - + /** * DELETE deletePet * Summary: Deletes a pet @@ -71,7 +71,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing deletePet as a DELETE method ?'); return $response; } - + /** * GET findPetsByStatus * Summary: Finds Pets by status @@ -89,7 +89,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing findPetsByStatus as a GET method ?'); return $response; } - + /** * GET findPetsByTags * Summary: Finds Pets by tags @@ -107,7 +107,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing findPetsByTags as a GET method ?'); return $response; } - + /** * GET getPetById * Summary: Find pet by ID @@ -124,7 +124,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing getPetById as a GET method ?'); return $response; } - + /** * PUT updatePet * Summary: Update an existing pet @@ -139,7 +139,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing updatePet as a PUT method ?'); return $response; } - + /** * POST updatePetWithForm * Summary: Updates a pet in the store with form data @@ -156,7 +156,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing updatePetWithForm as a POST method ?'); return $response; } - + /** * POST uploadFile * Summary: uploads an image @@ -174,7 +174,7 @@ class PetApi extends AbstractApiController $response->write('How about implementing uploadFile as a POST method ?'); return $response; } - + /** * POST uploadFileWithRequiredFile * Summary: uploads an image (required) diff --git a/samples/server/petstore/php-slim/lib/Api/StoreApi.php b/samples/server/petstore/php-slim/lib/Api/StoreApi.php index 8fcb48a41142..f668236a34f2 100644 --- a/samples/server/petstore/php-slim/lib/Api/StoreApi.php +++ b/samples/server/petstore/php-slim/lib/Api/StoreApi.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class StoreApi extends AbstractApiController { - + /** * DELETE deleteOrder * Summary: Delete purchase order by ID @@ -55,7 +55,7 @@ class StoreApi extends AbstractApiController $response->write('How about implementing deleteOrder as a DELETE method ?'); return $response; } - + /** * GET getInventory * Summary: Returns pet inventories by status @@ -71,7 +71,7 @@ class StoreApi extends AbstractApiController $response->write('How about implementing getInventory as a GET method ?'); return $response; } - + /** * GET getOrderById * Summary: Find purchase order by ID @@ -88,7 +88,7 @@ class StoreApi extends AbstractApiController $response->write('How about implementing getOrderById as a GET method ?'); return $response; } - + /** * POST placeOrder * Summary: Place an order for a pet diff --git a/samples/server/petstore/php-slim/lib/Api/UserApi.php b/samples/server/petstore/php-slim/lib/Api/UserApi.php index 39d48b919f4d..ef9863cfc333 100644 --- a/samples/server/petstore/php-slim/lib/Api/UserApi.php +++ b/samples/server/petstore/php-slim/lib/Api/UserApi.php @@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController; */ class UserApi extends AbstractApiController { - + /** * POST createUser * Summary: Create user @@ -55,7 +55,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing createUser as a POST method ?'); return $response; } - + /** * POST createUsersWithArrayInput * Summary: Creates list of users with given input array @@ -70,7 +70,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing createUsersWithArrayInput as a POST method ?'); return $response; } - + /** * POST createUsersWithListInput * Summary: Creates list of users with given input array @@ -85,7 +85,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing createUsersWithListInput as a POST method ?'); return $response; } - + /** * DELETE deleteUser * Summary: Delete user @@ -101,7 +101,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing deleteUser as a DELETE method ?'); return $response; } - + /** * GET getUserByName * Summary: Get user by user name @@ -117,7 +117,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing getUserByName as a GET method ?'); return $response; } - + /** * GET loginUser * Summary: Logs user into the system @@ -135,7 +135,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing loginUser as a GET method ?'); return $response; } - + /** * GET logoutUser * Summary: Logs out current logged in user session @@ -149,7 +149,7 @@ class UserApi extends AbstractApiController $response->write('How about implementing logoutUser as a GET method ?'); return $response; } - + /** * PUT updateUser * Summary: Updated user