|
| 1 | +# Scribe |
| 2 | + |
| 3 | + |
| 4 | +This package provides a [Rector](https://github.com/rectorphp/rector) rule to automatically convert most Scribe v3 docblock tags to v4 PHP 8 attributes. |
| 5 | + |
| 6 | +This package will smartly transform the following tags to their attribute equivalents: |
| 7 | + |
| 8 | +- `header` |
| 9 | +- `urlParam`, `queryParam`, and `bodyParam` |
| 10 | +- `responseField` |
| 11 | +- `response` |
| 12 | +- `responseFile` |
| 13 | +- `apiResource`,`apiResourceCollection`, and `apiResourceModel` |
| 14 | +- `transformer`, `transformerCollection`, and `transformerModel` |
| 15 | +- `subgroup` |
| 16 | +- `authenticated` and `unauthenticated` |
| 17 | + |
| 18 | +It won't transform `@group` tags or endpoint titles and descriptions (because they can look ugly as attributes). |
| 19 | + |
| 20 | +Example: |
| 21 | + |
| 22 | +```diff |
| 23 | + /* |
| 24 | + * Do a thing. |
| 25 | + * |
| 26 | + * Because you want to. |
| 27 | + * |
| 28 | + * @group Endpoints for doing things |
| 29 | +- * @subgroup Getting started |
| 30 | +- * @subgroupDescription Get started doing stuff |
| 31 | +- * @header Test Value |
| 32 | +- * @response 204 scenario="Nothing to see here" |
| 33 | +- * @apiResourceCollection App\Http\Resources\UserResource |
| 34 | +- * @apiResourceModel App\Models\User with=sideProjects,friends states=admin paginate=12,simple |
| 35 | +- * @responseFile 404 scenario="User not found" responses/not_found.json {"resource": "user"} |
| 36 | + */ |
| 37 | ++ #[Subgroup('Getting started', 'Get started doing stuff')] |
| 38 | ++ #[Header('Test', 'Value')] |
| 39 | ++ #[Response(status: 204, description: '204, Nothing to see here')] |
| 40 | ++ #[ResponseFromApiResource('App\Http\Resources\UserResource', 'App\Models\User', collection: true, factoryStates: ['admin'], with: ['sideProjects', 'friends'], simplePaginate: 12)] |
| 41 | ++ #[ResponseFromFile('responses/not_found.json', status: 404, merge: '{"resource": "user"}', description: '404, User not found')] |
| 42 | + public function doSomething() |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | +- Make sure the minimum PHP version in your `composer.json` is 8.0 (ie you should have `"php": ">= 8.0"` or similar in your `"require"` section). |
| 47 | +- Install this package |
| 48 | + ```sh |
| 49 | + composer require knuckleswtf/scribe-tags2attributes --dev |
| 50 | + ``` |
| 51 | + |
| 52 | +- Create a `rector.php` file in the root of your project |
| 53 | + ```sh |
| 54 | + ./vendor/bin/rector init |
| 55 | + ``` |
| 56 | + |
| 57 | +- Put this in the generated `rector.php` (delete whatever's in it): |
| 58 | + ```php |
| 59 | + <?php |
| 60 | + |
| 61 | + use Rector\Config\RectorConfig; |
| 62 | + |
| 63 | + return static function (RectorConfig $rectorConfig): void { |
| 64 | + $rectorConfig->disableParallel(); |
| 65 | + $rectorConfig->paths([ |
| 66 | + __DIR__ . '/app/Http/Controllers', // <- replace this with wherever your controllers are |
| 67 | + ]); |
| 68 | + $rectorConfig->importNames(); |
| 69 | + $rectorConfig->rule(Knuckles\Scribe\Docblock2Attributes\RectorRule::class); |
| 70 | + }; |
| 71 | + ``` |
| 72 | + |
| 73 | +- 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`. |
| 74 | + ```sh |
| 75 | + ./vendor/bin/rector process --dry-run --clear-cache |
| 76 | + ``` |
| 77 | + |
| 78 | +- When you're ready, run the command. |
| 79 | + ```sh |
| 80 | + ./vendor/bin/rector process --clear-cache |
| 81 | + ``` |
| 82 | + |
| 83 | +All done! You can delete the `rector.php` file and run `composer remove knuckleswtf/scribe-tags2attributes`. |
0 commit comments