spryker-sdk/async-api

AsyncAPI 的 SDK。

0.3.2 2023-08-18 12:42 UTC

This package is auto-updated.

Last update: 2024-09-20 13:38:54 UTC


README

Build Status Latest Stable Version Minimum PHP Version PHPStan

此库提供了一个 AsyncAPI 解析器。

安装

  • composer require --dev spryker-sdk/async-api

使用方法

解析 AsyncAPI 文件

use SprykerSdk\AsyncApi\AsyncApi\Loader\AsyncApiLoader;

$asyncApiLoader = new AsyncApiLoader();
$asyncApi = $asyncApiLoader->load('.../path/to/async-api.yml');

// Get the channel(s)
$channels = $asyncApi->getChannels();
$channel = $asyncApi->getChannel('channel-name');

// Get publish message(s)
$publishMessages = $channel->getPublishMessages();
$publishMessage = $channel->getPublishMessage('message-name');

// Get subscribe message(s)
$subscribeMessages = $channel->getSubscribeMessages();
$subscribeMessage = $channel->getSubscribeMessage('message-name');

// Get message detail(s)
$messageAttributes = $subscribeMessage->getAttributes();
$messageAttribute = $subscribeMessage->getAttribute('attribute-name');

运行测试/检查

  • composer test - 这将执行测试。
  • composer cs-check - 这将运行代码风格检查。
  • composer cs-fix - 这将修复可修复的代码风格。
  • composer stan - 这将运行 PHPStan 检查。

文档

以下控制台命令可用

  • vendor/bin/asyncapi schema:asyncapi:create
  • vendor/bin/asyncapi schema:asyncapi:message:add
  • vendor/bin/asyncapi schema:asyncapi:validate
  • vendor/bin/asyncapi code:asyncapi:generate

添加 AsyncAPI 文件

vendor/bin/asyncapi schema:asyncapi:create 添加一个最小化的 AsyncAPI 文件。

参数和选项

参数

  • 标题

vendor/bin/asyncapi schema:asyncapi:create "您的 Async API 标题" 将在您的 AsyncAPI 文件中设置标题。

...
info:
    title: 'Your Async API title'
...

选项

  • asyncapi-file
  • api-version

vendor/bin/asyncapi schema:asyncapi:create --asyncapi-file "path/to/async-api.yml" 将覆盖默认文件位置(resources/api/asyncapi.yml)。

vendor/bin/asyncapi schema:asyncapi:create --api-version 1.0.0 将覆盖默认文件版本(0.1.0)。

向 AsyncAPI 文件添加消息

vendor/bin/asyncapi schema:asyncapi:message:add 向给定的 AsyncAPI 文件添加消息。此命令也可以用于从现有的传输对象逆向工程。

此控制台命令有许多选项可以配置。运行以下命令查看所有选项:

vendor/bin/asyncapi schema:asyncapi:message:add -h

它将打印此命令的帮助页面。

添加订阅消息

要订阅特定通道的消息,需要按照以下方式运行命令:

vendor/bin/asyncapi schema:asyncapi:message:add foo-bar ZipZap ModuleName -e subscribe -P propertyA:string -P propertyB:int

这将在给定的 AsyncAPI 架构文件中添加一个订阅部分,描述发送名为 "ZipZap" 的消息,这些消息通过 "foo-bar" 通道发送,并且您发送了 "propertyA of type string" 和 "propertyB of type int" 属性。

您现在可以从此定义中创建代码。

添加发布消息

要接收特定通道的消息,需要按照以下方式运行命令:

vendor/bin/asyncapi schema:asyncapi:message:add foo-bar ZipZap ModuleName -e publish -P propertyA:string -P propertyB:int

这将在给定的 AsyncAPI 架构文件中添加一个发布部分,描述您想要接收名为 "ZipZap" 的消息,这些消息通过 "foo-bar" 通道发送,并且您想要使用 "propertyA of type string" 和 "propertyB of type int" 属性。

您现在可以从此定义中创建代码。

从给定的传输逆向工程

验证 AsyncAPI 文件

vendor/bin/asyncapi schema:asyncapi:validate 验证给定的 AsyncAPI 文件。

从现有的 AsyncAPI 创建代码

vendor/bin/asyncapi code:asyncapi:generate 读取现有的 AsyncAPI 文件,并从中创建代码。此命令创建

  • 消息传输定义(XML)
  • 为发送到应用程序的消息添加处理程序

选项

  • asyncapi-file,可用于使用特定 AsyncAPI 文件运行生成器
  • organization,可用于设置特定组织,当设置为 Spryker 时,代码将在核心模块中生成(默认:App)