diff --git a/src/JsonSchema/Registry.php b/src/JsonSchema/Registry.php index 1738d0cb3e..b8bfa00d05 100644 --- a/src/JsonSchema/Registry.php +++ b/src/JsonSchema/Registry.php @@ -9,10 +9,10 @@ class Registry { /** @var string[] */ - private $outputDirectories = []; + protected $outputDirectories = []; /** @var Schema[] */ - private $schemas = []; + protected $schemas = []; public function addOutputDirectory(string $outputDirectory): void { diff --git a/src/OpenApi/Command/GenerateCommand.php b/src/OpenApi/Command/GenerateCommand.php deleted file mode 100644 index bbf2de1207..0000000000 --- a/src/OpenApi/Command/GenerateCommand.php +++ /dev/null @@ -1,11 +0,0 @@ -run(); diff --git a/src/OpenApi/composer.json b/src/OpenApi/composer.json index ef7e8b6f70..a04fb8b746 100644 --- a/src/OpenApi/composer.json +++ b/src/OpenApi/composer.json @@ -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" diff --git a/src/OpenApi2/Application.php b/src/OpenApi2/Application.php deleted file mode 100644 index 041efe56f7..0000000000 --- a/src/OpenApi2/Application.php +++ /dev/null @@ -1,11 +0,0 @@ -=7.2", "ext-json": "*", diff --git a/src/OpenApi/Application.php b/src/OpenApiCommon/Application.php similarity index 69% rename from src/OpenApi/Application.php rename to src/OpenApiCommon/Application.php index f69b688eba..9d37e05a63 100644 --- a/src/OpenApi/Application.php +++ b/src/OpenApiCommon/Application.php @@ -1,9 +1,9 @@ setName('generate'); @@ -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']) { @@ -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; + } } diff --git a/src/OpenApiCommon/Registry.php b/src/OpenApiCommon/Registry.php index daff7a95b6..8f52349aeb 100644 --- a/src/OpenApiCommon/Registry.php +++ b/src/OpenApiCommon/Registry.php @@ -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); diff --git a/src/OpenApiCommon/SchemaParser/SchemaParser.php b/src/OpenApiCommon/SchemaParser/SchemaParser.php index ba631a6b73..b54509e8e9 100644 --- a/src/OpenApiCommon/SchemaParser/SchemaParser.php +++ b/src/OpenApiCommon/SchemaParser/SchemaParser.php @@ -8,6 +8,8 @@ abstract class SchemaParser { + protected static $parsed = []; + protected const OPEN_API_MODEL = null; protected const OPEN_API_VERSION_MAJOR = null; @@ -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) diff --git a/src/OpenApi2/bin/jane-openapi b/src/OpenApiCommon/bin/jane-openapi similarity index 95% rename from src/OpenApi2/bin/jane-openapi rename to src/OpenApiCommon/bin/jane-openapi index 3153dc02dc..60472b9964 100755 --- a/src/OpenApi2/bin/jane-openapi +++ b/src/OpenApiCommon/bin/jane-openapi @@ -1,7 +1,7 @@ #!/usr/bin/env 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" + ] }