[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:
Yuriy Belenko
2018-09-02 18:21:53 +05:00
committed by GitHub
parent cc53774180
commit 8a034ce063
10 changed files with 80 additions and 39 deletions

View File

@@ -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

View File

@@ -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"));
}