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