code compiles and *should* work, but need to re-organize for separate client/api-specific class namespaces

This commit is contained in:
nmonterroso 2015-06-22 19:26:26 -07:00
parent da14c9e692
commit 4fe979a8c0
4 changed files with 63 additions and 104 deletions

View File

@ -24,18 +24,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String groupId = "swagger"; protected String groupId = "swagger";
protected String artifactId = "swagger-client"; protected String artifactId = "swagger-client";
protected String artifactVersion = null; protected String artifactVersion = null;
protected String rootNamespace;
protected String invokerNamespace;
protected String modelNamespace;
protected String apiNamespace;
public PhpClientCodegen() { public PhpClientCodegen() {
super(); super();
rootNamespace = "Swagger";
invokerPackage = "Client";
modelPackage = "Models";
apiPackage = "Api";
outputFolder = "generated-code/php"; outputFolder = "generated-code/php";
modelTemplateFiles.put("model.mustache", ".php"); modelTemplateFiles.put("model.mustache", ".php");
apiTemplateFiles.put("api.mustache", ".php"); apiTemplateFiles.put("api.mustache", ".php");
@ -95,7 +87,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("object", "object"); typeMapping.put("object", "object");
typeMapping.put("DateTime", "\\DateTime"); typeMapping.put("DateTime", "\\DateTime");
cliOptions.add(new CliOption("rootNamespace", "root namespace from which other namespaces derive"));
cliOptions.add(new CliOption("invokerPackage", "namespace for core, non-api-specific classes")); cliOptions.add(new CliOption("invokerPackage", "namespace for core, non-api-specific classes"));
} }
@ -138,20 +129,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
this.setInvokerPackage((String) additionalProperties.get("invokerPackage")); this.setInvokerPackage((String) additionalProperties.get("invokerPackage"));
} }
if (additionalProperties.containsKey("rootNamespace")) {
this.setRootNamespace((String) additionalProperties.get("rootNamespace"));
}
prefixPackages();
// theirs
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
supportingFiles.add(new SupportingFile("configuration.mustache", toPackagePath(invokerPackage, "lib"), "Configuration.php"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", toPackagePath(invokerPackage, "lib"), "ApiClient.php"));
supportingFiles.add(new SupportingFile("ApiException.mustache", toPackagePath(invokerPackage, "lib"), "ApiException.php"));
supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php"));
// mine
supportingFiles.add(new SupportingFile("ApiClientConfiguration.mustache", toPackagePath(invokerPackage, "lib"), "ApiClientConfiguration.php")); supportingFiles.add(new SupportingFile("ApiClientConfiguration.mustache", toPackagePath(invokerPackage, "lib"), "ApiClientConfiguration.php"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", toPackagePath(invokerPackage, "lib"), "ApiClient.php")); supportingFiles.add(new SupportingFile("ApiClient.mustache", toPackagePath(invokerPackage, "lib"), "ApiClient.php"));
supportingFiles.add(new SupportingFile("ApiException.mustache", toPackagePath(invokerPackage, "lib"), "ApiException.php")); supportingFiles.add(new SupportingFile("ApiException.mustache", toPackagePath(invokerPackage, "lib"), "ApiException.php"));
@ -160,15 +137,15 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php")); supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php"));
} }
protected String getSrcDir(String packageName) { // protected String getSrcDir(String packageName) {
return "src/" + packageName; // return "src/" + packageName;
} // }
//
protected void prefixPackages() { // protected void prefixPackages() {
setApiPackage(getSrcDir(apiPackage)); // setApiPackage(getSrcDir(apiPackage));
setInvokerPackage(getSrcDir(invokerPackage)); // setInvokerPackage(getSrcDir(invokerPackage));
setModelPackage(getSrcDir(modelPackage)); // setModelPackage(getSrcDir(modelPackage));
} // }
@Override @Override
public String escapeReservedWord(String name) { public String escapeReservedWord(String name) {
@ -238,10 +215,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
this.invokerPackage = invokerPackage; this.invokerPackage = invokerPackage;
} }
public void setRootNamespace(String rootNamespace) {
this.rootNamespace = rootNamespace;
}
@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
@ -279,64 +252,54 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(name); return toModelName(name);
} }
public String toNamespace(String packageName) { // @Override
return rootNamespace + "\\" + packageName.replace('/', '\\').replace('.', '\\'); // public Map<String, Object> postProcessModels(Map<String, Object> objs) {
} // return addNamespaces(super.postProcessModels(objs));
// }
protected void setNamespacesFromPackages() { //
invokerNamespace = toNamespace(invokerPackage); // @Override
apiNamespace = toNamespace(apiPackage); // public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
modelNamespace = toNamespace(modelPackage); // objs = addNamespaces(super.postProcessOperations(objs));
} // objs = formatImports(objs, "imports", "import");
//
@Override // return objs;
public Map<String, Object> postProcessModels(Map<String, Object> objs) { // }
return addNamespaces(super.postProcessModels(objs)); //
} // @Override
// public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
@Override // objs = addNamespaces(super.postProcessSupportingFileData(objs));
public Map<String, Object> postProcessOperations(Map<String, Object> objs) { //
objs = addNamespaces(super.postProcessOperations(objs)); // return objs;
objs = formatImports(objs, "imports", "import"); // }
//
return objs; // protected Map<String, Object> addNamespaces(Map<String, Object> objs) {
} // objs.put("rootNamespace", rootNamespace);
// objs.put("invokerNamespace", invokerNamespace);
@Override // objs.put("apiNamespace", apiNamespace);
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) { // objs.put("modelNamespace", modelNamespace);
objs = addNamespaces(super.postProcessSupportingFileData(objs)); //
// return objs;
return objs; // }
} //
// protected Map<String, Object> formatImports(Map<String, Object> objs, String objsKey, String importKey) {
protected Map<String, Object> addNamespaces(Map<String, Object> objs) { // if (objs.containsKey(objsKey)) {
objs.put("rootNamespace", rootNamespace); // String modelName;
objs.put("invokerNamespace", invokerNamespace); // List<Map<String, Object>> newImportList = new ArrayList<Map<String, Object>>();
objs.put("apiNamespace", apiNamespace); //
objs.put("modelNamespace", modelNamespace); // for (Map<String, Object> importMap : (List<Map<String, Object>>) objs.get(objsKey)) {
// modelName = ((String) importMap.get(importKey)).replace(modelPackage + ".", "");
return objs; //
} // if (reservedWords.contains(modelName)) {
// continue;
protected Map<String, Object> formatImports(Map<String, Object> objs, String objsKey, String importKey) { // }
if (objs.containsKey(objsKey)) { //
String modelName; // importMap.put(importKey, modelNamespace + "\\" + modelName);
List<Map<String, Object>> newImportList = new ArrayList<Map<String, Object>>(); // newImportList.add(importMap);
// }
for (Map<String, Object> importMap : (List<Map<String, Object>>) objs.get(objsKey)) { //
modelName = ((String) importMap.get(importKey)).replace(modelPackage + ".", ""); // objs.put(objsKey, newImportList);
// }
if (reservedWords.contains(modelName)) { //
continue; // return objs;
} // }
importMap.put(importKey, modelNamespace + "\\" + modelName);
newImportList.add(importMap);
}
objs.put(objsKey, newImportList);
}
return objs;
}
} }

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
namespace {{invokerNamespace}}; namespace {{invokerPackage}};
class ApiClient { class ApiClient {
@ -77,7 +77,7 @@ class ApiClient {
* @param array $queryParams parameters to be place in query URL * @param array $queryParams parameters to be place in query URL
* @param array $postData parameters to be placed in POST body * @param array $postData parameters to be placed in POST body
* @param array $headerParams parameters to be place in request header * @param array $headerParams parameters to be place in request header
* @throws \{{invokerNamespace}}\ApiException on a non 2xx response * @throws \{{invokerPackage}}\ApiException on a non 2xx response
* @return mixed * @return mixed
*/ */
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) { public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams) {

View File

@ -1,5 +1,5 @@
<?php <?php
namespace {{invokerNamespace}}; namespace {{invokerPackage}};
class ObjectSerializer { class ObjectSerializer {

View File

@ -27,10 +27,6 @@ use \{{invokerPackage}}\ApiClient;
use \{{invokerPackage}}\ApiException; use \{{invokerPackage}}\ApiException;
use \{{invokerPackage}}\ObjectSerializer; use \{{invokerPackage}}\ObjectSerializer;
{{#imports}}
use {{{import}}};
{{/imports}}
{{#operations}} {{#operations}}
class {{classname}} { class {{classname}} {
@ -57,7 +53,7 @@ class {{classname}} {
} }
/** /**
* @param \{{invokerPackage}} $apiClient set the API client * @param \{{invokerPackage}}\ApiClient $apiClient set the API client
* @return {{classname}} * @return {{classname}}
*/ */
public function setApiClient(ApiClient $apiClient) { public function setApiClient(ApiClient $apiClient) {