定制 knuckleswtf/scribe-tags2attributes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

knuckleswtf/scribe-tags2attributes

Composer 安装命令:

composer require knuckleswtf/scribe-tags2attributes

包简介

Automatically convert most Scribe v3 docblock tags to v4 PHP 8 attributes

README 文档

README

This package provides a Rector rule to automatically convert most Scribe v3 docblock tags to v4 PHP 8 attributes.

This package will smartly transform the following tags on controller methods to their attribute equivalents:

  • header, urlParam, queryParam, and bodyParam
  • responseField, response and responseFile
  • apiResource,apiResourceCollection, and apiResourceModel
  • transformer, transformerCollection, and transformerModel
  • subgroup
  • authenticated and unauthenticated

It won't transform @group tags or endpoint titles and descriptions (because they can look ugly as attributes).

It will only work on methods in classes. Unfortunately, attributes can't be added to inline (closure) routes in a neat way.

Example:

  /*
   * Do a thing.
   *
   * Because you want to.
   *
   * @group Endpoints for doing things
-  * @subgroup Getting started
-  * @subgroupDescription Get started doing stuff
-  * @header Test Value
-  * @response 204 scenario="Nothing to see here"
-  * @apiResourceCollection App\Http\Resources\UserResource
-  * @apiResourceModel App\Models\User with=sideProjects,friends states=admin paginate=12,simple
-  * @responseFile 404 scenario="User not found" responses/not_found.json {"resource": "user"}
   */
+ #[Subgroup('Getting started', 'Get started doing stuff')]
+ #[Header('Test', 'Value')]
+ #[Response(status: 204, description: '204, Nothing to see here')]
+ #[ResponseFromApiResource(UserResource::class, User::class, collection: true, factoryStates: ['admin'], with: ['sideProjects', 'friends'], simplePaginate: 12)]
+ #[ResponseFromFile('responses/not_found.json', status: 404, merge: '{"resource": "user"}', description: '404, User not found')]
  public function doSomething()

Usage

  • Make sure the minimum PHP version in your composer.json is 8 (ie you should have "php": ">= 8.0" or similar in your "require" section).

  • Install this package

    composer require knuckleswtf/scribe-tags2attributes --dev
  • Run the Rector init command to create a rector.php file in the root of your project

    ./vendor/bin/rector init
  • Put this in the generated rector.php (delete whatever's in the file):

    <?php
    
    use Rector\Config\RectorConfig;
    
    return static function (RectorConfig $rectorConfig): void {
      $rectorConfig->disableParallel();
      $rectorConfig->paths([
        __DIR__ . '/app/Http/Controllers', // <- replace this with wherever your controllers are
      ]);
      $rectorConfig->importNames();
      $rectorConfig->rule(\Knuckles\Scribe\Tags2Attributes\RectorRule::class);
    };
  • Do a dry run. This will tell Rector to print out the changes that will be made, without actually making them. That way you can inspect and verify that it looks okay. We also recommend doing a git commit.

    ./vendor/bin/rector process --dry-run --clear-cache
  • When you're ready, run the command.

    ./vendor/bin/rector process --clear-cache
  • Make sure to add the attribute strategies to your config/scribe.php:

      'strategies' => [
          'metadata' => [
              Strategies\Metadata\GetFromDocBlocks::class,
    +         Strategies\Metadata\GetFromMetadataAttributes::class,
          ],
          'urlParameters' => [
              Strategies\UrlParameters\GetFromLaravelAPI::class,
              Strategies\UrlParameters\GetFromLumenAPI::class,
              Strategies\UrlParameters\GetFromUrlParamTag::class,
    +         Strategies\UrlParameters\GetFromUrlParamAttribute::class,
          ],
          'queryParameters' => [
              Strategies\QueryParameters\GetFromFormRequest::class,
              Strategies\QueryParameters\GetFromInlineValidator::class,
              Strategies\QueryParameters\GetFromQueryParamTag::class,
    +         Strategies\QueryParameters\GetFromQueryParamAttribute::class,
          ],
          'headers' => [
              Strategies\Headers\GetFromRouteRules::class,
              Strategies\Headers\GetFromHeaderTag::class,
    +         Strategies\Headers\GetFromHeaderAttribute::class,
          ],
          'bodyParameters' => [
              Strategies\BodyParameters\GetFromFormRequest::class,
              Strategies\BodyParameters\GetFromInlineValidator::class,
              Strategies\BodyParameters\GetFromBodyParamTag::class,
    +         Strategies\BodyParameters\GetFromBodyParamAttribute::class,
          ],
          'responses' => [
              Strategies\Responses\UseTransformerTags::class,
              Strategies\Responses\UseResponseTag::class,
              Strategies\Responses\UseResponseFileTag::class,
              Strategies\Responses\UseApiResourceTags::class,
    +         Strategies\Responses\UseResponseAttributes::class,
              Strategies\Responses\ResponseCalls::class,
          ],
          'responseFields' => [
              Strategies\ResponseFields\GetFromResponseFieldTag::class,
    +         Strategies\ResponseFields\GetFromResponseFieldAttribute::class,
          ],
      ],

All done! You can delete the rector.php file and run composer remove knuckleswtf/scribe-tags2attributes.

knuckleswtf/scribe-tags2attributes 适用场景与选型建议

knuckleswtf/scribe-tags2attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「documentation」 「api」 「laravel」 「Scribe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 knuckleswtf/scribe-tags2attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 knuckleswtf/scribe-tags2attributes 我们能提供哪些服务?
定制开发 / 二次开发

基于 knuckleswtf/scribe-tags2attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 91
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-25