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, andbodyParamresponseField,responseandresponseFileapiResource,apiResourceCollection, andapiResourceModeltransformer,transformerCollection, andtransformerModelsubgroupauthenticatedandunauthenticated
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.jsonis 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
initcommand to create arector.phpfile 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 knuckleswtf/scribe-tags2attributes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Laravel package that generates RESTful API documentation in Markdown based on PHPDoc.
A PSR-7 compatible library for making CRUD API endpoints
Laravel API documentation generating tool
Alfabank REST API integration
RESTful API server for structured and self-documenting resources over JSON, XML, ...
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-25