Skip to content

Commit d55aaff

Browse files
committed
Merge pull request #597 from wing328/php_package
Updated PHP codegen to support packaging via Composer
2 parents b9514a4 + 5c335be commit d55aaff

File tree

20 files changed

+265
-88
lines changed

20 files changed

+265
-88
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ public String getHelp() {
2727

2828
public PhpClientCodegen() {
2929
super();
30-
modelPackage = "models";
30+
31+
//TODO determine hte package name from host name
32+
invokerPackage = camelize("SwaggerPetstore");
33+
34+
String packagePath = invokerPackage + "-php";
35+
36+
modelPackage = packagePath + "/lib/models";
37+
apiPackage = packagePath + "/lib";
3138
outputFolder = "generated-code/php";
3239
modelTemplateFiles.put("model.mustache", ".php");
3340
apiTemplateFiles.put("api.mustache", ".php");
@@ -59,7 +66,10 @@ public PhpClientCodegen() {
5966
typeMapping.put("List", "array");
6067
typeMapping.put("map", "map");
6168

62-
supportingFiles.add(new SupportingFile("Swagger.mustache", "", "Swagger.php"));
69+
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
70+
supportingFiles.add(new SupportingFile("APIClient.mustache", packagePath + "/lib", "APIClient.php"));
71+
supportingFiles.add(new SupportingFile("APIClientException.mustache", packagePath + "/lib", "APIClientException.php"));
72+
supportingFiles.add(new SupportingFile("require.mustache", packagePath, invokerPackage + ".php"));
6373
}
6474

6575
@Override

modules/swagger-codegen/src/main/resources/php/Swagger.mustache renamed to modules/swagger-codegen/src/main/resources/php/APIClient.mustache

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
/* Autoload the model definition files */
19-
/**
20-
*
21-
* @param string $className the class to attempt to load
22-
*/
23-
function swagger_autoloader($className) {
24-
$currentDir = dirname(__FILE__);
25-
if (file_exists($currentDir . '/' . $className . '.php')) {
26-
include $currentDir . '/' . $className . '.php';
27-
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
28-
include $currentDir . '/models/' . $className . '.php';
29-
}
30-
}
31-
spl_autoload_register('swagger_autoloader');
32-
18+
namespace {{invokerPackage}};
3319

3420
class APIClient {
3521
22+
public static $PATCH = "PATCH";
3623
public static $POST = "POST";
3724
public static $GET = "GET";
3825
public static $PUT = "PUT";
@@ -199,7 +186,7 @@ class APIClient {
199186
* @return string the serialized object
200187
*/
201188
public static function toPathValue($value) {
202-
return rawurlencode(toString($value));
189+
return rawurlencode(self::toString($value));
203190
}
204191

205192
/**
@@ -214,7 +201,7 @@ class APIClient {
214201
if (is_array($object)) {
215202
return implode(',', $object);
216203
} else {
217-
return toString($object);
204+
return self::toString($object);
218205
}
219206
}
220207

@@ -226,7 +213,7 @@ class APIClient {
226213
* @return string the header string
227214
*/
228215
public static function toHeaderValue($value) {
229-
return toString($value);
216+
return self::toString($value);
230217
}
231218

232219
/**
@@ -237,7 +224,7 @@ class APIClient {
237224
* @return string the form string
238225
*/
239226
public static function toFormValue($value) {
240-
return toString($value);
227+
return self::toString($value);
241228
}
242229

243230
/**
@@ -292,6 +279,7 @@ class APIClient {
292279
settype($data, $class);
293280
$deserialized = $data;
294281
} else {
282+
$class = "{{invokerPackage}}\\models\\".$class;
295283
$instance = new $class();
296284
foreach ($instance::$swaggerTypes as $property => $type) {
297285
if (isset($data->$property)) {
@@ -307,20 +295,3 @@ class APIClient {
307295

308296
}
309297

310-
class APIClientException extends Exception {
311-
protected $response, $response_info;
312-
313-
public function __construct($message="", $code=0, $response_info=null, $response=null) {
314-
parent::__construct($message, $code);
315-
$this->response_info = $response_info;
316-
$this->response = $response;
317-
}
318-
319-
public function getResponse() {
320-
return $this->response;
321-
}
322-
323-
public function getResponseInfo() {
324-
return $this->response_info;
325-
}
326-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright 2015 Reverb Technologies, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace {{invokerPackage}};
19+
20+
use \Exception;
21+
22+
class APIClientException extends Exception {
23+
protected $response, $response_info;
24+
25+
public function __construct($message="", $code=0, $response_info=null, $response=null) {
26+
parent::__construct($message, $code);
27+
$this->response_info = $response_info;
28+
$this->response = $response;
29+
}
30+
31+
public function getResponse() {
32+
return $this->response;
33+
}
34+
35+
public function getResponseInfo() {
36+
return $this->response_info;
37+
}
38+
}

modules/swagger-codegen/src/main/resources/php/api.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
*
2020
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
2121
*/
22+
23+
namespace {{invokerPackage}};
24+
2225
{{#operations}}
2326
class {{classname}} {
2427
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "{{invokerPackage}}/{{invokerPackage}}-php",
3+
"description": "{{description}}",
4+
"keywords": [
5+
"swagger",
6+
"php",
7+
"sdk",
8+
"api"
9+
],
10+
"homepage": "http://swagger.io",
11+
"license": "Apache v2",
12+
"authors": [
13+
{
14+
"name": "Swagger and contributors",
15+
"homepage": "https://github.com/swagger-api/swagger-codegen"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.3.3",
20+
"ext-curl": "*",
21+
"ext-json": "*",
22+
"ext-mbstring": "*"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "~4.0",
26+
"satooshi/php-coveralls": "~0.6.1",
27+
"squizlabs/php_codesniffer": "~2.0"
28+
},
29+
"autoload": {
30+
"psr-4": { "{{invokerPackage}}\\" : "lib/" }
31+
}
32+
}

modules/swagger-codegen/src/main/resources/php/model.mustache

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
* limitations under the License.
1616
*/
1717

18+
{{#models}}
19+
{{#model}}
1820
/**
19-
* $model.description$
21+
* {{description}}
2022
*
2123
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
2224
*
2325
*/
24-
{{#models}}
25-
{{#model}}
26+
27+
namespace {{invokerPackage}}\models;
28+
29+
use \ArrayAccess;
2630

2731
class {{classname}} implements ArrayAccess {
2832
static $swaggerTypes = array(
@@ -41,7 +45,7 @@ class {{classname}} implements ArrayAccess {
4145
*/{{/description}}
4246
public ${{name}}; /* {{{datatype}}} */{{/vars}}
4347

44-
public function __construct(array $data) {
48+
public function __construct(array $data = null) {
4549
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
4650
{{/hasMore}}{{/vars}}
4751
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
// source code generated by http://restunited.com
3+
// for any feedback/issue, please send to feedback{at}restunited.com
4+
5+
// load models defined for endpoints
6+
foreach (glob(dirname(__FILE__)."/lib/models/*.php") as $filename)
7+
{
8+
require_once $filename;
9+
}
10+
11+
// load classes for accessing the endpoints
12+
foreach (glob(dirname(__FILE__)."/lib/*.php") as $filename)
13+
{
14+
require_once $filename;
15+
}
16+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
// source code generated by http://restunited.com
3+
// for any feedback/issue, please send to feedback{at}restunited.com
4+
5+
// load models defined for endpoints
6+
foreach (glob(dirname(__FILE__)."/lib/models/*.php") as $filename)
7+
{
8+
require_once $filename;
9+
}
10+
11+
// load classes for accessing the endpoints
12+
foreach (glob(dirname(__FILE__)."/lib/*.php") as $filename)
13+
{
14+
require_once $filename;
15+
}
16+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "SwaggerPetstore/SwaggerPetstore-php",
3+
"description": "",
4+
"keywords": [
5+
"swagger",
6+
"php",
7+
"sdk",
8+
"api"
9+
],
10+
"homepage": "http://swagger.io",
11+
"license": "Apache v2",
12+
"authors": [
13+
{
14+
"name": "Swagger and contributors",
15+
"homepage": "https://github.com/swagger-api/swagger-codegen"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.3.3",
20+
"ext-curl": "*",
21+
"ext-json": "*",
22+
"ext-mbstring": "*"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "~4.0",
26+
"satooshi/php-coveralls": "~0.6.1",
27+
"squizlabs/php_codesniffer": "~2.0"
28+
},
29+
"autoload": {
30+
"psr-4": { "SwaggerPetstore\\" : "lib/" }
31+
}
32+
}

samples/client/petstore/php/Swagger.php renamed to samples/client/petstore/php/SwaggerPetstore-php/lib/APIClient.php

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
/* Autoload the model definition files */
19-
/**
20-
*
21-
* @param string $className the class to attempt to load
22-
*/
23-
function swagger_autoloader($className) {
24-
$currentDir = dirname(__FILE__);
25-
if (file_exists($currentDir . '/' . $className . '.php')) {
26-
include $currentDir . '/' . $className . '.php';
27-
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
28-
include $currentDir . '/models/' . $className . '.php';
29-
}
30-
}
31-
spl_autoload_register('swagger_autoloader');
32-
18+
namespace SwaggerPetstore;
3319

3420
class APIClient {
3521

22+
public static $PATCH = "PATCH";
3623
public static $POST = "POST";
3724
public static $GET = "GET";
3825
public static $PUT = "PUT";
@@ -199,7 +186,7 @@ protected function sanitizeForSerialization($data)
199186
* @return string the serialized object
200187
*/
201188
public static function toPathValue($value) {
202-
return rawurlencode(toString($value));
189+
return rawurlencode(self::toString($value));
203190
}
204191

205192
/**
@@ -214,7 +201,7 @@ public static function toQueryValue($object) {
214201
if (is_array($object)) {
215202
return implode(',', $object);
216203
} else {
217-
return toString($object);
204+
return self::toString($object);
218205
}
219206
}
220207

@@ -226,7 +213,7 @@ public static function toQueryValue($object) {
226213
* @return string the header string
227214
*/
228215
public static function toHeaderValue($value) {
229-
return toString($value);
216+
return self::toString($value);
230217
}
231218

232219
/**
@@ -237,7 +224,7 @@ public static function toHeaderValue($value) {
237224
* @return string the form string
238225
*/
239226
public static function toFormValue($value) {
240-
return toString($value);
227+
return self::toString($value);
241228
}
242229

243230
/**
@@ -292,6 +279,7 @@ public static function deserialize($data, $class)
292279
settype($data, $class);
293280
$deserialized = $data;
294281
} else {
282+
$class = "SwaggerPetstore\\models\\".$class;
295283
$instance = new $class();
296284
foreach ($instance::$swaggerTypes as $property => $type) {
297285
if (isset($data->$property)) {
@@ -307,20 +295,3 @@ public static function deserialize($data, $class)
307295

308296
}
309297

310-
class APIClientException extends Exception {
311-
protected $response, $response_info;
312-
313-
public function __construct($message="", $code=0, $response_info=null, $response=null) {
314-
parent::__construct($message, $code);
315-
$this->response_info = $response_info;
316-
$this->response = $response;
317-
}
318-
319-
public function getResponse() {
320-
return $this->response;
321-
}
322-
323-
public function getResponseInfo() {
324-
return $this->response_info;
325-
}
326-
}

0 commit comments

Comments
 (0)