Skip to content

Commit 612abf1

Browse files
committed
api client as instance (not static class)
1 parent 2b7bbd9 commit 612abf1

File tree

7 files changed

+85
-11
lines changed

7 files changed

+85
-11
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ namespace {{invokerPackage}};
2525
{{#operations}}
2626
class {{classname}} {
2727
28-
function __construct($apiClient) {
29-
$this->apiClient = $apiClient;
28+
function __construct($apiClient = null) {
29+
if (null === $apiClient) {
30+
if (Configuration::$apiClient === null) {
31+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
32+
$this->apiClient = Configuration::$apiClient;
33+
}
34+
else
35+
$this->apiClient = Configuration::$apiClient; // use the default one
36+
} else {
37+
$this->apiClient = $apiClient; // use the one provided by the user
38+
}
3039
}
3140

3241
{{#operation}}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Configuration {
3131
public static $username = '';
3232
public static $password = '';
3333
34+
// an instance of APIClient
35+
public static $apiClient;
36+
37+
/*
38+
* manually initalize API client
39+
*/
40+
public static function init() {
41+
if (self::$apiClient === null)
42+
self::$apiClient = new APIClient();
43+
}
3444

3545
}
3646

samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Configuration {
3131
public static $username = '';
3232
public static $password = '';
3333

34+
// an instance of APIClient
35+
public static $apiClient;
36+
37+
/*
38+
* manually initalize API client
39+
*/
40+
public static function init() {
41+
if (self::$apiClient === null)
42+
self::$apiClient = new APIClient();
43+
}
3444

3545
}
3646

samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,37 @@
2424

2525
class PetApi {
2626

27-
function __construct($apiClient) {
28-
$this->apiClient = $apiClient;
27+
28+
function __construct($apiClient = null) {
29+
if (null === $apiClient) {
30+
if (Configuration::$apiClient === null) {
31+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
32+
$this->apiClient = Configuration::$apiClient;
33+
}
34+
else
35+
$this->apiClient = Configuration::$apiClient; // use the default one
36+
} else {
37+
$this->apiClient = $apiClient; // use the one provided by the user
38+
}
2939
}
3040

3141

42+
private $apiClient;
43+
44+
/**
45+
* get the API client
46+
*/
47+
public function getApiClient() {
48+
return $this->apiClient;
49+
}
50+
51+
/**
52+
* set the API client
53+
*/
54+
public function getApiClient($apiClient) {
55+
$this->apiClient = $apiClient;
56+
}
57+
3258
/**
3359
* updatePet
3460
*

samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@
2424

2525
class StoreApi {
2626

27-
function __construct($apiClient) {
28-
$this->apiClient = $apiClient;
27+
function __construct($apiClient = null) {
28+
if (null === $apiClient) {
29+
if (Configuration::$apiClient === null) {
30+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
$this->apiClient = Configuration::$apiClient;
32+
}
33+
else
34+
$this->apiClient = Configuration::$apiClient; // use the default one
35+
} else {
36+
$this->apiClient = $apiClient; // use the one provided by the user
37+
}
2938
}
3039

3140

samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@
2424

2525
class UserApi {
2626

27-
function __construct($apiClient) {
28-
$this->apiClient = $apiClient;
27+
function __construct($apiClient = null) {
28+
if (null === $apiClient) {
29+
if (Configuration::$apiClient === null) {
30+
Configuration::$apiClient = new APIClient(); // create a new API client if not present
31+
$this->apiClient = Configuration::$apiClient;
32+
}
33+
else
34+
$this->apiClient = Configuration::$apiClient; // use the default one
35+
} else {
36+
$this->apiClient = $apiClient; // use the one provided by the user
37+
}
2938
}
3039

3140

samples/client/petstore/php/test.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
require_once('SwaggerClient-php/SwaggerClient.php');
44

55
// initialize the API client
6-
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
7-
$api_client->addDefaultHeader("test1", "value1");
6+
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
7+
//$api_client->addDefaultHeader("test1", "value1");
88

99
$petId = 10005; // ID of pet that needs to be fetched
1010
try {
11-
$pet_api = new SwaggerClient\PetAPI($api_client);
11+
//$pet_api = new SwaggerClient\PetAPI($api_client);
12+
$pet_api = new SwaggerClient\PetAPI();
1213
// return Pet (model)
1314
$response = $pet_api->getPetById($petId);
1415
var_dump($response);

0 commit comments

Comments
 (0)