Skip to content

[PHP] Support for customizing namespace and Composer fields #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "Swagger\\Client";
protected String groupId = "swagger";
protected String artifactId = "swagger-client";
protected String composerVendorName = "swagger";
protected String composerProjectName = "swagger-client";
protected String packagePath = "SwaggerClient-php";
protected String artifactVersion = "1.0.0";
protected String srcBasePath = "lib";
Expand All @@ -38,15 +38,6 @@ public PhpClientCodegen() {
"__halt_compiler", "abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends", "final", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof", "insteadof", "interface", "isset", "list", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch", "throw", "trait", "try", "unset", "use", "var", "while", "xor")
);

additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("modelPackage", modelPackage);
additionalProperties.put("apiPackage", apiPackage);
additionalProperties.put("srcBasePath", srcBasePath);
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);

// ref: http://php.net/manual/en/language.types.intro.php
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
Expand Down Expand Up @@ -86,8 +77,12 @@ public PhpClientCodegen() {
typeMapping.put("object", "object");
typeMapping.put("DateTime", "\\DateTime");

cliOptions.add(new CliOption("packagePath", "main package name for classes"));
cliOptions.add(new CliOption("srcBasePath", "directory directory under packagePath to serve as source root"));
cliOptions.add(new CliOption("invokerPackage", "The main namespace to use for all classes."));
cliOptions.add(new CliOption("packagePath", "The main package name for classes."));
cliOptions.add(new CliOption("srcBasePath", "The directory under packagePath to serve as source root."));
cliOptions.add(new CliOption("composerVendorName", "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name."));
cliOptions.add(new CliOption("composerProjectName", "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name."));
cliOptions.add(new CliOption("artifactVersion", "The version to use in the composer package version field."));
}

public String getPackagePath() {
Expand Down Expand Up @@ -141,23 +136,52 @@ public void processOpts() {

if (additionalProperties.containsKey("packagePath")) {
this.setPackagePath((String) additionalProperties.get("packagePath"));
} else {
additionalProperties.put("packagePath", packagePath);
}

if (additionalProperties.containsKey("srcBasePath")) {
this.setSrcBasePath((String) additionalProperties.get("srcBasePath"));
} else {
additionalProperties.put("srcBasePath", srcBasePath);
}


if (additionalProperties.containsKey("invokerPackage")) {
this.setInvokerPackage((String) additionalProperties.get("invokerPackage"));
} else {
additionalProperties.put("invokerPackage", invokerPackage);
}

if (additionalProperties.containsKey("modelPackage")) {
this.setModelPackage((String) additionalProperties.get("modelPackage"));
} else {
additionalProperties.put("modelPackage", modelPackage);
}

if (additionalProperties.containsKey("apiPackage")) {
this.setApiPackage((String) additionalProperties.get("apiPackage"));
} else {
additionalProperties.put("apiPackage", apiPackage);
}

additionalProperties.put("srcBasePath", srcBasePath);
additionalProperties.put("modelPackage", modelPackage);
additionalProperties.put("apiPackage", apiPackage);

if (additionalProperties.containsKey("composerProjectName")) {
this.setComposerProjectName((String) additionalProperties.get("composerProjectName"));
} else {
additionalProperties.put("composerProjectName", composerProjectName);
}

if (additionalProperties.containsKey("composerVendorName")) {
this.setComposerVendorName((String) additionalProperties.get("composerVendorName"));
} else {
additionalProperties.put("composerVendorName", composerVendorName);
}

if (additionalProperties.containsKey("artifactVersion")) {
this.setArtifactVersion((String) additionalProperties.get("artifactVersion"));
} else {
additionalProperties.put("artifactVersion", artifactVersion);
}

additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));

supportingFiles.add(new SupportingFile("configuration.mustache", toPackagePath(invokerPackage, srcBasePath), "Configuration.php"));
Expand Down Expand Up @@ -235,6 +259,10 @@ public String toDefaultValue(Property p) {
public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}

public void setArtifactVersion(String artifactVersion) {
this.artifactVersion = artifactVersion;
}

public void setPackagePath(String packagePath) {
this.packagePath = packagePath;
Expand All @@ -243,6 +271,14 @@ public void setPackagePath(String packagePath) {
public void setSrcBasePath(String srcBasePath) {
this.srcBasePath = srcBasePath;
}

private void setComposerVendorName(String composerVendorName) {
this.composerVendorName = composerVendorName;
}

public void setComposerProjectName(String composerProjectName) {
this.composerProjectName = composerProjectName;
}

@Override
public String toVarName(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "{{groupId}}/{{artifactId}}",{{#artifactVersion}}
"name": "{{composerVendorName}}/{{composerProjectName}}",{{#artifactVersion}}
"version": "{{artifactVersion}}",{{/artifactVersion}}
"description": "{{description}}",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/php/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
//$api_client->getConfig()->addDefaultHeader("test1", "value1");
//$pet_api = new Swagger\Client\PetAPI($api_client);
$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api = new Swagger\Client\Api\PetApi();
$pet_api->getApiClient()->getConfig()->setTempFolderPath('/var/tmp/php/');
// test default header
//$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903");
Expand All @@ -47,7 +47,7 @@
$new_pet->setTags(array($tag));
$new_pet->setCategory($category);

$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api = new Swagger\Client\Api\PetApi();
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);

Expand Down