diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java index 3c57a89f4abf3..95d3c2ac777f0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java @@ -124,6 +124,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile(".php-cs-fixer.dist.php", "", ".php-cs-fixer.dist.php")); supportingFiles.add(new SupportingFile(".phplint.mustache", "", ".phplint.yml")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); + supportingFiles.add(new SupportingFile("ClientBuilder.mustache", toSrcPath(invokerPackage, srcBasePath), "ClientBuilder.php")); } @Override diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ClientBuilder.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ClientBuilder.mustache new file mode 100644 index 0000000000000..651e59f92c168 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ClientBuilder.mustache @@ -0,0 +1,122 @@ +partial_header}} +/** + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +namespace {{invokerPackage}}; + +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; +use {{invokerPackage}}\Configuration; +use {{invokerPackage}}\HeaderSelector; +{{#apiInfo}} +{{#apis}} +use {{apiPackage}}\{{classFilename}}; +{{/apis}} +{{/apiInfo}} + +/** + * ClientBuilder Class Doc Comment + * + * @package {{invokerPackage}} + * @author OpenAPI Generator team + * @link https://openapi-generator.tech +{{#apiInfo}} +{{#apis}} + * @property-read {{classFilename}} ${{classVarName}} +{{/apis}} +{{/apiInfo}} + */ +class ClientBuilder +{ + /** + * @var ClientInterface + */ + protected ClientInterface $client; + + /** + * @var Configuration + */ + protected Configuration $config; + + /** + * @var HeaderSelector + */ + protected HeaderSelector $headerSelector; + + /** + * @var int Host index + */ + protected int $hostIndex; + + /** + * @param ClientInterface|null $client + * @param Configuration|null $config + * @param HeaderSelector|null $selector + * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + */ + public function __construct( + ClientInterface $client = null, + Configuration $config = null, + HeaderSelector $selector = null, + int $hostIndex = 0 + ) { + $this->client = $client ?: new Client(); + $this->config = $config ?: new Configuration(); + $this->headerSelector = $selector ?: new HeaderSelector(); + $this->hostIndex = $hostIndex; + } + + /** + * Initiates new client builder with OAuth access token. + * @link https://docs.salla.dev/docs/merchant/2fa0d7aca2def-authorization + * + * @param string $accessToken + * @throws \InvalidArgumentException + * @return ClientBuilder + */ + public static function createWithAccessToken( + string $accessToken + ): static { + if (empty($accessToken)) { + throw new \InvalidArgumentException('Access token cannot be empty'); + } + + $config = new Configuration(); + $config->setAccessToken($accessToken); + + return new static(null, $config); + } + + /** + * {{=<% %>=}}{@inheritDoc}<%={{ }}=%> + */ + public function __get(string $name) + { + switch ($name) { + {{#apiInfo}} + {{#apis}} + case '{{classVarName}}': + return new {{classFilename}}($this->client, $this->config, $this->headerSelector, $this->hostIndex); + {{/apis}} + {{/apiInfo}} + default: + throw new \InvalidArgumentException(\sprintf('Unknown property "%s"', $name)); + } + } +} diff --git a/samples/client/echo_api/php-nextgen/.openapi-generator/FILES b/samples/client/echo_api/php-nextgen/.openapi-generator/FILES index 82705c8ca1df0..bfa03068186e7 100644 --- a/samples/client/echo_api/php-nextgen/.openapi-generator/FILES +++ b/samples/client/echo_api/php-nextgen/.openapi-generator/FILES @@ -30,6 +30,7 @@ src/Api/HeaderApi.php src/Api/PathApi.php src/Api/QueryApi.php src/ApiException.php +src/ClientBuilder.php src/Configuration.php src/HeaderSelector.php src/Model/Bird.php diff --git a/samples/client/echo_api/php-nextgen/src/ClientBuilder.php b/samples/client/echo_api/php-nextgen/src/ClientBuilder.php new file mode 100644 index 0000000000000..4ee01cb859616 --- /dev/null +++ b/samples/client/echo_api/php-nextgen/src/ClientBuilder.php @@ -0,0 +1,140 @@ +client = $client ?: new Client(); + $this->config = $config ?: new Configuration(); + $this->headerSelector = $selector ?: new HeaderSelector(); + $this->hostIndex = $hostIndex; + } + + /** + * Initiates new client builder with OAuth access token. + * @link https://docs.salla.dev/docs/merchant/2fa0d7aca2def-authorization + * + * @param string $accessToken + * @throws \InvalidArgumentException + * @return ClientBuilder + */ + public static function createWithAccessToken( + string $accessToken + ): static { + if (empty($accessToken)) { + throw new \InvalidArgumentException('Access token cannot be empty'); + } + + $config = new Configuration(); + $config->setAccessToken($accessToken); + + return new static(null, $config); + } + + /** + * {@inheritDoc} + */ + public function __get(string $name) + { + switch ($name) { + case 'auth': + return new AuthApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'body': + return new BodyApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'form': + return new FormApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'header': + return new HeaderApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'path': + return new PathApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'query': + return new QueryApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + default: + throw new \InvalidArgumentException(\sprintf('Unknown property "%s"', $name)); + } + } +} diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES b/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES index ec822fdd8838e..d01b2c03fa2c0 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/.openapi-generator/FILES @@ -71,6 +71,7 @@ src/Api/PetApi.php src/Api/StoreApi.php src/Api/UserApi.php src/ApiException.php +src/ClientBuilder.php src/Configuration.php src/HeaderSelector.php src/Model/AdditionalPropertiesClass.php diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ClientBuilder.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ClientBuilder.php new file mode 100644 index 0000000000000..3ce36d3e93e91 --- /dev/null +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ClientBuilder.php @@ -0,0 +1,143 @@ +client = $client ?: new Client(); + $this->config = $config ?: new Configuration(); + $this->headerSelector = $selector ?: new HeaderSelector(); + $this->hostIndex = $hostIndex; + } + + /** + * Initiates new client builder with OAuth access token. + * @link https://docs.salla.dev/docs/merchant/2fa0d7aca2def-authorization + * + * @param string $accessToken + * @throws \InvalidArgumentException + * @return ClientBuilder + */ + public static function createWithAccessToken( + string $accessToken + ): static { + if (empty($accessToken)) { + throw new \InvalidArgumentException('Access token cannot be empty'); + } + + $config = new Configuration(); + $config->setAccessToken($accessToken); + + return new static(null, $config); + } + + /** + * {@inheritDoc} + */ + public function __get(string $name) + { + switch ($name) { + case 'anotherFake': + return new AnotherFakeApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'default': + return new DefaultApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'fake': + return new FakeApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'fakeClassnameTags123': + return new FakeClassnameTags123Api($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'pet': + return new PetApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'store': + return new StoreApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + case 'user': + return new UserApi($this->client, $this->config, $this->headerSelector, $this->hostIndex); + default: + throw new \InvalidArgumentException(\sprintf('Unknown property "%s"', $name)); + } + } +}