psx / asyncapi
模型类,用于以类型安全的方式生成 AsyncAPI 规范
    v0.1.1
    2024-05-10 16:31 UTC
Requires
- php: >=8.1
- psx/openapi: ^0.1
- psx/record: ^3.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- psx/schema: ^6.0
- symfony/yaml: ^5.0|^6.0|^7.0
- vimeo/psalm: ^5.0
README
关于
此库包含模型类,用于以类型安全的方式生成 AsyncAPI 规范。这些模型是根据 TypeSchema 规范(见 typeschema.json)自动生成的。以下示例展示了如何生成 OpenAPI 规范
$info = new Info(); $info->setTitle('Account Service'); $info->setVersion('1.0.0'); $info->setDescription('This service is in charge of processing user signups :rocket:'); $message = new Message(); $message->setPayload(Record::fromArray(['$ref' => '#/components/schemas/Pet'])); $http = new HttpOperationBinding(); $http->setType('request'); $http->setMethod('POST'); $bindings = new OperationBindings(); $bindings->setHttp($http); $operation = new Operation(); $operation->setMessage($message); $operation->setBindings($bindings); $channel = new Channel(); $channel->setPublish($operation); $channels = new Channels(); $channels->put('user/signedup', $channel); $schemas = new Schemas(); $schemas->put('Pet', [ 'required' => ['id', 'name'], 'properties' => [ 'id' => ['type' => 'integer', 'format' => 'int64'], 'name' => ['type' => 'string'], 'tag' => ['type' => 'string'], ] ]); $components = new Components(); $components->setSchemas($schemas); $asyncAPI = new AsyncAPI(); $asyncAPI->setInfo($info); $asyncAPI->setChannels($channels); $asyncAPI->setComponents($components); echo json_encode($asyncAPI, JSON_PRETTY_PRINT);
这将生成以下 JSON
{
  "asyncapi": "2.6.0",
  "info": {
    "title": "Account Service",
    "description": "This service is in charge of processing user signups :rocket:",
    "version": "1.0.0"
  },
  "channels": {
    "user\/signedup": {
      "publish": {
        "bindings": {
          "http": {
            "type": "request",
            "method": "POST"
          }
        },
        "message": {
          "payload": {
            "$ref": "#\/components\/schemas\/Pet"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "name": {
            "type": "string"
          },
          "tag": {
            "type": "string"
          }
        }
      }
    }
  }
}
贡献
如果您想提出更改,请只修改 typeschema.json 规范,然后运行 php gen.php 脚本来重新生成所有模型类。