Skip to content

Commit d9423a4

Browse files
authored
[PHP] 6.0.x make php implementation depend on meta packages for http client (#16368)
* add psr-18 support * update doc * fix test
1 parent 2815e6a commit d9423a4

193 files changed

Lines changed: 51269 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/configs/php-psr-18.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: php
2+
outputDir: samples/client/petstore/php/psr-18
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/php/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/php
5+
library: psr-18
6+
nameMappings:
7+
_type: underscore_type
8+
type_: type_with_underscore
9+
type-: type_with_dash
10+
parameterNameMappings:
11+
_type: underscore_type
12+
type_: type_with_underscore

docs/generators/php.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|hideGenerationTimestamp|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |true|
3232
|invokerPackage|The main namespace to use for all classes. e.g. Yay\Pets| |null|
3333
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
34+
|library|HTTP library template (sub-template) to use|<dl><dt>**guzzle**</dt><dd>Guzzle</dd><dt>**psr-18**</dt><dd>psr/http-client-implementation, also known as PSR-18. (beta support)</dd></dl>|guzzle|
3435
|licenseName|The name of the license| |null|
3536
|modelPackage|package for generated models| |null|
3637
|packageName|The main package name for classes. e.g. GeneratedPetstore| |null|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
public class PhpClientCodegen extends AbstractPhpCodegen {
3636
@SuppressWarnings("hiding")
3737
private final Logger LOGGER = LoggerFactory.getLogger(PhpClientCodegen.class);
38+
public static final String GUZZLE = "guzzle";
39+
public static final String PSR18 = "psr-18";
3840

3941
public PhpClientCodegen() {
4042
super();
@@ -82,6 +84,15 @@ public PhpClientCodegen() {
8284

8385
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.ALLOW_UNICODE_IDENTIFIERS_DESC)
8486
.defaultValue(Boolean.TRUE.toString()));
87+
88+
supportedLibraries.put(GUZZLE, "Guzzle");
89+
supportedLibraries.put(PSR18, "psr/http-client-implementation, also known as PSR-18. (beta support)");
90+
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "HTTP library template (sub-template) to use");
91+
libraryOption.setEnum(supportedLibraries);
92+
// set GUZZLE as the default
93+
libraryOption.setDefault(GUZZLE);
94+
cliOptions.add(libraryOption);
95+
setLibrary(GUZZLE);
8596
}
8697

8798
@Override
@@ -114,5 +125,14 @@ public void processOpts() {
114125
supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
115126
supportingFiles.add(new SupportingFile(".php-cs-fixer.dist.php", "", ".php-cs-fixer.dist.php"));
116127
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
128+
129+
if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
130+
this.setLibrary((String) additionalProperties.get(CodegenConstants.LIBRARY));
131+
}
132+
133+
if (PSR18.equals(getLibrary())) {
134+
supportingFiles.add(new SupportingFile("DebugPlugin.mustache", toSrcPath(invokerPackage, srcBasePath), "DebugPlugin.php"));
135+
}
136+
117137
}
118138
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* ApiException
4+
* PHP version 7.2
5+
*
6+
* @category Class
7+
* @package {{invokerPackage}}
8+
* @author OpenAPI Generator team
9+
* @link https://openapi-generator.tech
10+
*/
11+
12+
{{>partial_header}}
13+
/**
14+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
15+
* https://openapi-generator.tech
16+
* Do not edit the class manually.
17+
*/
18+
19+
namespace {{invokerPackage}};
20+
21+
use Exception;
22+
use Http\Client\Exception\RequestException;
23+
use Psr\Http\Message\RequestInterface;
24+
use Psr\Http\Message\ResponseInterface;
25+
26+
/**
27+
* ApiException Class Doc Comment
28+
*
29+
* @category Class
30+
* @package {{invokerPackage}}
31+
* @author OpenAPI Generator team
32+
* @link https://openapi-generator.tech
33+
*/
34+
class ApiException extends RequestException
35+
{
36+
37+
/**
38+
* The HTTP body of the server response either as Json or string.
39+
*
40+
* @var string|null
41+
*/
42+
protected $responseBody;
43+
44+
/**
45+
* The HTTP header of the server response.
46+
*
47+
* @var string[]|null
48+
*/
49+
protected $responseHeaders;
50+
51+
/**
52+
* The deserialized response object
53+
*
54+
* @var \stdClass|string|null
55+
*/
56+
protected $responseObject;
57+
58+
public function __construct(
59+
$message,
60+
RequestInterface $request,
61+
ResponseInterface $response = null,
62+
Exception $previous = null
63+
) {
64+
parent::__construct($message, $request, $previous);
65+
if ($response) {
66+
$this->responseHeaders = $response->getHeaders();
67+
$this->responseBody = (string) $response->getBody();
68+
$this->code = $response->getStatusCode();
69+
}
70+
}
71+
72+
/**
73+
* Gets the HTTP response header
74+
*
75+
* @return string[]|null HTTP response header
76+
*/
77+
public function getResponseHeaders()
78+
{
79+
return $this->responseHeaders;
80+
}
81+
82+
/**
83+
* Gets the HTTP body of the server response either as Json or string
84+
*
85+
* @return \stdClass|string|null HTTP body of the server response either as \stdClass or string
86+
*/
87+
public function getResponseBody()
88+
{
89+
return $this->responseBody;
90+
}
91+
92+
/**
93+
* Sets the deseralized response object (during deserialization)
94+
*
95+
* @param mixed $obj Deserialized response object
96+
*
97+
* @return void
98+
*/
99+
public function setResponseObject($obj)
100+
{
101+
$this->responseObject = $obj;
102+
}
103+
104+
/**
105+
* Gets the deseralized response object (during deserialization)
106+
*
107+
* @return mixed the deserialized response object
108+
*/
109+
public function getResponseObject()
110+
{
111+
return $this->responseObject;
112+
}
113+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Configuration
4+
* PHP version 7.2
5+
*
6+
* @category Class
7+
* @package {{invokerPackage}}
8+
* @author OpenAPI Generator team
9+
* @link https://openapi-generator.tech
10+
*/
11+
12+
{{>partial_header}}
13+
/**
14+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
15+
* https://openapi-generator.tech
16+
* Do not edit the class manually.
17+
*/
18+
19+
namespace {{invokerPackage}};
20+
21+
use Http\Client\Common\Plugin;
22+
use Http\Promise\Promise;
23+
use Psr\Http\Client\ClientExceptionInterface;
24+
use Psr\Http\Message\RequestInterface;
25+
use Psr\Http\Message\ResponseInterface;
26+
use function is_resource;
27+
28+
/**
29+
* Configuration Class Doc Comment
30+
* PHP version 7.2
31+
*
32+
* @category Class
33+
* @package {{invokerPackage}}
34+
* @author OpenAPI Generator team
35+
* @link https://openapi-generator.tech
36+
*/
37+
class DebugPlugin implements Plugin
38+
{
39+
40+
/**
41+
* @var resource
42+
*/
43+
private $output;
44+
45+
/**
46+
* DebuggingPlugin constructor.
47+
*
48+
* @param resource $output
49+
*/
50+
public function __construct($output)
51+
{
52+
if (!is_resource($output)) {
53+
throw new \InvalidArgumentException('debugging resource is not valid');
54+
}
55+
$this->output = $output;
56+
}
57+
58+
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
59+
{
60+
return $next($request)->then(
61+
function (ResponseInterface $response) use ($request) {
62+
$this->logSuccess($request, $response);
63+
64+
return $response;
65+
},
66+
function (ClientExceptionInterface $exception) use ($request) {
67+
$this->logError($request, $exception);
68+
69+
throw $exception;
70+
}
71+
);
72+
}
73+
74+
private function logSuccess(RequestInterface $request, ResponseInterface $response): void
75+
{
76+
$methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath();
77+
$protocol = $response->getProtocolVersion();
78+
$responseCode = $response->getStatusCode();
79+
\fprintf($this->output, '<%s HTTP/%s> %s', $methodAndPath, $protocol, $responseCode);
80+
\fwrite($this->output, "\n");
81+
}
82+
83+
private function logError(RequestInterface $request, ClientExceptionInterface $exception): void
84+
{
85+
$methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath();
86+
$protocol = $request->getProtocolVersion();
87+
$error = $exception->getMessage();
88+
$responseCode = $exception->getCode();
89+
\fprintf($this->output, '<%s HTTP/%s> %s %s', $methodAndPath, $responseCode, $error, $protocol);
90+
\fwrite($this->output, "\n");
91+
}
92+
}

0 commit comments

Comments
 (0)