Skip to content

Commit

Permalink
Single command for both OpenApi2 & OpenApi3
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Dec 26, 2019
1 parent 9ea2b8f commit 879d582
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 96 deletions.
4 changes: 2 additions & 2 deletions src/JsonSchema/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
class Registry
{
/** @var string[] */
private $outputDirectories = [];
protected $outputDirectories = [];

/** @var Schema[] */
private $schemas = [];
protected $schemas = [];

public function addOutputDirectory(string $outputDirectory): void
{
Expand Down
11 changes: 0 additions & 11 deletions src/OpenApi/Command/GenerateCommand.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/OpenApi/Tests/JaneOpenApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Jane\OpenApi\Tests;

use Jane\OpenApi\Command\GenerateCommand;
use Jane\OpenApiCommon\Command\GenerateCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
Expand Down
22 changes: 0 additions & 22 deletions src/OpenApi/bin/jane-openapi

This file was deleted.

3 changes: 0 additions & 3 deletions src/OpenApi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"bin": [
"bin/jane-openapi"
],
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
Expand Down
11 changes: 0 additions & 11 deletions src/OpenApi2/Application.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/OpenApi2/Command/GenerateCommand.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/OpenApi2/Tests/JaneOpenApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Jane\OpenApi2\Tests;

use Jane\OpenApi2\Command\GenerateCommand;
use Jane\OpenApiCommon\Command\GenerateCommand;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
Expand Down
3 changes: 0 additions & 3 deletions src/OpenApi2/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"Jane\\OpenApi2\\": ""
}
},
"bin": [
"bin/jane-openapi"
],
"require": {
"php": ">=7.2",
"ext-json": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Jane\OpenApi;
namespace Jane\OpenApiCommon;

use Jane\JsonSchema\Application as JsonSchemaApplication;
use Jane\OpenApi\Command\GenerateCommand;
use Jane\OpenApiCommon\Command\GenerateCommand;

class Application extends JsonSchemaApplication
{
Expand Down
44 changes: 39 additions & 5 deletions src/OpenApiCommon/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

abstract class GenerateCommand extends Command
class GenerateCommand extends Command
{
protected const OPEN_API_CLASS = '';

public function configure()
{
$this->setName('generate');
Expand Down Expand Up @@ -51,9 +49,10 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}

$janeOpenApiClass = static::OPEN_API_CLASS;
$openApiClass = $this->matchOpenApiClass($registry);

/** @var JaneOpenApi $janeOpenApi */
$janeOpenApi = $janeOpenApiClass::build($options);
$janeOpenApi = $openApiClass::build($options);
$fixerConfigFile = '';

if (\array_key_exists('fixer-config-file', $options) && null !== $options['fixer-config-file']) {
Expand Down Expand Up @@ -141,4 +140,39 @@ protected function resolveSchema($schema, array $options = [])

return new Schema($schema, $options['namespace'], $options['directory'], '');
}

private function matchOpenApiClass(Registry $registry): string
{
$firstSchema = $registry->getFirstSchema();
$openApiClass = null;

if (class_exists(\Jane\OpenApi2\JaneOpenApi::class)) {
$openApi2Serializer = \Jane\OpenApi2\JaneOpenApi::buildSerializer();
$openApi2SchemaParser = new \Jane\OpenApi2\SchemaParser\SchemaParser($openApi2Serializer);

try {
$openApi2SchemaParser->parseSchema($firstSchema->getOrigin());
$openApiClass = \Jane\OpenApi2\JaneOpenApi::class;
} catch (\Exception $e) {
// We don't need this exception, we will trigger another one if needed ~
}
}
if (null === $openApiClass && class_exists(\Jane\OpenApi\JaneOpenApi::class)) {
$openApi3Serializer = \Jane\OpenApi\JaneOpenApi::buildSerializer();
$openApi3SchemaParser = new \Jane\OpenApi\SchemaParser\SchemaParser($openApi3Serializer);

try {
$openApi3SchemaParser->parseSchema($firstSchema->getOrigin());
$openApiClass = \Jane\OpenApi\JaneOpenApi::class;
} catch (\Exception $e) {
// We don't need this exception, we will trigger another one if needed ~
}
}

if (null === $openApiClass) {
throw new \BadMethodCallException('Only OpenApi v2 / v3 specifications are supported, use an external tool to convert your api files.');
}

return $openApiClass;
}
}
5 changes: 5 additions & 0 deletions src/OpenApiCommon/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class Registry extends BaseRegistry
{
public function getFirstSchema(): Schema
{
return reset($this->schemas);
}

public function hasSecurityScheme($securitySchemeReference): bool
{
return null !== $this->getClass($securitySchemeReference);
Expand Down
50 changes: 28 additions & 22 deletions src/OpenApiCommon/SchemaParser/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

abstract class SchemaParser
{
protected static $parsed = [];

protected const OPEN_API_MODEL = null;
protected const OPEN_API_VERSION_MAJOR = null;

Expand All @@ -21,30 +23,34 @@ public function __construct(SerializerInterface $serializer)

public function parseSchema(string $openApiSpecPath)
{
$openApiSpecContents = file_get_contents($openApiSpecPath);
$jsonException = null;
$yamlException = null;

try {
return $this->deserialize($openApiSpecContents, $openApiSpecPath);
} catch (\Exception $exception) {
$jsonException = $exception;
}
if (!\array_key_exists($openApiSpecPath, static::$parsed)) {
$openApiSpecContents = file_get_contents($openApiSpecPath);
$jsonException = null;
$yamlException = null;

try {
return static::$parsed[$openApiSpecPath] = $this->deserialize($openApiSpecContents, $openApiSpecPath);
} catch (\Exception $exception) {
$jsonException = $exception;
}

try {
$content = Yaml::parse(
$openApiSpecContents,
Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_DATETIME | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE
);

return $this->denormalize($content, $openApiSpecPath);
} catch (YamlException $yamlException) {
throw new \LogicException(sprintf(
"Could not parse schema in JSON nor YAML format:\n- JSON error: \"%s\"\n- YAML error: \"%s\"\n",
$jsonException->getMessage(),
$yamlException->getMessage()
));
try {
$content = Yaml::parse(
$openApiSpecContents,
Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP | Yaml::PARSE_DATETIME | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE
);

return static::$parsed[$openApiSpecPath] = $this->denormalize($content, $openApiSpecPath);
} catch (YamlException $yamlException) {
throw new \LogicException(sprintf(
"Could not parse schema in JSON nor YAML format:\n- JSON error: \"%s\"\n- YAML error: \"%s\"\n",
$jsonException->getMessage(),
$yamlException->getMessage()
));
}
}

return static::$parsed[$openApiSpecPath];
}

protected function deserialize($openApiSpecContents, $openApiSpecPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

use Jane\OpenApi2\Application;
use Jane\OpenApiCommon\Application;

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once(__DIR__ . '/../vendor/autoload.php');
Expand Down
9 changes: 8 additions & 1 deletion src/OpenApiCommon/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@
"php": ">=7.2",
"ext-json": "*",
"jane-php/json-schema": "^5.0"
}
},
"suggest": {
"jane-php/open-api-2": "Allow to generate OpenApi v2 clients",
"jane-php/open-api-3": "Allow to generate OpenApi v3 clients"
},
"bin": [
"bin/jane-openapi"
]
}

0 comments on commit 879d582

Please sign in to comment.