added php generator

This commit is contained in:
Tony Tam 2014-12-24 10:19:42 -08:00
parent 20c21cb2f7
commit a921278650
5 changed files with 289 additions and 56 deletions

View File

@ -479,12 +479,17 @@ public class DefaultCodegen {
property.containerType = "array";
ArrayProperty ap = (ArrayProperty) p;
CodegenProperty cp = fromProperty("inner", ap.getItems());
property.baseType = getSwaggerType(p);
if(!languageSpecificPrimitives.contains(cp.baseType))
property.complexType = cp.baseType;
else
property.isPrimitiveType = true;
if(cp == null) {
System.out.println("skipping invalid property:");
Json.prettyPrint(p);
}
else {
property.baseType = getSwaggerType(p);
if(!languageSpecificPrimitives.contains(cp.baseType))
property.complexType = cp.baseType;
else
property.isPrimitiveType = true;
}
}
else if(p instanceof MapProperty) {
property.isContainer = true;

View File

@ -0,0 +1,241 @@
package com.wordnik.swagger.codegen.languages;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.util.Json;
import com.wordnik.swagger.models.properties.*;
import java.util.*;
import java.io.File;
public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "com.wordnik.client";
protected String groupId = "com.wordnik";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
// protected String sourceFolder = "";
public String getName() {
return "php";
}
public String getHelp() {
return "Generates a PHP client library.";
}
public PhpClientCodegen() {
super();
outputFolder = "generated-code/php";
modelTemplateFiles.put("model.mustache", ".php");
apiTemplateFiles.put("api.mustache", ".php");
templateDir = "php";
reservedWords = new HashSet<String> (
Arrays.asList(
"int")
);
additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);
languageSpecificPrimitives.add("List");
languageSpecificPrimitives.add("String");
supportingFiles.add(new SupportingFile("Swagger.mustache", "", "Swagger.php"));
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
}
@Override
public String apiFileFolder() {
return outputFolder + "/" + apiPackage().replaceAll("\\.", "/");
}
public String modelFileFolder() {
return outputFolder + "/" + modelPackage().replaceAll("\\.", "/");
}
@Override
public String getTypeDeclaration(Property p) {
if(p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">";
}
else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">";
}
return super.getTypeDeclaration(p);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type = null;
if(typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if(languageSpecificPrimitives.contains(type))
return toModelName(type);
}
else
type = swaggerType;
if(type == null)
return null;
return toModelName(type);
}
}
/*
package com.wordnik.swagger.codegen
import com.wordnik.swagger.codegen.model._
import java.io.File
object BasicPHPGenerator extends BasicPHPGenerator {
def main(args: Array[String]) = generateClient(args)
}
class BasicPHPGenerator extends BasicGenerator {
// template used for models
modelTemplateFiles += "model.mustache" -> ".php"
// template used for models
apiTemplateFiles += "api.mustache" -> ".php"
// location of templates
override def templateDir = "php"
// where to write generated code
override def destinationDir = "generated-code/php"
// package for models
override def modelPackage: Option[String] = Some("models")
// package for apis
override def apiPackage: Option[String] = Some("")
// file suffix
override def fileSuffix = ".php"
// reserved words which need special quoting
// These will all be object properties, in which context we don't need
// to worry about escaping them for PHP.
override def reservedWords = Set()
// import/require statements for specific datatypes
override def importMapping = Map()
// response classes
override def processResponseClass(responseClass: String): Option[String] = {
typeMapping.contains(responseClass) match {
case true => Some(typeMapping(responseClass))
case false => {
responseClass match {
case "void" => None
case e: String => {
responseClass.startsWith("List") match {
case true => Some("array")
case false => Some(responseClass)
}
}
}
}
}
}
override def processResponseDeclaration(responseClass: String): Option[String] = {
typeMapping.contains(responseClass) match {
case true => Some(typeMapping(responseClass))
case false => {
responseClass match {
case "void" => None
case e: String => {
responseClass.startsWith("List") match {
case true => {
val responseSubClass = responseClass.dropRight(1).substring(5)
typeMapping.contains(responseSubClass) match {
case true => Some("array[" + typeMapping(responseSubClass) + "]")
case false => Some("array[" + responseSubClass + "]")
}
}
case false => Some(responseClass)
}
}
}
}
}
}
override def typeMapping = Map(
"string" -> "string",
"str" -> "string",
"int" -> "int",
"float" -> "float",
"long" -> "int",
"double" -> "float",
"Array" -> "array",
"boolean" -> "bool",
"Date" -> "DateTime"
)
override def toDeclaredType(dt: String): String = {
val declaredType = typeMapping.getOrElse(dt, dt)
declaredType.startsWith("Array") match {
case true => {
val innerType = dt.dropRight(1).substring(6)
typeMapping.contains(innerType) match {
case true => "array[" + typeMapping(innerType) + "]"
case false => "array[" + innerType + "]"
}
}
case _ => declaredType
}
}
override def toDeclaration(obj: ModelProperty) = {
var declaredType = toDeclaredType(obj.`type`)
declaredType match {
case "Array" => declaredType = "array"
case e: String => {
e
}
}
val defaultValue = toDefaultValue(declaredType, obj)
declaredType match {
case "array" => {
val inner = {
obj.items match {
case Some(items) => items.ref.getOrElse(items.`type`)
case _ => {
println("failed on " + declaredType + ", " + obj)
throw new Exception("no inner type defined")
}
}
}
declaredType += "[" + toDeclaredType(inner) + "]"
"array"
}
case _ =>
}
(declaredType, defaultValue)
}
// supporting classes
override def supportingFiles = List(
("Swagger.mustache", destinationDir + File.separator + apiPackage.get,
"Swagger.php")
)
}
*/

View File

@ -7,4 +7,5 @@ com.wordnik.swagger.codegen.languages.ScalatraServerCodegen
com.wordnik.swagger.codegen.languages.StaticDocCodegen
com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
com.wordnik.swagger.codegen.languages.SwaggerGenerator
com.wordnik.swagger.codegen.languages.TizenClientCodegen
com.wordnik.swagger.codegen.languages.TizenClientCodegen
com.wordnik.swagger.codegen.languages.PhpClientCodegen

View File

@ -1,6 +1,6 @@
<?php
/**
* Copyright 2011 Wordnik, Inc.
* Copyright 2014 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,18 +28,17 @@ class {{classname}} {
{{#operation}}
/**
* {{nickname}}
* {{summary}}
{{#allParams}}
* {{paramName}}, {{dataType}}: {{description}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
{{newline}}
{{/allParams}}
* @return {{returnType}}
* {{{nickname}}}
*
* {{{summary}}}
{{#allParams}}* {{paramName}}, {{dataType}}: {{description}} {{^optional}}(required){{/optional}}{{#optional}}(optional){{/optional}}
* {{/allParams}}
* @return {{{returnType}}}
*/
public function {{nickname}}({{#allParams}}${{paramName}}{{#optional}}=null{{/optional}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
//parse inputs
// parse inputs
$resourcePath = "{{path}}";
$resourcePath = str_replace("{format}", "json", $resourcePath);
$method = "{{httpMethod}}";
@ -48,45 +47,38 @@ class {{classname}} {
$headerParams['Accept'] = '{{#produces}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/produces}}';
$headerParams['Content-Type'] = '{{#consumes}}{{mediaType}}{{#hasMore}},{{/hasMore}}{{/consumes}}';
{{#queryParams}}
if(${{paramName}} != null) {
{{#queryParams}}// query params
if(${{paramName}} != null) {
$queryParams['{{paramName}}'] = $this->apiClient->toQueryValue(${{paramName}});
}
{{/queryParams}}
{{#headerParams}}
if(${{paramName}} != null) {
}{{/queryParams}}
{{#headerParams}}// header params
if(${{paramName}} != null) {
$headerParams['{{paramName}}'] = $this->apiClient->toHeaderValue(${{paramName}});
}
{{/headerParams}}
{{#pathParams}}
if(${{paramName}} != null) {
}{{/headerParams}}
{{#pathParams}}// path params
if(${{paramName}} != null) {
$resourcePath = str_replace("{" . "{{paramName}}" . "}",
$this->apiClient->toPathValue(${{paramName}}), $resourcePath);
}
{{/pathParams}}
//make the API Call
if (! isset($body)) {
$body = null;
}{{/pathParams}}
{{#bodyParams}}// body params
$body = null;
if (isset(${{paramName}})) {
$body = ${{paramName}};
}
{{/bodyParams}}
// make the API Call
$response = $this->apiClient->callAPI($resourcePath, $method,
$queryParams, $body,
$headerParams);
{{#returnType}}
if(! $response){
return null;
}
{{#returnType}}if(! $response) {
return null;
}
$responseObject = $this->apiClient->deserialize($response,
'{{returnType}}');
return $responseObject;
{{/returnType}}
return $responseObject;{{/returnType}}
}
{{/operation}}
{{newline}}

View File

@ -1,6 +1,6 @@
<?php
/**
* Copyright 2011 Wordnik, Inc.
* Copyright 2014 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,21 +27,15 @@
class {{classname}} {
static $swaggerTypes = array(
{{#vars}}
'{{name}}' => '{{{datatype}}}'{{#hasMore}},
{{/hasMore}}
{{/vars}}{{newline}}
{{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
);
{{#vars}}
{{#description}}
{{#vars}}{{#description}}
/**
* {{description}}
*/
{{/description}}
public ${{name}}; // {{{datatype}}}
{{/vars}}
* {{{description}}}
*/{{/description}}
public ${{name}}; /* {{{datatype}}} */{{/vars}}
}
{{/model}}
{{/models}}