From 1c7480fd76fa26a59dc8c4191b44a59617d7b7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Sat, 11 Feb 2023 08:02:42 +0800 Subject: [PATCH] Added `gen:swagger` to generate `openapi.json`. --- src/swagger/publish/swagger.php | 2 +- .../src/Annotation/AnnotationTrait.php | 48 +++++++++++++ src/swagger/src/Annotation/Get.php | 21 ++++++ src/swagger/src/Annotation/Head.php | 21 ++++++ src/swagger/src/Annotation/Patch.php | 21 ++++++ src/swagger/src/Annotation/PathParameter.php | 21 ++++++ src/swagger/src/Annotation/Post.php | 21 ++++++ src/swagger/src/Annotation/Put.php | 21 ++++++ src/swagger/src/Annotation/QueryParameter.php | 71 +++++++++++++++++++ src/swagger/src/Command/GenCommand.php | 48 +++++++++++++ src/swagger/src/ConfigProvider.php | 4 ++ src/swagger/src/HttpServer.php | 2 +- 12 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 src/swagger/src/Annotation/AnnotationTrait.php create mode 100644 src/swagger/src/Annotation/Get.php create mode 100644 src/swagger/src/Annotation/Head.php create mode 100644 src/swagger/src/Annotation/Patch.php create mode 100644 src/swagger/src/Annotation/PathParameter.php create mode 100644 src/swagger/src/Annotation/Post.php create mode 100644 src/swagger/src/Annotation/Put.php create mode 100644 src/swagger/src/Annotation/QueryParameter.php create mode 100644 src/swagger/src/Command/GenCommand.php diff --git a/src/swagger/publish/swagger.php b/src/swagger/publish/swagger.php index eb6ada6b0..eeffa9f54 100644 --- a/src/swagger/publish/swagger.php +++ b/src/swagger/publish/swagger.php @@ -12,7 +12,7 @@ declare(strict_types=1); return [ 'enable' => env('APP_ENV', 'dev') !== 'prod', 'port' => 9500, - 'json' => '/storage/openapi.json', + 'json' => BASE_PATH . '/openapi.json', 'html' => null, 'url' => '/swagger', ]; diff --git a/src/swagger/src/Annotation/AnnotationTrait.php b/src/swagger/src/Annotation/AnnotationTrait.php new file mode 100644 index 000000000..0564e3d00 --- /dev/null +++ b/src/swagger/src/Annotation/AnnotationTrait.php @@ -0,0 +1,48 @@ +formatAnnotation($annotation)); + } + + public function collectMethod(string $className, ?string $target): void + { + $annotation = AnnotationCollector::getClassMethodAnnotation($className, $target)[static::class] ?? null; + + AnnotationCollector::collectMethod($className, $target, static::class, $this->formatAnnotation($annotation)); + } + + public function collectProperty(string $className, ?string $target): void + { + $annotation = AnnotationCollector::getClassPropertyAnnotation($className, $target)[static::class] ?? null; + + AnnotationCollector::collectProperty($className, $target, static::class, $this->formatAnnotation($annotation)); + } + + protected function formatAnnotation(?MultipleAnnotation $annotation): MultipleAnnotation + { + if ($annotation instanceof MultipleAnnotation) { + return $annotation->insert($this); + } + + return new MultipleAnnotation($this); + } +} diff --git a/src/swagger/src/Annotation/Get.php b/src/swagger/src/Annotation/Get.php new file mode 100644 index 000000000..ff74628a0 --- /dev/null +++ b/src/swagger/src/Annotation/Get.php @@ -0,0 +1,21 @@ +setDescription('Generate swagger json file.'); + } + + public function handle() + { + $config = $this->container->get(ConfigInterface::class); + + $paths = $config->get('annotations.scan.paths', []); + + $openapi = Generator::scan($paths, [ + 'validate' => false, + ]); + + $path = $config->get('swagger.json'); + + file_put_contents($path, $openapi->toJson()); + + $this->output->writeln('Generate swagger json success.'); + } +} diff --git a/src/swagger/src/ConfigProvider.php b/src/swagger/src/ConfigProvider.php index 7f9280b85..571ccf6f5 100644 --- a/src/swagger/src/ConfigProvider.php +++ b/src/swagger/src/ConfigProvider.php @@ -11,6 +11,7 @@ declare(strict_types=1); */ namespace Hyperf\Swagger; +use Hyperf\Swagger\Command\GenCommand; use Hyperf\Swagger\Listener\BootSwaggerListener; class ConfigProvider @@ -18,6 +19,9 @@ class ConfigProvider public function __invoke(): array { return [ + 'commands' => [ + GenCommand::class, + ], 'listeners' => [ BootSwaggerListener::class, ], diff --git a/src/swagger/src/HttpServer.php b/src/swagger/src/HttpServer.php index d02e0f754..a408801b3 100644 --- a/src/swagger/src/HttpServer.php +++ b/src/swagger/src/HttpServer.php @@ -79,7 +79,7 @@ class HttpServer implements OnRequestInterface return $this->metadata; } - return $this->metadata = Json::decode(file_get_contents(BASE_PATH . $this->config['json'])); + return $this->metadata = Json::decode(file_get_contents($this->config['json'])); } protected function getHtml(): string