diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java index 2f86153bfb7..734e1612849 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java @@ -27,7 +27,14 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { public PhpClientCodegen() { super(); - modelPackage = "models"; + + //TODO determine hte package name from host name + invokerPackage = camelize("SwaggerPetstore"); + + String packagePath = invokerPackage + "-php"; + + modelPackage = packagePath + "/lib/models"; + apiPackage = packagePath + "/lib"; outputFolder = "generated-code/php"; modelTemplateFiles.put("model.mustache", ".php"); apiTemplateFiles.put("api.mustache", ".php"); @@ -59,7 +66,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("List", "array"); typeMapping.put("map", "map"); - supportingFiles.add(new SupportingFile("Swagger.mustache", "", "Swagger.php")); + supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json")); + supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php")); + supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php")); + supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php")); } @Override diff --git a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache similarity index 89% rename from modules/swagger-codegen/src/main/resources/php/Swagger.mustache rename to modules/swagger-codegen/src/main/resources/php/APIClient.mustache index 3d6b77d819e..4e60cf6d338 100644 --- a/modules/swagger-codegen/src/main/resources/php/Swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -15,24 +15,11 @@ * limitations under the License. */ -/* Autoload the model definition files */ -/** - * - * @param string $className the class to attempt to load - */ -function swagger_autoloader($className) { - $currentDir = dirname(__FILE__); - if (file_exists($currentDir . '/' . $className . '.php')) { - include $currentDir . '/' . $className . '.php'; - } elseif (file_exists($currentDir . '/models/' . $className . '.php')) { - include $currentDir . '/models/' . $className . '.php'; - } -} -spl_autoload_register('swagger_autoloader'); - +namespace {{invokerPackage}}; class APIClient { + public static $PATCH = "PATCH"; public static $POST = "POST"; public static $GET = "GET"; public static $PUT = "PUT"; @@ -199,7 +186,7 @@ class APIClient { * @return string the serialized object */ public static function toPathValue($value) { - return rawurlencode(toString($value)); + return rawurlencode(self::toString($value)); } /** @@ -214,7 +201,7 @@ class APIClient { if (is_array($object)) { return implode(',', $object); } else { - return toString($object); + return self::toString($object); } } @@ -226,7 +213,7 @@ class APIClient { * @return string the header string */ public static function toHeaderValue($value) { - return toString($value); + return self::toString($value); } /** @@ -237,7 +224,7 @@ class APIClient { * @return string the form string */ public static function toFormValue($value) { - return toString($value); + return self::toString($value); } /** @@ -292,6 +279,7 @@ class APIClient { settype($data, $class); $deserialized = $data; } else { + $class = "{{invokerPackage}}\\models\\".$class; $instance = new $class(); foreach ($instance::$swaggerTypes as $property => $type) { if (isset($data->$property)) { @@ -307,20 +295,3 @@ class APIClient { } -class APIClientException extends Exception { - protected $response, $response_info; - - public function __construct($message="", $code=0, $response_info=null, $response=null) { - parent::__construct($message, $code); - $this->response_info = $response_info; - $this->response = $response; - } - - public function getResponse() { - return $this->response; - } - - public function getResponseInfo() { - return $this->response_info; - } -} diff --git a/modules/swagger-codegen/src/main/resources/php/APIClientException.mustache b/modules/swagger-codegen/src/main/resources/php/APIClientException.mustache new file mode 100644 index 00000000000..3cf74d69288 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/php/APIClientException.mustache @@ -0,0 +1,38 @@ +response_info = $response_info; + $this->response = $response; + } + + public function getResponse() { + return $this->response; + } + + public function getResponseInfo() { + return $this->response_info; + } +} diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index bf059063ffd..ca1bf24438d 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -19,6 +19,9 @@ * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. */ + +namespace {{invokerPackage}}; + {{#operations}} class {{classname}} { diff --git a/modules/swagger-codegen/src/main/resources/php/composer.mustache b/modules/swagger-codegen/src/main/resources/php/composer.mustache new file mode 100644 index 00000000000..fb47a865834 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/php/composer.mustache @@ -0,0 +1,32 @@ +{ + "name": "{{invokerPackage}}/{{invokerPackage}}-php", + "description": "{{description}}", + "keywords": [ + "swagger", + "php", + "sdk", + "api" + ], + "homepage": "http://swagger.io", + "license": "Apache v2", + "authors": [ + { + "name": "Swagger and contributors", + "homepage": "https://github.com/swagger-api/swagger-codegen" + } + ], + "require": { + "php": ">=5.3.3", + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*" + }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "satooshi/php-coveralls": "~0.6.1", + "squizlabs/php_codesniffer": "~2.0" + }, + "autoload": { + "psr-4": { "{{invokerPackage}}\\" : "lib/" } + } +} diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index 4540f973633..614334353de 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -15,14 +15,18 @@ * limitations under the License. */ +{{#models}} +{{#model}} /** - * $model.description$ + * {{description}} * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ -{{#models}} -{{#model}} + +namespace {{invokerPackage}}\models; + +use \ArrayAccess; class {{classname}} implements ArrayAccess { static $swaggerTypes = array( @@ -41,7 +45,7 @@ class {{classname}} implements ArrayAccess { */{{/description}} public ${{name}}; /* {{{datatype}}} */{{/vars}} - public function __construct(array $data) { + public function __construct(array $data = null) { {{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}} {{/hasMore}}{{/vars}} } diff --git a/modules/swagger-codegen/src/main/resources/php/require.mustache b/modules/swagger-codegen/src/main/resources/php/require.mustache new file mode 100644 index 00000000000..058aa169504 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/php/require.mustache @@ -0,0 +1,16 @@ + diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore.json b/modules/swagger-codegen/src/test/resources/2_0/petstore.json index b4678fc3ab9..37863375b89 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore.json +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore.json @@ -848,6 +848,7 @@ } }, "Category": { + "description" : "testing category description", "properties": { "id": { "type": "integer", diff --git a/samples/client/petstore/php/SwaggerPetstore-php/SwaggerPetstore.php b/samples/client/petstore/php/SwaggerPetstore-php/SwaggerPetstore.php new file mode 100644 index 00000000000..058aa169504 --- /dev/null +++ b/samples/client/petstore/php/SwaggerPetstore-php/SwaggerPetstore.php @@ -0,0 +1,16 @@ + diff --git a/samples/client/petstore/php/SwaggerPetstore-php/composer.json b/samples/client/petstore/php/SwaggerPetstore-php/composer.json new file mode 100644 index 00000000000..7109d7a8790 --- /dev/null +++ b/samples/client/petstore/php/SwaggerPetstore-php/composer.json @@ -0,0 +1,32 @@ +{ + "name": "SwaggerPetstore/SwaggerPetstore-php", + "description": "", + "keywords": [ + "swagger", + "php", + "sdk", + "api" + ], + "homepage": "http://swagger.io", + "license": "Apache v2", + "authors": [ + { + "name": "Swagger and contributors", + "homepage": "https://github.com/swagger-api/swagger-codegen" + } + ], + "require": { + "php": ">=5.3.3", + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*" + }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "satooshi/php-coveralls": "~0.6.1", + "squizlabs/php_codesniffer": "~2.0" + }, + "autoload": { + "psr-4": { "SwaggerPetstore\\" : "lib/" } + } +} diff --git a/samples/client/petstore/php/Swagger.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/APIClient.php similarity index 89% rename from samples/client/petstore/php/Swagger.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/APIClient.php index 3d6b77d819e..1f72299756f 100644 --- a/samples/client/petstore/php/Swagger.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/APIClient.php @@ -15,24 +15,11 @@ * limitations under the License. */ -/* Autoload the model definition files */ -/** - * - * @param string $className the class to attempt to load - */ -function swagger_autoloader($className) { - $currentDir = dirname(__FILE__); - if (file_exists($currentDir . '/' . $className . '.php')) { - include $currentDir . '/' . $className . '.php'; - } elseif (file_exists($currentDir . '/models/' . $className . '.php')) { - include $currentDir . '/models/' . $className . '.php'; - } -} -spl_autoload_register('swagger_autoloader'); - +namespace SwaggerPetstore; class APIClient { + public static $PATCH = "PATCH"; public static $POST = "POST"; public static $GET = "GET"; public static $PUT = "PUT"; @@ -199,7 +186,7 @@ class APIClient { * @return string the serialized object */ public static function toPathValue($value) { - return rawurlencode(toString($value)); + return rawurlencode(self::toString($value)); } /** @@ -214,7 +201,7 @@ class APIClient { if (is_array($object)) { return implode(',', $object); } else { - return toString($object); + return self::toString($object); } } @@ -226,7 +213,7 @@ class APIClient { * @return string the header string */ public static function toHeaderValue($value) { - return toString($value); + return self::toString($value); } /** @@ -237,7 +224,7 @@ class APIClient { * @return string the form string */ public static function toFormValue($value) { - return toString($value); + return self::toString($value); } /** @@ -292,6 +279,7 @@ class APIClient { settype($data, $class); $deserialized = $data; } else { + $class = "SwaggerPetstore\\models\\".$class; $instance = new $class(); foreach ($instance::$swaggerTypes as $property => $type) { if (isset($data->$property)) { @@ -307,20 +295,3 @@ class APIClient { } -class APIClientException extends Exception { - protected $response, $response_info; - - public function __construct($message="", $code=0, $response_info=null, $response=null) { - parent::__construct($message, $code); - $this->response_info = $response_info; - $this->response = $response; - } - - public function getResponse() { - return $this->response; - } - - public function getResponseInfo() { - return $this->response_info; - } -} diff --git a/samples/client/petstore/php/SwaggerPetstore-php/lib/APIClientException.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/APIClientException.php new file mode 100644 index 00000000000..8f083caf6f9 --- /dev/null +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/APIClientException.php @@ -0,0 +1,38 @@ +response_info = $response_info; + $this->response = $response; + } + + public function getResponse() { + return $this->response; + } + + public function getResponseInfo() { + return $this->response_info; + } +} diff --git a/samples/client/petstore/php/PetApi.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/PetApi.php similarity index 99% rename from samples/client/petstore/php/PetApi.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/PetApi.php index 4cc555eeefc..7a885b45b82 100644 --- a/samples/client/petstore/php/PetApi.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/PetApi.php @@ -19,6 +19,9 @@ * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. */ + +namespace SwaggerPetstore; + class PetApi { function __construct($apiClient) { diff --git a/samples/client/petstore/php/StoreApi.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/StoreApi.php similarity index 99% rename from samples/client/petstore/php/StoreApi.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/StoreApi.php index 169766a8e76..90fb0f4d4f8 100644 --- a/samples/client/petstore/php/StoreApi.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/StoreApi.php @@ -19,6 +19,9 @@ * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. */ + +namespace SwaggerPetstore; + class StoreApi { function __construct($apiClient) { diff --git a/samples/client/petstore/php/UserApi.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/UserApi.php similarity index 99% rename from samples/client/petstore/php/UserApi.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/UserApi.php index 4d5c3de954f..f6952074f7b 100644 --- a/samples/client/petstore/php/UserApi.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/UserApi.php @@ -19,6 +19,9 @@ * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. */ + +namespace SwaggerPetstore; + class UserApi { function __construct($apiClient) { diff --git a/samples/client/petstore/php/models/Category.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Category.php similarity index 90% rename from samples/client/petstore/php/models/Category.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/models/Category.php index 98e7ba24800..2bd2410268c 100644 --- a/samples/client/petstore/php/models/Category.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Category.php @@ -16,12 +16,16 @@ */ /** - * $model.description$ + * testing category description * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ +namespace SwaggerPetstore\models; + +use \ArrayAccess; + class Category implements ArrayAccess { static $swaggerTypes = array( 'id' => 'int', @@ -37,7 +41,7 @@ class Category implements ArrayAccess { public $id; /* int */ public $name; /* string */ - public function __construct(array $data) { + public function __construct(array $data = null) { $this->id = $data["id"]; $this->name = $data["name"]; } diff --git a/samples/client/petstore/php/models/Order.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Order.php similarity index 94% rename from samples/client/petstore/php/models/Order.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/models/Order.php index 2770e8bbbdf..87e8c812eb7 100644 --- a/samples/client/petstore/php/models/Order.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Order.php @@ -16,12 +16,16 @@ */ /** - * $model.description$ + * * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ +namespace SwaggerPetstore\models; + +use \ArrayAccess; + class Order implements ArrayAccess { static $swaggerTypes = array( 'id' => 'int', @@ -52,7 +56,7 @@ class Order implements ArrayAccess { public $status; /* string */ public $complete; /* boolean */ - public function __construct(array $data) { + public function __construct(array $data = null) { $this->id = $data["id"]; $this->pet_id = $data["pet_id"]; $this->quantity = $data["quantity"]; diff --git a/samples/client/petstore/php/models/Pet.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Pet.php similarity index 94% rename from samples/client/petstore/php/models/Pet.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/models/Pet.php index eee7fa3784c..eb67904bf13 100644 --- a/samples/client/petstore/php/models/Pet.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Pet.php @@ -16,12 +16,16 @@ */ /** - * $model.description$ + * * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ +namespace SwaggerPetstore\models; + +use \ArrayAccess; + class Pet implements ArrayAccess { static $swaggerTypes = array( 'id' => 'int', @@ -52,7 +56,7 @@ class Pet implements ArrayAccess { */ public $status; /* string */ - public function __construct(array $data) { + public function __construct(array $data = null) { $this->id = $data["id"]; $this->category = $data["category"]; $this->name = $data["name"]; diff --git a/samples/client/petstore/php/models/Tag.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Tag.php similarity index 92% rename from samples/client/petstore/php/models/Tag.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/models/Tag.php index f8efc998df6..a9982317ece 100644 --- a/samples/client/petstore/php/models/Tag.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/Tag.php @@ -16,12 +16,16 @@ */ /** - * $model.description$ + * * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ +namespace SwaggerPetstore\models; + +use \ArrayAccess; + class Tag implements ArrayAccess { static $swaggerTypes = array( 'id' => 'int', @@ -37,7 +41,7 @@ class Tag implements ArrayAccess { public $id; /* int */ public $name; /* string */ - public function __construct(array $data) { + public function __construct(array $data = null) { $this->id = $data["id"]; $this->name = $data["name"]; } diff --git a/samples/client/petstore/php/models/User.php b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/User.php similarity index 95% rename from samples/client/petstore/php/models/User.php rename to samples/client/petstore/php/SwaggerPetstore-php/lib/models/User.php index d6b520eba51..8662f937bc8 100644 --- a/samples/client/petstore/php/models/User.php +++ b/samples/client/petstore/php/SwaggerPetstore-php/lib/models/User.php @@ -16,12 +16,16 @@ */ /** - * $model.description$ + * * * NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. * */ +namespace SwaggerPetstore\models; + +use \ArrayAccess; + class User implements ArrayAccess { static $swaggerTypes = array( 'id' => 'int', @@ -58,7 +62,7 @@ class User implements ArrayAccess { */ public $user_status; /* int */ - public function __construct(array $data) { + public function __construct(array $data = null) { $this->id = $data["id"]; $this->username = $data["username"]; $this->first_name = $data["first_name"]; diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php new file mode 100644 index 00000000000..df10876feb5 --- /dev/null +++ b/samples/client/petstore/php/test.php @@ -0,0 +1,17 @@ +getPetById($petId); + var_dump($response); +} catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; +} + +?>