Completed
Pull Request — master (#690)
by
unknown
01:27
created

RouteDocBlocker::getRouteCacheId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

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.

Loading history...
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
     * @return string
60
     */
61
    protected static function normalizeClassName($classNameOrInstance): string
62
    {
63
        if (is_object($classNameOrInstance)) {
64
            // route handlers are not destroyed until the script
65
            // ends so this should be perfectly safe.
66
            $classNameOrInstance = get_class($classNameOrInstance) . '::' . spl_object_id($classNameOrInstance);
67
        }
68
        return $classNameOrInstance;
69
    }
70
71
    protected static function getCachedDocBlock(Route $route, string $className, string $methodName)
72
    {
73
        $routeId = self::getRouteCacheId($route, $className, $methodName);
74
75
        return self::$docBlocks[$routeId] ?? null;
76
    }
77
78
    protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks)
79
    {
80
        $routeId = self::getRouteCacheId($route, $className, $methodName);
81
        self::$docBlocks[$routeId] = $docBlocks;
82
    }
83
84
    private static function getRouteCacheId(Route $route, string $className, string $methodName): string
85
    {
86
        return $route->uri()
87
            .':'
88
            .implode(array_diff($route->methods(), ['HEAD']))
89
            .$className
90
            .$methodName;
91
    }
92
}
93