knuckleswtf/scribe-tags2attributes

自动将大多数 Scribe v3 docblock 标签转换为 v4 PHP 8 属性

0.1.0 2022-09-08 19:28 UTC

This package is auto-updated.

Last update: 2024-09-10 07:24:40 UTC


README

此包提供了一个Rector规则,可自动将大多数 Scribe v3 docblock 标签转换为 v4 PHP 8 属性。

此包将智能地将以下标签从控制器方法转换为它们的属性等效项

  • headerurlParamqueryParambodyParam
  • responseFieldresponseresponseFile
  • apiResourceapiResourceCollectionapiResourceModel
  • transformertransformerCollectiontransformerModel
  • 子组
  • authenticatedunauthenticated

它不会转换@group标签或端点标题和描述(因为它们作为属性看起来会很丑)。

它只适用于类中的方法。不幸的是,无法以整洁的方式将属性添加到内联(闭包)路由中。

示例

  /*
   * 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()

用法

  • 请确保您的composer.json中的最小PHP版本为8(即您应该在"require"部分中有"php": ">= 8.0"或类似的设置)。

  • 安装此包

    composer require knuckleswtf/scribe-tags2attributes --dev
  • 运行Rector的init命令以在项目的根目录中创建一个rector.php文件

    ./vendor/bin/rector init
  • 将以下内容放入生成的rector.php中(删除文件中的任何内容)

    <?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);
    };
  • 进行dry run。这将告诉Rector打印出将要进行的更改,而不会实际进行更改。这样,您可以检查并验证它看起来是否正常。我们还建议执行git commit

    ./vendor/bin/rector process --dry-run --clear-cache
  • 准备好后,运行命令。

    ./vendor/bin/rector process --clear-cache
  • 请确保将属性策略添加到您的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,
          ],
      ],

完成!您可以删除rector.php文件并运行composer remove knuckleswtf/scribe-tags2attributes