make srcBasePath configureable

This commit is contained in:
nmonterroso 2015-06-22 20:24:19 -07:00
parent d5d1483656
commit c93b0dd3b8
3 changed files with 21 additions and 9 deletions

View File

@ -25,6 +25,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String artifactId = "swagger-client"; protected String artifactId = "swagger-client";
protected String packagePath = "SwaggerClient-php"; protected String packagePath = "SwaggerClient-php";
protected String artifactVersion = null; protected String artifactVersion = null;
protected String srcBasePath = "lib";
public PhpClientCodegen() { public PhpClientCodegen() {
super(); super();
@ -44,6 +45,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
additionalProperties.put("invokerPackage", invokerPackage); additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("modelPackage", modelPackage); additionalProperties.put("modelPackage", modelPackage);
additionalProperties.put("apiPackage", apiPackage); additionalProperties.put("apiPackage", apiPackage);
additionalProperties.put("srcBasePath", srcBasePath);
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
additionalProperties.put("groupId", groupId); additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId); additionalProperties.put("artifactId", artifactId);
@ -89,6 +91,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("DateTime", "\\DateTime"); typeMapping.put("DateTime", "\\DateTime");
cliOptions.add(new CliOption("packagePath", "main package name for classes")); cliOptions.add(new CliOption("packagePath", "main package name for classes"));
cliOptions.add(new CliOption("srcBasePath", "directory directory under packagePath to serve as source root"));
} }
public String getPackagePath() { public String getPackagePath() {
@ -130,6 +133,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
this.setPackagePath((String) additionalProperties.get("packagePath")); this.setPackagePath((String) additionalProperties.get("packagePath"));
} }
if (additionalProperties.containsKey("srcBasePath")) {
this.setSrcBasePath((String) additionalProperties.get("srcBasePath"));
}
if (additionalProperties.containsKey("modelPackage")) { if (additionalProperties.containsKey("modelPackage")) {
this.setModelPackage(invokerPackage + "\\" + additionalProperties.get("modelPackage")); this.setModelPackage(invokerPackage + "\\" + additionalProperties.get("modelPackage"));
} }
@ -138,14 +145,15 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
this.setApiPackage(invokerPackage + "\\" + additionalProperties.get("apiPackage")); this.setApiPackage(invokerPackage + "\\" + additionalProperties.get("apiPackage"));
} }
additionalProperties.replace("srcBasePath", srcBasePath);
additionalProperties.replace("modelPackage", modelPackage); additionalProperties.replace("modelPackage", modelPackage);
additionalProperties.replace("apiPackage", apiPackage); additionalProperties.replace("apiPackage", apiPackage);
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\")); additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
supportingFiles.add(new SupportingFile("ApiClientConfiguration.mustache", toPackagePath(invokerPackage, "lib"), "ApiClientConfiguration.php")); supportingFiles.add(new SupportingFile("ApiClientConfiguration.mustache", toPackagePath(invokerPackage, srcBasePath), "ApiClientConfiguration.php"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", toPackagePath(invokerPackage, "lib"), "ApiClient.php")); supportingFiles.add(new SupportingFile("ApiClient.mustache", toPackagePath(invokerPackage, srcBasePath), "ApiClient.php"));
supportingFiles.add(new SupportingFile("ApiException.mustache", toPackagePath(invokerPackage, "lib"), "ApiException.php")); supportingFiles.add(new SupportingFile("ApiException.mustache", toPackagePath(invokerPackage, srcBasePath), "ApiException.php"));
supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toPackagePath(invokerPackage, "lib"), "ObjectSerializer.php")); supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toPackagePath(invokerPackage, srcBasePath), "ObjectSerializer.php"));
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json")); supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php")); supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php"));
} }
@ -157,11 +165,11 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override @Override
public String apiFileFolder() { public String apiFileFolder() {
return (outputFolder + "/" + toPackagePath(apiPackage(), "lib")); return (outputFolder + "/" + toPackagePath(apiPackage(), srcBasePath));
} }
public String modelFileFolder() { public String modelFileFolder() {
return (outputFolder + "/" + toPackagePath(modelPackage(), "lib")); return (outputFolder + "/" + toPackagePath(modelPackage(), srcBasePath));
} }
@Override @Override
@ -222,6 +230,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
this.packagePath = packagePath; this.packagePath = packagePath;
} }
public void setSrcBasePath(String srcBasePath) {
this.srcBasePath = srcBasePath;
}
@Override @Override
public String toVarName(String name) { public String toVarName(String name) {
// parameter name starting with number won't compile // parameter name starting with number won't compile

View File

@ -4,7 +4,7 @@
* *
* After registering this autoload function with SPL, the following line * After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \{{invokerPackage}}\Baz\Qux class * would cause the function to attempt to load the \{{invokerPackage}}\Baz\Qux class
* from /path/to/project/lib/Baz/Qux.php: * from /path/to/project/{{srcBasePath}}/Baz/Qux.php:
* *
* new \{{invokerPackage}}\Baz\Qux; * new \{{invokerPackage}}\Baz\Qux;
* *
@ -17,7 +17,7 @@ spl_autoload_register(function ($class) {
$prefix = '{{escapedInvokerPackage}}\\'; $prefix = '{{escapedInvokerPackage}}\\';
// base directory for the namespace prefix // base directory for the namespace prefix
$base_dir = __DIR__ . '/lib/'; $base_dir = __DIR__ . '/{{srcBasePath}}/';
// does the class use the namespace prefix? // does the class use the namespace prefix?
$len = strlen($prefix); $len = strlen($prefix);

View File

@ -28,6 +28,6 @@
"squizlabs/php_codesniffer": "~2.0" "squizlabs/php_codesniffer": "~2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\" : "lib/" } "psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
} }
} }