diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index 7ba928ce1848..2ef85004c4b1 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -23,6 +23,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use {{invokerPackage}}\ApiException; use {{invokerPackage}}\Configuration; use {{invokerPackage}}\HeaderSelector; @@ -122,9 +123,9 @@ use {{invokerPackage}}\ObjectSerializer; $request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -236,7 +237,7 @@ use {{invokerPackage}}\ObjectSerializer; $request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { {{#returnType}} @@ -487,5 +488,23 @@ use {{invokerPackage}}\ObjectSerializer; } {{/operation}} + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } {{/operations}} diff --git a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/Api/FakeApi.php b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/Api/FakeApi.php index 16ad474af261..bf65cbac5d00 100644 --- a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/Api/FakeApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -113,9 +114,9 @@ class FakeApi $request = $this->testCodeInjectEndRnNRRequest($test_code_inject____end____rn_n_r); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -184,7 +185,7 @@ class FakeApi $request = $this->testCodeInjectEndRnNRRequest($test_code_inject____end____rn_n_r); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -294,4 +295,22 @@ class FakeApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php index a5980f76cc21..a56487fc0629 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/AnotherFakeApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -114,9 +115,9 @@ class AnotherFakeApi $request = $this->testSpecialTagsRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -207,7 +208,7 @@ class AnotherFakeApi $request = $this->testSpecialTagsRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -336,4 +337,22 @@ class AnotherFakeApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php index 61d4e4a3095a..4df01fca5032 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -110,9 +111,9 @@ class FakeApi $request = $this->fakeOuterBooleanSerializeRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -203,7 +204,7 @@ class FakeApi $request = $this->fakeOuterBooleanSerializeRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -356,9 +357,9 @@ class FakeApi $request = $this->fakeOuterCompositeSerializeRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -449,7 +450,7 @@ class FakeApi $request = $this->fakeOuterCompositeSerializeRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -602,9 +603,9 @@ class FakeApi $request = $this->fakeOuterNumberSerializeRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -695,7 +696,7 @@ class FakeApi $request = $this->fakeOuterNumberSerializeRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -848,9 +849,9 @@ class FakeApi $request = $this->fakeOuterStringSerializeRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -941,7 +942,7 @@ class FakeApi $request = $this->fakeOuterStringSerializeRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1098,9 +1099,9 @@ class FakeApi $request = $this->testClientModelRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1191,7 +1192,7 @@ class FakeApi $request = $this->testClientModelRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1379,9 +1380,9 @@ class FakeApi $request = $this->testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer, $int32, $int64, $float, $string, $binary, $date, $date_time, $password, $callback); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1476,7 +1477,7 @@ class FakeApi $request = $this->testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer, $int32, $int64, $float, $string, $binary, $date, $date_time, $password, $callback); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -1773,9 +1774,9 @@ class FakeApi $request = $this->testEnumParametersRequest($enum_form_string_array, $enum_form_string, $enum_header_string_array, $enum_header_string, $enum_query_string_array, $enum_query_string, $enum_query_integer, $enum_query_double); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1858,7 +1859,7 @@ class FakeApi $request = $this->testEnumParametersRequest($enum_form_string_array, $enum_form_string, $enum_header_string_array, $enum_header_string, $enum_query_string_array, $enum_query_string, $enum_query_integer, $enum_query_double); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -2042,9 +2043,9 @@ class FakeApi $request = $this->testInlineAdditionalPropertiesRequest($param); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -2113,7 +2114,7 @@ class FakeApi $request = $this->testInlineAdditionalPropertiesRequest($param); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -2263,9 +2264,9 @@ class FakeApi $request = $this->testJsonFormDataRequest($param, $param2); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -2336,7 +2337,7 @@ class FakeApi $request = $this->testJsonFormDataRequest($param, $param2); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -2463,4 +2464,22 @@ class FakeApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php index 3e306d423a3e..621058189636 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/FakeClassnameTags123Api.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -114,9 +115,9 @@ class FakeClassnameTags123Api $request = $this->testClassnameRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -207,7 +208,7 @@ class FakeClassnameTags123Api $request = $this->testClassnameRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -341,4 +342,22 @@ class FakeClassnameTags123Api ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index 313e0f2882d3..80f41fdf971d 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -113,9 +114,9 @@ class PetApi $request = $this->addPetRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -184,7 +185,7 @@ class PetApi $request = $this->addPetRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -338,9 +339,9 @@ class PetApi $request = $this->deletePetRequest($pet_id, $api_key); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -411,7 +412,7 @@ class PetApi $request = $this->deletePetRequest($pet_id, $api_key); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -574,9 +575,9 @@ class PetApi $request = $this->findPetsByStatusRequest($status); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -667,7 +668,7 @@ class PetApi $request = $this->findPetsByStatusRequest($status); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -838,9 +839,9 @@ class PetApi $request = $this->findPetsByTagsRequest($tags); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -931,7 +932,7 @@ class PetApi $request = $this->findPetsByTagsRequest($tags); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1102,9 +1103,9 @@ class PetApi $request = $this->getPetByIdRequest($pet_id); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1195,7 +1196,7 @@ class PetApi $request = $this->getPetByIdRequest($pet_id); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1367,9 +1368,9 @@ class PetApi $request = $this->updatePetRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1438,7 +1439,7 @@ class PetApi $request = $this->updatePetRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -1594,9 +1595,9 @@ class PetApi $request = $this->updatePetWithFormRequest($pet_id, $name, $status); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1669,7 +1670,7 @@ class PetApi $request = $this->updatePetWithFormRequest($pet_id, $name, $status); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -1841,9 +1842,9 @@ class PetApi $request = $this->uploadFileRequest($pet_id, $additional_metadata, $file); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1938,7 +1939,7 @@ class PetApi $request = $this->uploadFileRequest($pet_id, $additional_metadata, $file); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -2087,4 +2088,22 @@ class PetApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php index 9424f62693e5..175f939a7e6b 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -113,9 +114,9 @@ class StoreApi $request = $this->deleteOrderRequest($order_id); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -184,7 +185,7 @@ class StoreApi $request = $this->deleteOrderRequest($order_id); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -336,9 +337,9 @@ class StoreApi $request = $this->getInventoryRequest(); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -427,7 +428,7 @@ class StoreApi $request = $this->getInventoryRequest(); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -585,9 +586,9 @@ class StoreApi $request = $this->getOrderByIdRequest($order_id); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -678,7 +679,7 @@ class StoreApi $request = $this->getOrderByIdRequest($order_id); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -853,9 +854,9 @@ class StoreApi $request = $this->placeOrderRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -946,7 +947,7 @@ class StoreApi $request = $this->placeOrderRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1075,4 +1076,22 @@ class StoreApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php index ce31171cef12..523da183ae65 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/UserApi.php @@ -32,6 +32,7 @@ use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use GuzzleHttp\RequestOptions; use Swagger\Client\ApiException; use Swagger\Client\Configuration; use Swagger\Client\HeaderSelector; @@ -113,9 +114,9 @@ class UserApi $request = $this->createUserRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -184,7 +185,7 @@ class UserApi $request = $this->createUserRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -332,9 +333,9 @@ class UserApi $request = $this->createUsersWithArrayInputRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -403,7 +404,7 @@ class UserApi $request = $this->createUsersWithArrayInputRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -551,9 +552,9 @@ class UserApi $request = $this->createUsersWithListInputRequest($body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -622,7 +623,7 @@ class UserApi $request = $this->createUsersWithListInputRequest($body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -770,9 +771,9 @@ class UserApi $request = $this->deleteUserRequest($username); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -841,7 +842,7 @@ class UserApi $request = $this->deleteUserRequest($username); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -995,9 +996,9 @@ class UserApi $request = $this->getUserByNameRequest($username); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1088,7 +1089,7 @@ class UserApi $request = $this->getUserByNameRequest($username); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1258,9 +1259,9 @@ class UserApi $request = $this->loginUserRequest($username, $password); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1353,7 +1354,7 @@ class UserApi $request = $this->loginUserRequest($username, $password); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { $responseBody = $response->getBody(); @@ -1525,9 +1526,9 @@ class UserApi $request = $this->logoutUserRequest(); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1594,7 +1595,7 @@ class UserApi $request = $this->logoutUserRequest(); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -1734,9 +1735,9 @@ class UserApi $request = $this->updateUserRequest($username, $body); try { - + $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request); + $response = $this->client->send($request, $options); } catch (RequestException $e) { throw new ApiException( "[{$e->getCode()}] {$e->getMessage()}", @@ -1807,7 +1808,7 @@ class UserApi $request = $this->updateUserRequest($username, $body); return $this->client - ->sendAsync($request) + ->sendAsync($request, $this->createHttpClientOption()) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; @@ -1937,4 +1938,22 @@ class UserApi ); } + /** + * Create http client option + * + * @throws \RuntimeException on file opening failure + * @return array of http client options + */ + protected function createHttpClientOption() + { + $options = []; + if ($this->config->getDebug()) { + $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); + if (!$options[RequestOptions::DEBUG]) { + throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); + } + } + + return $options; + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/DebugTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/DebugTest.php new file mode 100644 index 000000000000..6eb9dacaffeb --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/tests/DebugTest.php @@ -0,0 +1,26 @@ +expectOutputRegex('#GET /v2/pet/1 HTTP/1.1#'); + + $config = new Configuration(); + $config->setDebug(true); + $api = new Api\PetApi(null, $config); + $api->getPetById(1); + } + + public function testEnableDebugOutputAsync() + { + $this->expectOutputRegex('#GET /v2/pet/1 HTTP/1.1#'); + + $config = new Configuration(); + $config->setDebug(true); + $api = new Api\PetApi(null, $config); + $promise = $api->getPetByIdAsync(1); + $promise->wait(); + } +}