proper autoloader and move generated files to PSR-4 compliant locations

This commit is contained in:
nmonterroso
2015-06-18 15:08:45 -07:00
parent 4ef34680cd
commit 4495774fd5
3 changed files with 113 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
package io.swagger.codegen.languages;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
@@ -12,22 +13,25 @@ import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "io.swagger.client";
protected String groupId = "io.swagger";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
protected String rootNamespace;
protected String invokerNamespace;
protected String modelNamespace;
protected String apiNamespace;
public PhpClientCodegen() {
super();
invokerPackage = camelize("SwaggerClient");
String packagePath = invokerPackage + "-php";
modelPackage = packagePath + "/lib/models";
apiPackage = packagePath + "/lib";
rootNamespace = "Swagger";
invokerPackage = "Client";
modelPackage = "Models";
apiPackage = "Api";
outputFolder = "generated-code/php";
modelTemplateFiles.put("model.mustache", ".php");
apiTemplateFiles.put("api.mustache", ".php");
@@ -78,11 +82,8 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("list", "array");
typeMapping.put("object", "object");
supportingFiles.add(new SupportingFile("composer.mustache", packagePath.replace('/', File.separatorChar), "composer.json"));
supportingFiles.add(new SupportingFile("configuration.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "Configuration.php"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiClient.php"));
supportingFiles.add(new SupportingFile("ApiException.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "ApiException.php"));
supportingFiles.add(new SupportingFile("require.mustache", packagePath.replace('/', File.separatorChar), invokerPackage + ".php"));
cliOptions.add(new CliOption("rootNamespace", "root namespace from which other namespaces derive"));
cliOptions.add(new CliOption("invokerPackage", "namespace for core, non-api-specific classes"));
}
public CodegenType getTag() {
@@ -97,6 +98,38 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return "Generates a PHP client library.";
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey("invokerPackage")) {
this.setInvokerPackage((String) additionalProperties.get("invokerPackage"));
}
if (additionalProperties.containsKey("rootNamespace")) {
this.setRootNamespace((String) additionalProperties.get("rootNamespace"));
}
setNamespacesFromPackages();
prefixPackages();
supportingFiles.add(new SupportingFile("configuration.mustache", invokerPackage.replace('/', File.separatorChar), "Configuration.php"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerPackage.replace('/', File.separatorChar), "ApiClient.php"));
supportingFiles.add(new SupportingFile("ApiException.mustache", invokerPackage.replace('/', File.separatorChar), "ApiException.php"));
supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json"));
supportingFiles.add(new SupportingFile("autoload.mustache", "", "autoload.php"));
}
protected String getSrcDir(String packageName) {
return rootNamespace + "/src/" + packageName;
}
protected void prefixPackages() {
setApiPackage(getSrcDir(apiPackage));
setInvokerPackage(getSrcDir(invokerPackage));
setModelPackage(getSrcDir(modelPackage));
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
@@ -149,6 +182,13 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return "null";
}
public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
public void setRootNamespace(String rootNamespace) {
this.rootNamespace = rootNamespace;
}
@Override
public String toVarName(String name) {
@@ -187,4 +227,37 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(name);
}
public String toNamespace(String packageName) {
return rootNamespace + "\\" + packageName.replace('/', '\\').replace('.', '\\');
}
protected void setNamespacesFromPackages() {
invokerNamespace = toNamespace(invokerPackage);
apiNamespace = toNamespace(apiPackage);
modelNamespace = toNamespace(modelPackage);
}
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
return addNamespaces(super.postProcessModels(objs));
}
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
return addNamespaces(super.postProcessOperations(objs));
}
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
return addNamespaces(super.postProcessSupportingFileData(objs));
}
protected Map<String, Object> addNamespaces(Map<String, Object> objs) {
objs.put("rootNamespace", rootNamespace);
objs.put("invokerNamespace", invokerNamespace);
objs.put("apiNamespace", apiNamespace);
objs.put("modelNamespace", modelNamespace);
return objs;
}
}

View File

@@ -0,0 +1,29 @@
<?php
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = '{{rootNamespace}}\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});

View File

@@ -1,13 +0,0 @@
<?php
// load models defined for endpoints
foreach (glob(dirname(__FILE__)."/lib/models/*.php") as $filename)
{
require_once $filename;
}
// load classes for accessing the endpoints
foreach (glob(dirname(__FILE__)."/lib/*.php") as $filename)
{
require_once $filename;
}
?>