forked from loafle/openapi-generator-original
[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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
@@ -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}}
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}}
|
||||
|
||||
@@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController;
|
||||
*/
|
||||
class AnotherFakeApi extends AbstractApiController
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* PATCH call123TestSpecialTags
|
||||
* Summary: To test special tags
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -39,7 +39,7 @@ use OpenAPIServer\AbstractApiController;
|
||||
*/
|
||||
class FakeClassnameTags123Api extends AbstractApiController
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* PATCH testClassname
|
||||
* Summary: To test class name in snake case
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user