1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Extracting; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
6
|
|
|
use Mpociot\ApiDoc\Tools\Utils; |
7
|
|
|
use Mpociot\Reflection\DocBlock; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionFunction; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RouteDocBlocker |
13
|
|
|
* Utility class to help with retrieving doc blocks from route classes and methods. |
14
|
|
|
* Also caches them so repeated access is faster. |
15
|
|
|
*/ |
16
|
|
|
class RouteDocBlocker |
17
|
|
|
{ |
18
|
|
|
protected static $docBlocks = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Route $route |
22
|
|
|
* |
23
|
|
|
* @throws \ReflectionException |
24
|
|
|
* @throws \Exception |
25
|
|
|
* |
26
|
|
|
* @return array<string, DocBlock> Method and class docblocks |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
public static function getDocBlocksFromRoute(Route $route): array |
29
|
|
|
{ |
30
|
|
|
list($className, $methodName) = Utils::getRouteClassAndMethodNames($route); |
31
|
|
|
$normalizedClassName = static::normalizeClassName($className); |
32
|
|
|
$docBlocks = self::getCachedDocBlock($route, $normalizedClassName, $methodName); |
33
|
|
|
|
34
|
|
|
if ($docBlocks) { |
35
|
|
|
return $docBlocks; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$class = new ReflectionClass($className); |
39
|
|
|
|
40
|
|
|
if (! $class->hasMethod($methodName)) { |
41
|
|
|
throw new \Exception("Error while fetching docblock for route: Class $className does not contain method $methodName"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$method = $className instanceof \Closure |
45
|
|
|
? new ReflectionFunction($className) |
46
|
|
|
: $class->getMethod($methodName); |
47
|
|
|
|
48
|
|
|
$docBlocks = [ |
49
|
|
|
'method' => new DocBlock($method->getDocComment() ?: ''), |
50
|
|
|
'class' => new DocBlock($class->getDocComment() ?: ''), |
51
|
|
|
]; |
52
|
|
|
self::cacheDocBlocks($route, $normalizedClassName, $methodName, $docBlocks); |
53
|
|
|
|
54
|
|
|
return $docBlocks; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string|object $classNameOrInstance |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected static function normalizeClassName($classNameOrInstance): string |
63
|
|
|
{ |
64
|
|
|
if (is_object($classNameOrInstance)) { |
65
|
|
|
// route handlers are not destroyed until the script |
66
|
|
|
// ends so this should be perfectly safe. |
67
|
|
|
$classNameOrInstance = get_class($classNameOrInstance).'::'.spl_object_id($classNameOrInstance); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $classNameOrInstance; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected static function getCachedDocBlock(Route $route, string $className, string $methodName) |
74
|
|
|
{ |
75
|
|
|
$routeId = self::getRouteCacheId($route, $className, $methodName); |
76
|
|
|
|
77
|
|
|
return self::$docBlocks[$routeId] ?? null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks) |
81
|
|
|
{ |
82
|
|
|
$routeId = self::getRouteCacheId($route, $className, $methodName); |
83
|
|
|
self::$docBlocks[$routeId] = $docBlocks; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function getRouteCacheId(Route $route, string $className, string $methodName): string |
87
|
|
|
{ |
88
|
|
|
return $route->uri() |
89
|
|
|
.':' |
90
|
|
|
.implode(array_diff($route->methods(), ['HEAD'])) |
91
|
|
|
.$className |
92
|
|
|
.$methodName; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.