somnambulist/api-bundle

添加了创建带有文档、响应处理等功能的API的基础设施。

安装次数: 4,701

依赖者: 1

建议者: 0

安全: 0

星标: 3

关注者: 4

分支: 1

开放问题: 0

类型:symfony-bundle

6.1.0 2024-08-19 13:59 UTC

README

GitHub Actions Build Status Issues License PHP Version Current Version

提供几个辅助器和支持对象,以更好地处理与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,改用 keymetainclude
  • 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+
  • UuidValueResolverExternalIdentityValueResolver 现在需要 somnambulist/domain 4.0+

v2的BC Breaks

从v2.0.0开始,以下更改已实施

  • 在整个库中使用PHP 7.4功能
  • 移除了 Services 命名空间组件
  • Converters 命名空间已更改为 ExceptionConverters
  • TransformersExceptionConverters 现在是 Response 命名空间的一部分
  • TransformerBinding 已被移除,改为使用具有特定接口的 Types
  • ApiControllerpaginatecollectionitem 方法现在是强类型
  • 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 运行测试。

链接