Skip to content

Commit

Permalink
add configuration aware interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Jul 12, 2024
1 parent 254a11d commit ff56672
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 94 deletions.
5 changes: 2 additions & 3 deletions src/Console/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/Generator/Client/LanguageAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions src/Generator/Client/LanguageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -98,8 +93,8 @@ public function getClient(SpecificationInterface $specification): Dto\Client
$operations,
$tags,
$exceptions,
$security,
$specification->getBaseUrl(),
$security?->toArray(),
$baseUrl,
);
}

Expand Down
38 changes: 38 additions & 0 deletions src/Generator/ConfigurationAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <[email protected]>
*
* 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 <[email protected]>
* @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;
}
71 changes: 71 additions & 0 deletions src/Generator/ConfigurationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <[email protected]>
*
* 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 <[email protected]>
* @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;
}
}
}
10 changes: 8 additions & 2 deletions src/Generator/Markup/MarkupAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down
43 changes: 5 additions & 38 deletions src/Generator/Proxy/SDKgen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/Generator/Spec/ApiAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

namespace PSX\Api\Generator\Spec;

use PSX\Api\Generator\ConfigurationAwareInterface;
use PSX\Api\GeneratorInterface;

/**
* @author Christoph Kappestein <[email protected]>
* @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;
Expand Down
21 changes: 3 additions & 18 deletions src/Generator/Spec/OpenAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,8 +68,9 @@
*/
class OpenAPI extends ApiAbstract
{
use ConfigurationTrait;

private int $apiVersion;
private ?string $baseUrl;

private Dumper $dumper;
private Generator\JsonSchema $generator;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
}
Loading

0 comments on commit ff56672

Please sign in to comment.