somnambulist / api-bundle
添加了创建带有文档、响应处理等功能的API的基础设施。
Requires
- php: ^8.1
- ext-json: *
- psr/log: ^1|^2|^3
- ramsey/uuid: ^4.0
- somnambulist/form-request-bundle: ^2.0
- somnambulist/fractal-bundle: ^2.0
- symfony/framework-bundle: ^6.4|^7.0
Requires (Dev)
- doctrine/collections: ~1.8
- monolog/monolog: ^3.4
- pagerfanta/pagerfanta: ^3.8
- phpunit/phpunit: ^10.5
- somnambulist/api-client: ^4.0.0
- somnambulist/domain: ^6.0.0
- somnambulist/read-models: ^4.0
- symfony/browser-kit: ^6.4|^7.0
- symfony/messenger: ^6.4|^7.0
- symfony/monolog-bundle: ~3.5
- symfony/phpunit-bridge: ^6.4|^7.0
- symfony/serializer: ^6.4|^7.0
- symfony/stopwatch: ^6.4|^7.0
- symfony/twig-bundle: ^6.4|^7.0
- symfony/var-dumper: ^6.4|^7.0
- symfony/yaml: ^6.4|^7.0
Suggests
- pagerfanta/pagerfanta: Pagination support expects Pagerfanta collections
- symfony/stopwatch: Add profiling data from using fractal
- symfony/twig-bundle: Required to use OpenAPI documentation generator
- dev-master
- 6.1.0
- 6.0.1
- 6.0.0
- 5.x-dev
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.0
- 5.0.1
- 5.0.0
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.8.0
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.0
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-19 14:15:54 UTC
README
提供几个辅助器和支持对象,以更好地处理与Symfony的League Fractal集成。与Fractal的集成基于Dingo API for Laravel: https://github.com/dingo/api
要求
- PHP 8.1+
- somnambulist/fractal-bundle
- symfony/twig-bundle (用于文档输出)
安装
使用composer安装,或从github.com检查/拉取文件。
- composer require somnambulist/api-bundle
用法
将 SomnambulistApiBundle
添加到您的 bundles.php
列表中,并在 packages
中添加一个配置文件,如果您希望配置该捆绑包。以下选项可以设置
somnambulist_api: exception_handler: converters: Assert\InvalidArgumentException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\AssertionExceptionConverter Assert\LazyAssertionException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\LazyAssertionExceptionConverter Doctrine\ORM\EntityNotFoundException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\EntityNotFoundConverter Doctrine\ORM\NoResultException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\EntityNotFoundConverter Somnambulist\Components\Models\Exceptions\EntityNotFoundException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\EntityNotFoundConverter Somnambulist\Components\ReadModels\Exceptions\EntityNotFoundException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\EntityNotFoundConverter Somnambulist\Components\ReadModels\Exceptions\NoResultsException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\EntityNotFoundConverter Symfony\Component\Messenger\Exception\HandlerFailedException: Somnambulist\Bundles\ApiBundle\Response\ExceptionConverters\HandlerFailedExceptionConverter api_root: '/api' doc_root: '/api/docs' subscribers: exception_to_json: true json_to_post: true request_id: true openapi: path: '%kernel.project_dir%/config/openapi' title: 'API Docs' version: '1.0.0' description: 'The documentation for the API' cache_time: 1400 # cache time in seconds for the generated docs tags: tag_name: "Description for the tag"
扩展捆绑的 ApiController 并构建您的API。
可选地,您可以
V5的BC Breaks
V5删除了所有之前已弃用的方法,并对变换器类型的签名进行了更改。变换器类型现在是只读的,一旦实例化就无法修改。这意味着构造函数参数已更改,以包括所有选项,并已修订顺序。
所有响应类型默认为 data
键名,包括之前为null的 ObjectType
。现在,您必须指定一个null键来删除它。使用 data
键可以更容易地将元数据应用于响应对象。
V4的BC Breaks
V4更新到league/fractal 0.20.0。这为属性和方法添加了许多类型提示。特别是在 TransformerAbstract
中,各种可覆盖的数组现在需要 array
类型提示。
此外
- 移除了
RequestArgumentHelper
的使用,改用FormRequests,因为它们提供了一个更干净、自文档化的API - 从捆绑包配置中移除了arg helper的配置
- 从
Response\AbstractType
中移除了withXXX
,改用key
、meta
和include
AbstractType::include()
方法现在只接受多个字符串参数- 请求id头字符串现在通过参数设置(请参阅文档)
V3.5.0中的捆绑包更改
从3.5.0开始,samj/fractal-bundle
已被替换为 somnambulist/fractal-bundle
,因为samj已被废弃和存档。该替换提供相同的服务解析,并允许变换器被标记以及自动配置。
必要的唯一代码更改是在 bundles.php
中将 SamjFractalBundle
替换为 SomnambulistFractalBundle
。
V3的BC Breaks
从v3.0.0开始,该库已重命名为 Somanmbulist\Bundles\ApiBundle
。请确保更新所有引用。
此外
ReadModelTransformer
现在需要 somnambulist/read-models 2.0+UuidValueResolver
和ExternalIdentityValueResolver
现在需要 somnambulist/domain 4.0+
v2的BC Breaks
从v2.0.0开始,以下更改已实施
- 在整个库中使用PHP 7.4功能
- 移除了
Services
命名空间组件 Converters
命名空间已更改为ExceptionConverters
Transformers
和ExceptionConverters
现在是Response
命名空间的一部分TransformerBinding
已被移除,改为使用具有特定接口的Types
ApiController
的paginate
、collection
、item
方法现在是强类型withIncludes
方法现在接受多个字符串参数,而不是数组- 所有转换器都应该注册为容器服务(转换器现在是一个显式的字符串)
要从 TransformerBinding
切换,请将每个调用替换为
TransformerBinding::item()
替换为new ObjectType()
TransformerBinding::collection()
替换为new CollectionType()
或new IterableType()
TransformerBinding::paginate()
替换为new PagerfantaType()
用于 Pagerfanta。
构造函数签名大致相同;除了集合和分页器外,最后一个参数为额外的 key
,默认为 data
。
在更新时,请记住如果使用包含的默认设置,则更新您的 somnambulist.yaml 配置文件中的异常转换器。
测试
使用 PHPUnit 9+ 进行测试。通过 vendor/bin/phpunit
运行测试。