mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Added gen:swagger
to generate openapi.json
.
This commit is contained in:
parent
c91742bf0f
commit
1c7480fd76
@ -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',
|
||||
];
|
||||
|
48
src/swagger/src/Annotation/AnnotationTrait.php
Normal file
48
src/swagger/src/Annotation/AnnotationTrait.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Hyperf\Di\Annotation\AnnotationCollector;
|
||||
use Hyperf\Di\Annotation\MultipleAnnotation;
|
||||
|
||||
trait AnnotationTrait
|
||||
{
|
||||
public function collectClass(string $className): void
|
||||
{
|
||||
$annotation = AnnotationCollector::getClassAnnotation($className, static::class);
|
||||
|
||||
AnnotationCollector::collectClass($className, static::class, $this->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);
|
||||
}
|
||||
}
|
21
src/swagger/src/Annotation/Get.php
Normal file
21
src/swagger/src/Annotation/Get.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
||||
class Get extends \OpenApi\Attributes\Get implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
21
src/swagger/src/Annotation/Head.php
Normal file
21
src/swagger/src/Annotation/Head.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
||||
class Head extends \OpenApi\Attributes\Head implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
21
src/swagger/src/Annotation/Patch.php
Normal file
21
src/swagger/src/Annotation/Patch.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
||||
class Patch extends \OpenApi\Attributes\Patch implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
21
src/swagger/src/Annotation/PathParameter.php
Normal file
21
src/swagger/src/Annotation/PathParameter.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
|
||||
class PathParameter extends \OpenApi\Attributes\PathParameter implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
21
src/swagger/src/Annotation/Post.php
Normal file
21
src/swagger/src/Annotation/Post.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
||||
class Post extends \OpenApi\Attributes\Post implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
21
src/swagger/src/Annotation/Put.php
Normal file
21
src/swagger/src/Annotation/Put.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
|
||||
class Put extends \OpenApi\Attributes\Put implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
}
|
71
src/swagger/src/Annotation/QueryParameter.php
Normal file
71
src/swagger/src/Annotation/QueryParameter.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AnnotationInterface;
|
||||
use OpenApi\Attributes\Attachable;
|
||||
use OpenApi\Attributes\JsonContent;
|
||||
use OpenApi\Attributes\Schema;
|
||||
use OpenApi\Attributes\XmlContent;
|
||||
use OpenApi\Generator;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
|
||||
class QueryParameter extends \OpenApi\Attributes\QueryParameter implements AnnotationInterface
|
||||
{
|
||||
use AnnotationTrait;
|
||||
|
||||
public function __construct(
|
||||
?string $parameter = null,
|
||||
?string $name = null,
|
||||
?string $description = null,
|
||||
?string $in = null,
|
||||
?bool $required = null,
|
||||
?bool $deprecated = null,
|
||||
?bool $allowEmptyValue = null,
|
||||
object|string|null $ref = null,
|
||||
?Schema $schema = null,
|
||||
mixed $example = Generator::UNDEFINED,
|
||||
?array $examples = null,
|
||||
JsonContent|array|Attachable|XmlContent|null $content = null,
|
||||
?string $style = null,
|
||||
?bool $explode = null,
|
||||
?bool $allowReserved = null,
|
||||
?array $spaceDelimited = null,
|
||||
?array $pipeDelimited = null,
|
||||
?array $x = null,
|
||||
?array $attachables = null,
|
||||
public mixed $rules = null,
|
||||
) {
|
||||
parent::__construct(
|
||||
$parameter,
|
||||
$name,
|
||||
$description,
|
||||
$in,
|
||||
$required,
|
||||
$deprecated,
|
||||
$allowEmptyValue,
|
||||
$ref,
|
||||
$schema,
|
||||
$example,
|
||||
$examples,
|
||||
$content,
|
||||
$style,
|
||||
$explode,
|
||||
$allowReserved,
|
||||
$spaceDelimited,
|
||||
$pipeDelimited,
|
||||
$x,
|
||||
$attachables
|
||||
);
|
||||
}
|
||||
}
|
48
src/swagger/src/Command/GenCommand.php
Normal file
48
src/swagger/src/Command/GenCommand.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Swagger\Command;
|
||||
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use OpenApi\Generator;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class GenCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('gen:swagger');
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
parent::configure();
|
||||
$this->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.');
|
||||
}
|
||||
}
|
@ -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,
|
||||
],
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user