定制 knuckleswtf/scribe-tags2attributes 二次开发

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

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

knuckleswtf/scribe-tags2attributes

最新稳定版本:0.1.0

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.

统计信息

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

GitHub 信息

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

其他信息

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