diff --git a/src/Console/PushCommand.php b/src/Console/PushCommand.php index 08e21db..e2f355a 100644 --- a/src/Console/PushCommand.php +++ b/src/Console/PushCommand.php @@ -21,6 +21,7 @@ namespace PSX\Api\Console; use Composer\InstalledVersions; +use PSX\Api\Generator\ConfigurationAwareInterface; use PSX\Api\Generator\Spec\OpenAPI; use PSX\Api\Generator\Spec\TypeAPI; use PSX\Api\GeneratorFactory; @@ -98,11 +99,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $spec->setBaseUrl(null); $spec->setSecurity(null); - if ($generator instanceof TypeAPI) { + if ($generator instanceof ConfigurationAwareInterface) { $generator->setBaseUrl(null); $generator->setSecurity(null); - } elseif ($generator instanceof OpenAPI) { - $generator->setBaseUrl(null); } } diff --git a/src/Generator/Client/LanguageAbstract.php b/src/Generator/Client/LanguageAbstract.php index 8bb203e..4c09f47 100644 --- a/src/Generator/Client/LanguageAbstract.php +++ b/src/Generator/Client/LanguageAbstract.php @@ -23,6 +23,8 @@ use PSX\Api\Generator\Client\Dto\Response; use PSX\Api\Generator\Client\Dto\Tag; use PSX\Api\Generator\Client\Util\Naming; +use PSX\Api\Generator\ConfigurationAwareInterface; +use PSX\Api\Generator\ConfigurationTrait; use PSX\Api\GeneratorInterface; use PSX\Api\SpecificationInterface; use PSX\Schema\DefinitionsInterface; @@ -44,9 +46,10 @@ * @license http://www.apache.org/licenses/LICENSE-2.0 * @link https://phpsx.org */ -abstract class LanguageAbstract implements GeneratorInterface +abstract class LanguageAbstract implements GeneratorInterface, ConfigurationAwareInterface { - protected ?string $baseUrl; + use ConfigurationTrait; + protected ?string $namespace; protected ?Generator\Config $config; protected Environment $engine; @@ -61,10 +64,10 @@ abstract class LanguageAbstract implements GeneratorInterface public function __construct(?string $baseUrl = null, ?Generator\Config $config = null) { - $this->baseUrl = $baseUrl; + $this->baseUrl = $baseUrl; $this->namespace = $config?->get(Generator\Config::NAMESPACE); - $this->config = $config; - $this->engine = $this->newTemplateEngine(); + $this->config = $config; + $this->engine = $this->newTemplateEngine(); $this->generator = $this->newGenerator(); if (!$this->generator instanceof Generator\TypeAwareInterface) { @@ -88,7 +91,9 @@ public function generate(SpecificationInterface $specification): Generator\Code\ { $chunks = new Generator\Code\Chunks(); - $client = $this->converter->getClient($specification); + $baseUrl = $this->getBaseUrl($specification); + $security = $this->getSecurity($specification); + $client = $this->converter->getClient($specification, $baseUrl, $security); $tagImports = []; foreach ($client->tags as $tag) { diff --git a/src/Generator/Client/LanguageBuilder.php b/src/Generator/Client/LanguageBuilder.php index f7eb596..a06effd 100644 --- a/src/Generator/Client/LanguageBuilder.php +++ b/src/Generator/Client/LanguageBuilder.php @@ -80,13 +80,8 @@ public function __construct(GeneratorInterface $generator, Naming $naming) * @throws InvalidTypeException * @throws TypeNotFoundException */ - public function getClient(SpecificationInterface $specification): Dto\Client + public function getClient(SpecificationInterface $specification, ?string $baseUrl, ?SecurityInterface $security): Dto\Client { - $security = null; - if ($specification->getSecurity() instanceof SecurityInterface) { - $security = $specification->getSecurity()->toArray(); - } - $exceptions = []; $grouped = $this->groupOperations($specification->getOperations()); @@ -98,8 +93,8 @@ public function getClient(SpecificationInterface $specification): Dto\Client $operations, $tags, $exceptions, - $security, - $specification->getBaseUrl(), + $security?->toArray(), + $baseUrl, ); } diff --git a/src/Generator/ConfigurationAwareInterface.php b/src/Generator/ConfigurationAwareInterface.php new file mode 100644 index 0000000..8eed570 --- /dev/null +++ b/src/Generator/ConfigurationAwareInterface.php @@ -0,0 +1,38 @@ + + * + * Copyright 2010-2023 Christoph Kappestein + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace PSX\Api\Generator; + +use PSX\Api\SecurityInterface; + +/** + * ConfigurationAwareInterface + * + * @see https://typeschema.org/ + * @author Christoph Kappestein + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @link https://phpsx.org + */ +interface ConfigurationAwareInterface +{ + public function setBaseUrl(?string $baseUrl): void; + + public function setSecurity(?SecurityInterface $security): void; +} \ No newline at end of file diff --git a/src/Generator/ConfigurationTrait.php b/src/Generator/ConfigurationTrait.php new file mode 100644 index 0000000..c191a0b --- /dev/null +++ b/src/Generator/ConfigurationTrait.php @@ -0,0 +1,71 @@ + + * + * Copyright 2010-2023 Christoph Kappestein + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace PSX\Api\Generator; + +use PSX\Api\SecurityInterface; +use PSX\Api\SpecificationInterface; + +/** + * ConfigurationTrait + * + * @author Christoph Kappestein + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @link https://phpsx.org + */ +trait ConfigurationTrait +{ + protected ?string $baseUrl; + protected ?SecurityInterface $security = null; + + public function setBaseUrl(?string $baseUrl): void + { + $this->baseUrl = $baseUrl; + } + + public function setSecurity(?SecurityInterface $security): void + { + $this->security = $security; + } + + protected function getBaseUrl(SpecificationInterface $specification): ?string + { + $baseUrl = $specification->getBaseUrl(); + if (!empty($baseUrl)) { + return $baseUrl; + } elseif (!empty($this->baseUrl)) { + return $this->baseUrl; + } else { + return null; + } + } + + protected function getSecurity(SpecificationInterface $specification): ?SecurityInterface + { + $security = $specification->getSecurity(); + if ($security instanceof SecurityInterface) { + return $security; + } elseif ($this->security instanceof SecurityInterface) { + return $this->security; + } else { + return null; + } + } +} \ No newline at end of file diff --git a/src/Generator/Markup/MarkupAbstract.php b/src/Generator/Markup/MarkupAbstract.php index 0575153..3235d46 100644 --- a/src/Generator/Markup/MarkupAbstract.php +++ b/src/Generator/Markup/MarkupAbstract.php @@ -24,6 +24,8 @@ use PSX\Api\Generator\Client\Dto; use PSX\Api\Generator\Client\LanguageBuilder; use PSX\Api\Generator\Client\Util\Naming; +use PSX\Api\Generator\ConfigurationAwareInterface; +use PSX\Api\Generator\ConfigurationTrait; use PSX\Api\GeneratorInterface; use PSX\Api\SpecificationInterface; use PSX\Schema\DefinitionsInterface; @@ -39,8 +41,10 @@ * @license http://www.apache.org/licenses/LICENSE-2.0 * @link https://phpsx.org */ -abstract class MarkupAbstract implements GeneratorInterface +abstract class MarkupAbstract implements GeneratorInterface, ConfigurationAwareInterface { + use ConfigurationTrait; + private SchemaGeneratorInterface $generator; private Naming $naming; private LanguageBuilder $converter; @@ -58,7 +62,9 @@ public function __construct() public function generate(SpecificationInterface $specification): string { - $client = $this->converter->getClient($specification); + $baseUrl = $this->getBaseUrl($specification); + $security = $this->getSecurity($specification); + $client = $this->converter->getClient($specification, $baseUrl, $security); $lines = $this->startLines($client); diff --git a/src/Generator/Proxy/SDKgen.php b/src/Generator/Proxy/SDKgen.php index 2e0ec66..318b8c1 100644 --- a/src/Generator/Proxy/SDKgen.php +++ b/src/Generator/Proxy/SDKgen.php @@ -21,9 +21,10 @@ namespace PSX\Api\Generator\Proxy; use PSX\Api\Exception\GeneratorException; +use PSX\Api\Generator\ConfigurationAwareInterface; +use PSX\Api\Generator\ConfigurationTrait; use PSX\Api\Generator\Spec\TypeAPI; use PSX\Api\GeneratorInterface; -use PSX\Api\SecurityInterface; use PSX\Api\SpecificationInterface; use PSX\Http\Client\ClientInterface; use PSX\Http\Client\PostRequest; @@ -38,14 +39,14 @@ * @license http://www.apache.org/licenses/LICENSE-2.0 * @link https://phpsx.org */ -class SDKgen implements GeneratorInterface +class SDKgen implements GeneratorInterface, ConfigurationAwareInterface { + use ConfigurationTrait; + private ClientInterface $httpClient; private string $accessToken; private string $type; - private ?string $baseUrl; private ?Generator\Config $config; - private ?SecurityInterface $security = null; public function __construct(ClientInterface $httpClient, string $accessToken, string $type, ?string $baseUrl = null, ?Generator\Config $config = null) { @@ -100,38 +101,4 @@ public function generate(SpecificationInterface $specification): Generator\Code\ throw new GeneratorException('Could not generate SDK, received an invalid response'); } } - - public function setBaseUrl(?string $baseUrl): void - { - $this->baseUrl = $baseUrl; - } - - public function setSecurity(?SecurityInterface $security): void - { - $this->security = $security; - } - - private function getBaseUrl(SpecificationInterface $specification): ?string - { - $baseUrl = $specification->getBaseUrl(); - if (!empty($baseUrl)) { - return $baseUrl; - } elseif (!empty($this->baseUrl)) { - return $this->baseUrl; - } else { - return null; - } - } - - private function getSecurity(SpecificationInterface $specification): ?SecurityInterface - { - $security = $specification->getSecurity(); - if ($security instanceof SecurityInterface) { - return $security; - } elseif ($this->security instanceof SecurityInterface) { - return $this->security; - } else { - return null; - } - } } diff --git a/src/Generator/Spec/ApiAbstract.php b/src/Generator/Spec/ApiAbstract.php index 5da8172..3f885e1 100644 --- a/src/Generator/Spec/ApiAbstract.php +++ b/src/Generator/Spec/ApiAbstract.php @@ -20,6 +20,7 @@ namespace PSX\Api\Generator\Spec; +use PSX\Api\Generator\ConfigurationAwareInterface; use PSX\Api\GeneratorInterface; /** @@ -27,7 +28,7 @@ * @license http://www.apache.org/licenses/LICENSE-2.0 * @link https://phpsx.org */ -abstract class ApiAbstract implements GeneratorInterface +abstract class ApiAbstract implements GeneratorInterface, ConfigurationAwareInterface { public const FLOW_AUTHORIZATION_CODE = 0; public const FLOW_IMPLICIT = 1; diff --git a/src/Generator/Spec/OpenAPI.php b/src/Generator/Spec/OpenAPI.php index 836142c..6e38bfd 100644 --- a/src/Generator/Spec/OpenAPI.php +++ b/src/Generator/Spec/OpenAPI.php @@ -20,6 +20,7 @@ namespace PSX\Api\Generator\Spec; +use PSX\Api\Generator\ConfigurationTrait; use PSX\Api\Operation\Argument; use PSX\Api\OperationInterface; use PSX\Api\OperationsInterface; @@ -67,8 +68,9 @@ */ class OpenAPI extends ApiAbstract { + use ConfigurationTrait; + private int $apiVersion; - private ?string $baseUrl; private Dumper $dumper; private Generator\JsonSchema $generator; @@ -95,11 +97,6 @@ public function generate(SpecificationInterface $specification): Generator\Code\ return $this->buildDeclaration($paths, $definitions, $this->getBaseUrl($specification)); } - public function setBaseUrl(?string $baseUrl): void - { - $this->baseUrl = $baseUrl; - } - protected function buildDeclaration(Paths $paths, DefinitionsInterface $definitions, ?string $baseUrl): string { $info = new Info(); @@ -437,16 +434,4 @@ private function newConfig(): Generator\Config return $config; } - - private function getBaseUrl(SpecificationInterface $specification): ?string - { - $baseUrl = $specification->getBaseUrl(); - if (!empty($baseUrl)) { - return $baseUrl; - } elseif (!empty($this->baseUrl)) { - return $this->baseUrl; - } else { - return null; - } - } } diff --git a/src/Generator/Spec/TypeAPI.php b/src/Generator/Spec/TypeAPI.php index 81b5496..d30a0c0 100644 --- a/src/Generator/Spec/TypeAPI.php +++ b/src/Generator/Spec/TypeAPI.php @@ -20,6 +20,7 @@ namespace PSX\Api\Generator\Spec; +use PSX\Api\Generator\ConfigurationTrait; use PSX\Api\GeneratorInterface; use PSX\Api\SecurityInterface; use PSX\Api\SpecificationInterface; @@ -38,8 +39,7 @@ */ class TypeAPI implements GeneratorInterface { - private ?string $baseUrl; - private ?SecurityInterface $security = null; + use ConfigurationTrait; public function __construct(?string $baseUrl = null) { @@ -53,18 +53,14 @@ public function generate(SpecificationInterface $specification): Generator\Code\ $data = []; - $baseUrl = $specification->getBaseUrl(); + $baseUrl = $this->getBaseUrl($specification); if (!empty($baseUrl)) { $data['baseUrl'] = $baseUrl; - } elseif (!empty($this->baseUrl)) { - $data['baseUrl'] = $this->baseUrl; } - $security = $specification->getSecurity(); + $security = $this->getSecurity($specification); if ($security instanceof SecurityInterface) { $data['security'] = $security; - } elseif ($this->security instanceof SecurityInterface) { - $data['security'] = $this->security; } $data['operations'] = $operations; @@ -73,16 +69,6 @@ public function generate(SpecificationInterface $specification): Generator\Code\ return Parser::encode($data); } - public function setBaseUrl(?string $baseUrl): void - { - $this->baseUrl = $baseUrl; - } - - public function setSecurity(?SecurityInterface $security): void - { - $this->security = $security; - } - private function generateDefinitions(DefinitionsInterface $definitions): ?array { $generator = new Generator\TypeSchema();