File tree Expand file tree Collapse file tree 7 files changed +85
-11
lines changed
modules/swagger-codegen/src/main/resources/php
samples/client/petstore/php Expand file tree Collapse file tree 7 files changed +85
-11
lines changed Original file line number Diff line number Diff line change @@ -25,8 +25,17 @@ namespace {{invokerPackage}};
25
25
{ {#operations} }
26
26
class { {classname} } {
27
27
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
+ }
30
39
}
31
40
32
41
{ {#operation} }
Original file line number Diff line number Diff line change @@ -31,6 +31,16 @@ class Configuration {
31
31
public static $username = ' ' ;
32
32
public static $password = ' ' ;
33
33
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
+ }
34
44
35
45
}
36
46
Original file line number Diff line number Diff line change @@ -31,6 +31,16 @@ class Configuration {
31
31
public static $ username = '' ;
32
32
public static $ password = '' ;
33
33
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
+ }
34
44
35
45
}
36
46
Original file line number Diff line number Diff line change 24
24
25
25
class PetApi {
26
26
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
+ }
29
39
}
30
40
31
41
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
+
32
58
/**
33
59
* updatePet
34
60
*
Original file line number Diff line number Diff line change 24
24
25
25
class StoreApi {
26
26
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
+ }
29
38
}
30
39
31
40
Original file line number Diff line number Diff line change 24
24
25
25
class UserApi {
26
26
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
+ }
29
38
}
30
39
31
40
Original file line number Diff line number Diff line change 3
3
require_once ('SwaggerClient-php/SwaggerClient.php ' );
4
4
5
5
// 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");
8
8
9
9
$ petId = 10005 ; // ID of pet that needs to be fetched
10
10
try {
11
- $ pet_api = new SwaggerClient \PetAPI ($ api_client );
11
+ //$pet_api = new SwaggerClient\PetAPI($api_client);
12
+ $ pet_api = new SwaggerClient \PetAPI ();
12
13
// return Pet (model)
13
14
$ response = $ pet_api ->getPetById ($ petId );
14
15
var_dump ($ response );
You can’t perform that action at this time.
0 commit comments