Skip to content

Commit a9f8bb5

Browse files
committed
add one more usage case
1 parent 867f63b commit a9f8bb5

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/Commands/GenerateDocumentation.php

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

33
namespace Mpociot\ApiDoc\Commands;
44

5+
use Mpociot\ApiDoc\Tools\Utils;
56
use ReflectionClass;
67
use ReflectionException;
78
use Illuminate\Routing\Route;
@@ -207,7 +208,7 @@ private function processRoutes(Generator $generator, array $routes)
207208
foreach ($routes as $routeItem) {
208209
$route = $routeItem['route'];
209210
/** @var Route $route */
210-
if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction()['uses'])) {
211+
if ($this->isValidRoute($route) && $this->isRouteVisibleForDocumentation($route->getAction())) {
211212
$parsedRoutes[] = $generator->processRoute($route, $routeItem['apply']);
212213
$this->info('Processed route: ['.implode(',', $generator->getMethods($route)).'] '.$generator->getUri($route));
213214
} else {
@@ -225,7 +226,7 @@ private function processRoutes(Generator $generator, array $routes)
225226
*/
226227
private function isValidRoute(Route $route)
227228
{
228-
$action = $route->getAction()['uses'];
229+
$action = Utils::getRouteActionUses($route->getAction());
229230
if (is_array($action)) {
230231
$action = implode('@', $action);
231232
}
@@ -234,15 +235,15 @@ private function isValidRoute(Route $route)
234235
}
235236

236237
/**
237-
* @param $route
238-
*
239-
* @throws ReflectionException
238+
* @param $action
240239
*
241240
* @return bool
241+
*@throws ReflectionException
242+
*
242243
*/
243-
private function isRouteVisibleForDocumentation($route)
244+
private function isRouteVisibleForDocumentation($action)
244245
{
245-
list($class, $method) = is_array($route) ? $route : explode('@', $route);
246+
list($class, $method) = Utils::getRouteActionUses($action);
246247
$reflection = new ReflectionClass($class);
247248

248249
if (! $reflection->hasMethod($method)) {
@@ -255,7 +256,7 @@ private function isRouteVisibleForDocumentation($route)
255256
$phpdoc = new DocBlock($comment);
256257

257258
return collect($phpdoc->getTags())
258-
->filter(function ($tag) use ($route) {
259+
->filter(function ($tag) use ($action) {
259260
return $tag->getName() === 'hideFromAPIDocumentation';
260261
})
261262
->isEmpty();

src/Tools/Generator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public function getMethods(Route $route)
5353
*/
5454
public function processRoute(Route $route, array $rulesToApply = [])
5555
{
56-
$action = $route->getAction();
57-
if (is_array($action) && array_key_exists('uses', $action)) {
58-
$action = $action['uses'];
59-
}
60-
list($class, $method) = is_array($action) ? $action : explode('@', $action);
56+
list($class, $method) = Utils::getRouteActionUses($route->getAction());
6157
$controller = new ReflectionClass($class);
6258
$method = $controller->getMethod($method);
6359

src/Tools/Utils.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ public static function getFullUrl(Route $route, array $bindings = []): string
1414
return self::replaceUrlParameterBindings($uri, $bindings);
1515
}
1616

17+
public static function getRouteActionUses(array $action): ?array
18+
{
19+
if ($action['uses'] !== null) {
20+
if (is_array($action['uses'])) {
21+
return $action['uses'];
22+
}
23+
elseif (is_string($action['uses'])) {
24+
return explode('@', $action['uses']);
25+
}
26+
}
27+
if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
28+
return [
29+
0 => $action[0],
30+
1 => $action[1]
31+
];
32+
}
33+
34+
return null;
35+
}
36+
1737
/**
1838
* Transform parameters in URLs into real values (/users/{user} -> /users/2).
1939
* Uses bindings specified by caller, otherwise just uses '1'.

0 commit comments

Comments
 (0)