|
2 | 2 |
|
3 | 3 | namespace Rakutentech\LaravelRequestDocs;
|
4 | 4 |
|
| 5 | +use Illuminate\Support\Collection; |
5 | 6 | use Route;
|
6 | 7 | use ReflectionMethod;
|
7 | 8 | use ReflectionClass;
|
@@ -118,11 +119,15 @@ public function filterByMethods($docs, $get, $post, $put, $path, $delete, $head)
|
118 | 119 |
|
119 | 120 | public function groupDocs($docs, $group = 'default')
|
120 | 121 | {
|
121 |
| - if ($group == 'default') { |
122 |
| - return $docs; |
| 122 | + if ($group === 'api_uri') { |
| 123 | + return $this->groupDocsByAPIURI($docs); |
| 124 | + } |
| 125 | + |
| 126 | + if ($group === 'controller_full_path') { |
| 127 | + return $this->groupDocsByFQController($docs); |
123 | 128 | }
|
124 |
| - $grouped = [];//unimplemented |
125 |
| - return $docs; //unimplemented, returning as it is |
| 129 | + |
| 130 | + return $docs; |
126 | 131 | }
|
127 | 132 |
|
128 | 133 | public function getControllersInfo(): array
|
@@ -363,4 +368,94 @@ private function multiexplode($delimiters, $string)
|
363 | 368 | $launch = explode($delimiters[0], $ready);
|
364 | 369 | return $launch;
|
365 | 370 | }
|
| 371 | + |
| 372 | + /** |
| 373 | + * Parse the `$docs['uri']` and attach `group` and `group_index` details. |
| 374 | + * |
| 375 | + * @param array $docs |
| 376 | + * @return array $docs |
| 377 | + */ |
| 378 | + private function groupDocsByAPIURI(array $docs): array |
| 379 | + { |
| 380 | + $patterns = config('request-docs.group_by.uri_patterns', []); |
| 381 | + |
| 382 | + $regex = count($patterns) > 0 ? '(' . implode('|', $patterns) . ')' : ''; |
| 383 | + |
| 384 | + // A collection<string, int> to remember indexes with `group` => `index` pair. |
| 385 | + $groupIndexes = collect(); |
| 386 | + |
| 387 | + foreach ($docs as $i => $doc) { |
| 388 | + if ($regex !== '') { |
| 389 | + // If $regex = '^api/v[\d]+/', |
| 390 | + // and $uri = '/api/v1/users', |
| 391 | + // then $prefix = '/api/v1/'. |
| 392 | + $prefix = Str::match($regex, $doc['uri']); |
| 393 | + } |
| 394 | + |
| 395 | + $group = $this->getGroupByURI($prefix ?? '', $doc['uri']); |
| 396 | + $groupIndexes = $this->rememberGroupIndex($groupIndexes, $group); |
| 397 | + $docs[$i] = $this->attachGroupInfo($doc, $group, $groupIndexes->get($group)); |
| 398 | + } |
| 399 | + |
| 400 | + return $docs; |
| 401 | + } |
| 402 | + |
| 403 | + /** |
| 404 | + * Create and return group name by the `$uri`. |
| 405 | + */ |
| 406 | + private function getGroupByURI(string $prefix, string $uri): string |
| 407 | + { |
| 408 | + if ($prefix === '') { |
| 409 | + // No prefix, create group by the first path. |
| 410 | + $paths = explode('/', $uri); |
| 411 | + return $paths[0]; |
| 412 | + } |
| 413 | + |
| 414 | + // Glue the prefix + "first path after prefix" to form a group. |
| 415 | + $after = (Str::after($uri, $prefix)); |
| 416 | + $paths = explode('/', $after); |
| 417 | + return $prefix . $paths[0]; |
| 418 | + } |
| 419 | + |
| 420 | + /** |
| 421 | + * Parse the `$docs['controller_full_path']` and attach `group` and `group_index` details. |
| 422 | + */ |
| 423 | + private function groupDocsByFQController(array $docs): array |
| 424 | + { |
| 425 | + // To remember group indexes with group => index pair. |
| 426 | + $groupIndexes = collect(); |
| 427 | + |
| 428 | + foreach ($docs as $i => $doc) { |
| 429 | + $group = $doc['controller_full_path']; |
| 430 | + $groupIndexes = $this->rememberGroupIndex($groupIndexes, $group); |
| 431 | + $docs[$i] = $this->attachGroupInfo($doc, $group, $groupIndexes->get($group)); |
| 432 | + } |
| 433 | + return $docs; |
| 434 | + } |
| 435 | + |
| 436 | + /** |
| 437 | + * Set the last index number into `$groupIndexes` |
| 438 | + * |
| 439 | + * @param \Illuminate\Support\Collection<string, int> $groupIndexes [`group` => `index`] |
| 440 | + */ |
| 441 | + private function rememberGroupIndex(Collection $groupIndexes, string $key): Collection |
| 442 | + { |
| 443 | + if (!$groupIndexes->has($key)) { |
| 444 | + $groupIndexes->put($key, 0); |
| 445 | + return $groupIndexes; |
| 446 | + } |
| 447 | + |
| 448 | + $groupIndexes->put($key, $groupIndexes->get($key) + 1); |
| 449 | + return $groupIndexes; |
| 450 | + } |
| 451 | + |
| 452 | + /** |
| 453 | + * Attach `group` and `group_index` into `$doc`. |
| 454 | + */ |
| 455 | + private function attachGroupInfo(array $doc, string $group, int $groupIndex): array |
| 456 | + { |
| 457 | + $doc['group'] = $group; |
| 458 | + $doc['group_index'] = $groupIndex; |
| 459 | + return $doc; |
| 460 | + } |
366 | 461 | }
|
0 commit comments