Skip to content

Commit 2055699

Browse files
committed
- Added support for nested controller API docs
1 parent cf6f9be commit 2055699

File tree

3 files changed

+69
-42
lines changed

3 files changed

+69
-42
lines changed

docs/generating-api-docs/adding-additional-fields.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Use this method to specify additional query parameters for `index` endpoint.
1212
Will support all properties allowed by `Knuckles\Scribe\Attributes\QueryParam` Attribute.
1313

1414
```php
15-
// in Islands.php
15+
// in IslandsController.php
1616
public static function apiDocAdditionalIndexQueryParameters(): array
1717
{
1818
return [
@@ -37,7 +37,7 @@ Use this method to specify additional query parameters for `show` endpoint.
3737
Will support all properties allowed by `Knuckles\Scribe\Attributes\QueryParam` Attribute.
3838

3939
```php
40-
// in Islands.php
40+
// in IslandsController.php
4141
public static function apiDocAdditionalShowQueryParameters(): array
4242
{
4343
return [
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: API Docs for nested controllers
3+
sidebar_position: 4
4+
---
5+
6+
This package automatically determines the allowed sorts, fields, etc. for each controller by statitically creating a new controller instance and calling the relevant methods.
7+
While this works well for basic controllers, this can present an issue for nested controllers which may require parent models to dynamically determine the allowed values.
8+
9+
If a nested controller requires a parent model for determining the values, then you can override the `apiDocNewControllerInstance` static method and call the `setAdditionalParams` to set the parent model(s) to some value.
10+
11+
```php
12+
// in FormFieldsController.php
13+
public static function apiDocNewControllerInstance(): static
14+
{
15+
/** @var static $controller */
16+
$controller = app(static::class);
17+
18+
$form = \App\Models\Form::whereSlug('sample_form_for_docs')->first();
19+
20+
$controller->setAdditionalParams(compact('form'));
21+
22+
return $controller;
23+
}
24+
```
25+
26+
27+
28+
29+
30+
31+
32+
33+

src/Concerns/ApiDocHelpers.php

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,90 +108,84 @@ public static function apiDocAllAllowedFilters(): array
108108
);
109109
}
110110

111+
public static function apiDocNewControllerInstance(): static
112+
{
113+
return app(static::class);
114+
}
115+
111116
public static function apiDocDefaultSort(): string
112117
{
113-
/** @var self $new_instance */
114-
$new_instance = app(static::class);
118+
$new_instance = static::apiDocNewControllerInstance();
115119

116120
return $new_instance->getDefaultSort();
117121
}
118122

119123
public static function apiDocAllowedFilters(): array
120124
{
121-
/** @var self $new_instance */
122-
$new_instance = app(static::class);
125+
$new_instance = static::apiDocNewControllerInstance();
123126

124127
return $new_instance->getAllowedFilters();
125128
}
126129

127130
public static function apiDocAllowedIncludes(): array
128131
{
129-
/** @var self $new_instance */
130-
$new_instance = app(static::class);
132+
$new_instance = static::apiDocNewControllerInstance();
131133

132134
return $new_instance->getAllowedIncludes();
133135
}
134136

135137
public static function apiDocIndexAllowedFields(): array
136138
{
137-
/** @var self $new_instance */
138-
$new_instance = app(static::class);
139+
$new_instance = static::apiDocNewControllerInstance();
139140

140141
return array_merge($new_instance->getIndexAllowedFields(), $new_instance->getAllowedAppendAttributes());
141142
}
142143

143144
public static function apiDocIndexAllowedAppends(): array
144145
{
145-
/** @var self $new_instance */
146-
$new_instance = app(static::class);
146+
$new_instance = static::apiDocNewControllerInstance();
147147

148148
return $new_instance->getAllowedAppendAttributes();
149149
}
150150

151151
public static function apiDocShowAllowedFields(): array
152152
{
153-
/** @var self $new_instance */
154-
$new_instance = app(static::class);
153+
$new_instance = static::apiDocNewControllerInstance();
155154

156155
return array_merge($new_instance->getAllowedFields(), $new_instance->getShowAllowedAppendAttributes());
157156
}
158157

159158
public static function apiDocShowAllowedAppends(): array
160159
{
161-
/** @var self $new_instance */
162-
$new_instance = app(static::class);
160+
$new_instance = static::apiDocNewControllerInstance();
163161

164162
return $new_instance->getShowAllowedAppendAttributes();
165163
}
166164

167165
public static function apiDocAllowedSorts(): array
168166
{
169-
/** @var self $new_instance */
170-
$new_instance = app(static::class);
167+
$new_instance = static::apiDocNewControllerInstance();
171168

172169
return $new_instance->getAllowedSorts();
173170
}
174171

175172
public static function apiDocDefaultPerPage(): int
176173
{
177-
/** @var self $new_instance */
178-
$new_instance = app(static::class);
174+
$new_instance = static::apiDocNewControllerInstance();
179175

180176
return $new_instance->getDefaultPerPage();
181177
}
182178

183179
public static function apiDocMaxPerPage(): int
184180
{
185-
/** @var self $new_instance */
186-
$new_instance = app(static::class);
181+
$new_instance = static::apiDocNewControllerInstance();
187182

188183
return $new_instance->getMaxPerPage();
189184
}
190185

191186
public static function apiDocAllowUnlimitedResultsPerPage(): bool
192187
{
193-
/** @var self $new_instance */
194-
$new_instance = app(static::class);
188+
$new_instance = static::apiDocNewControllerInstance();
195189

196190
return $new_instance->allowUnlimitedResultsPerPage();
197191
}
@@ -221,8 +215,8 @@ public static function apiDocGenerateFieldsMetadata(array $fields): array
221215

222216
public static function apiDefaultSortsDescription(): string
223217
{
224-
return 'Which fields to sort the results by. '.
225-
'<br>To sort in descending order, append a `-` to the field name, e.g. `?sort=-created_at`. '.
218+
return 'Which fields to sort the results by. ' .
219+
'<br>To sort in descending order, append a `-` to the field name, e.g. `?sort=-created_at`. ' .
226220
'<br>To sort by multiple fields, provide a comma-separated list, e.g. `?sort=id,-created_at`. ';
227221
}
228222

@@ -236,7 +230,7 @@ public static function apiDocGenerateSortsMetadata(array $sorts, string $default
236230
return [
237231
'type' => 'string',
238232
'description' => static::apiSortsDescription() .
239-
'<br><br>**Allowed sorts:** ' . "\n" . implode("\n", array_map(fn ($field) => "- `$field`", $sorts)) . "\n\n" .
233+
'<br><br>**Allowed sorts:** ' . "\n" . implode("\n", array_map(fn($field) => "- `$field`", $sorts)) . "\n\n" .
240234
'<br>**Default sort:** ' . ($default_sort ? '`' . $default_sort . '`' : 'None'),
241235
'enum' => static::apiDocAllowedSorts(),
242236
'example' => static::apiDocDefaultSort(),
@@ -294,8 +288,8 @@ public static function apiDocGeneratePerPageMetadata(): array
294288
{
295289
return [
296290
'type' => 'integer',
297-
'description' => 'How many results to return per page. '.
298-
(static::apiDocAllowUnlimitedResultsPerPage() ? '<br></br>To return all results, set `per_page` to `-1`' : '').
291+
'description' => 'How many results to return per page. ' .
292+
(static::apiDocAllowUnlimitedResultsPerPage() ? '<br></br>To return all results, set `per_page` to `-1`' : '') .
299293
'<br>**Max per page:** ' . static::apiDocMaxPerPage() .
300294
'<br>**Default per page:** ' . static::apiDocDefaultPerPage(),
301295
'example' => static::apiDocDefaultPerPage(),
@@ -306,16 +300,16 @@ public static function apiDocGeneratePageMetadata(): array
306300
{
307301
return [
308302
'type' => 'integer',
309-
'description' => 'For paginated results, which page to return.' ,
303+
'description' => 'For paginated results, which page to return.',
310304
'example' => 1,
311305
];
312306
}
313307

314308
public static function apiDocGenerateFilterMetadata(
315-
string $filter_name,
309+
string $filter_name,
316310
string|AllowedFilter $filter,
317-
string $singular_resource_name,
318-
array $metadata = [],
311+
string $singular_resource_name,
312+
array $metadata = [],
319313
): array
320314
{
321315
$filter_title = Str::of($filter_name)
@@ -327,20 +321,20 @@ public static function apiDocGenerateFilterMetadata(
327321

328322
return array_merge([
329323
'type' => 'string',
330-
'description' => is_string($filter) ? 'Filter by the ' . Str::lower($filter_title) . ' of the ' . $singular_resource_name : 'Apply the ' . Str::lower($filter_title) . ' filter',
324+
'description' => is_string($filter) ? 'Filter by the ' . Str::lower($filter_title) . ' of the ' . $singular_resource_name : 'Apply the ' . Str::lower($filter_title) . ' filter',
331325
], $metadata);
332326
}
333327

334328

335329
public static function apiDocDefaultQueryParameters(
336-
array $fields = [],
337-
array $sorts = [],
330+
array $fields = [],
331+
array $sorts = [],
338332
string $default_sort = '',
339-
array $appends = [],
340-
array $includes = [],
341-
array $filters = [],
342-
array $filter_metadata = [],
343-
bool $include_pagination = false
333+
array $appends = [],
334+
array $includes = [],
335+
array $filters = [],
336+
array $filter_metadata = [],
337+
bool $include_pagination = false
344338
): array
345339
{
346340
$params = [];
@@ -379,7 +373,7 @@ public static function apiDocDefaultQueryParameters(
379373
$filter_name = $filter->getName();
380374
}
381375

382-
if (! $filter_name) {
376+
if (!$filter_name) {
383377
continue;
384378
}
385379

0 commit comments

Comments
 (0)