Skip to content

Commit 5b7ba1e

Browse files
committed
Fix #121 Implement group
1 parent d6d063b commit 5b7ba1e

File tree

4 files changed

+403
-4
lines changed

4 files changed

+403
-4
lines changed

config/request-docs.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
// Can be overridden as // @LRDResponses 200|400|401
3838
'default_responses' => [ "200", "400", "401", "403", "404", "405", "422", "429", "500", "503"],
3939

40+
// By default, LRD group your routes by the first /path.
41+
// This is a set of regex to group your routes by prefix.
42+
'group_by' => [
43+
'uri_patterns' => [
44+
'^api/v[\d]+/', // `/api/v1/users/store` group as `/api/v1/users`.
45+
'^api/', // `/api/users/store` group as `/api/users`.
46+
]
47+
],
48+
4049
// No need to touch below
4150
// open api config
4251
// used to generate open api json

src/LaravelRequestDocs.php

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Rakutentech\LaravelRequestDocs;
44

5+
use Illuminate\Support\Collection;
56
use Route;
67
use ReflectionMethod;
78
use ReflectionClass;
@@ -118,11 +119,15 @@ public function filterByMethods($docs, $get, $post, $put, $path, $delete, $head)
118119

119120
public function groupDocs($docs, $group = 'default')
120121
{
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);
123128
}
124-
$grouped = [];//unimplemented
125-
return $docs; //unimplemented, returning as it is
129+
130+
return $docs;
126131
}
127132

128133
public function getControllersInfo(): array
@@ -363,4 +368,94 @@ private function multiexplode($delimiters, $string)
363368
$launch = explode($delimiters[0], $ready);
364369
return $launch;
365370
}
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+
}
366461
}

0 commit comments

Comments
 (0)