psx / api
解析和生成API规范格式
v7.1.6
2024-09-22 20:37 UTC
Requires
- php: >=8.1
- psx/json: ^3.0
- psx/openapi: ^0.1
- psx/schema: ^6.3
- symfony/cache: ^5.0|^6.0|^7.0
- symfony/console: ^5.0|^6.0|^7.0
- symfony/property-access: ^5.0|^6.0|^7.0
- symfony/yaml: ^5.0|^6.0|^7.0
- twig/twig: ^2.4|^3.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^5.0
- dev-master
- v7.1.6
- v7.1.5
- v7.1.4
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- 6.x-dev
- v6.2.4
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.9
- v6.1.8
- v6.1.7
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.9
- v6.0.8
- v6.0.7
- v6.0.6
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- 5.x-dev
- v5.4.3
- v5.4.2
- v5.4.1
- v5.4.0
- v5.3.7
- v5.3.6
- v5.3.5
- v5.3.4
- v5.3.3
- v5.3.2
- v5.3.1
- v5.3.0
- v5.2.9
- v5.2.8
- v5.2.7
- v5.2.6
- v5.2.5
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- 4.x-dev
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.x-dev
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-RC5
- v3.0.0-RC4
- v3.0.0-RC3
- v3.0.0-RC2
- v3.0.0-RC1
- 2.x-dev
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-09-22 20:39:01 UTC
README
API组件是TypeAPI规范的参考实现。它提供模型来描述REST API并根据这些模型生成不同的输出。您可以通过解析TypeAPI或OpenAPI规范或使用PHP属性来创建这些模型。基于这些模型,您可以生成例如客户端SDK。
用法
根模型对象被称为Specification
,它包含Operations
和Definitions
。每个操作映射到特定的REST API端点,定义表示描述JSON请求或响应负载的架构。
框架
您可以使用PHP属性来描述端点的结构。然后,您可以使用属性解析器(PSX\Api\Parser\Attribute
)自动生成控制器的规范。一个控制器类可能看起来像这样
<?php class MyController { #[Get] #[Path('/my/endpoint/:id')] public function getModel(#[Param] int $id, #[Query] int $year): \My\Response\Model { // @TODO implement } #[Post] #[Path('/my/endpoint')] public function insertModel(#[Body] \My\Request\Model $model): \My\Response\Model { // @TODO implement } }
这足以让API组件生成OpenAPI规范或客户端SDK。请注意,此库只需要元信息,如果您可以在框架中以另一种方式获取这些元信息,您也可以实现自定义的ParserInterface
。
独立
除了框架集成之外,您还可以使用此组件简单地解析现有的TypeAPI规范并生成特定输出。以下是一个如何使用PHP API以及如何生成代码的简单示例。
<?php // use the API manager to obtain a specification from different sources $manager = new \PSX\Api\ApiManager(new \PSX\Schema\SchemaManager()); // reads the TypeAPI specification and generates a specification $specification = $manager->getApi('./typeapi.json'); // contains all schema type definitions $definitions = $specification->getDefinitions(); // returns the resource foo from the specification $operation = $specification->getOperations()->get('my.operation'); // returns all available arguments $operation->getArguments(); // returns the return type $operation->getReturn(); // returns all exceptions which are described $operation->getThrows(); // returns the assigned HTTP method $operation->getMethod(); // returns the assigned HTTP path $operation->getPath(); // creates a PHP client which consumes the defined operations $registry = \PSX\Api\GeneratorFactory::fromLocal()->factory(); $generator = $registry->getGenerator(\PSX\Api\Repository\LocalRepository::CLIENT_PHP) $source = $generator->generate($resource);
生成器
客户端
- PHP
- Typescript
标记
- 客户端
- HTML
- Markdown