12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace Mpociot\ApiDoc\Extracting;
- use Illuminate\Routing\Route;
- use Mpociot\ApiDoc\Tools\Utils;
- use Mpociot\Reflection\DocBlock;
- use ReflectionClass;
- /**
- * Class RouteDocBlocker
- * Utility class to help with retrieving doc blocks from route classes and methods.
- * Also caches them so repeated access is faster.
- */
- class RouteDocBlocker
- {
- protected static $docBlocks = [];
- /**
- * @param Route $route
- *
- * @throws \ReflectionException
- * @throws \Exception
- *
- * @return array<string, DocBlock> Method and class docblocks
- */
- public static function getDocBlocksFromRoute(Route $route): array
- {
- list($className, $methodName) = Utils::getRouteClassAndMethodNames($route);
- $normalizedClassName = static::normalizeClassName($className);
- $docBlocks = self::getCachedDocBlock($route, $normalizedClassName, $methodName);
- if ($docBlocks) {
- return $docBlocks;
- }
- $class = new ReflectionClass($className);
- if (! $class->hasMethod($methodName)) {
- throw new \Exception("Error while fetching docblock for route: Class $className does not contain method $methodName");
- }
- $method = Utils::reflectRouteMethod([$className, $methodName]);
- $docBlocks = [
- 'method' => new DocBlock($method->getDocComment() ?: ''),
- 'class' => new DocBlock($class->getDocComment() ?: ''),
- ];
- self::cacheDocBlocks($route, $normalizedClassName, $methodName, $docBlocks);
- return $docBlocks;
- }
- /**
- * @param string|object $classNameOrInstance
- *
- * @return string
- */
- protected static function normalizeClassName($classNameOrInstance): string
- {
- if (is_object($classNameOrInstance)) {
- // route handlers are not destroyed until the script
- // ends so this should be perfectly safe.
- $classNameOrInstance = get_class($classNameOrInstance) . '::' . spl_object_id($classNameOrInstance);
- }
- return $classNameOrInstance;
- }
- protected static function getCachedDocBlock(Route $route, string $className, string $methodName)
- {
- $routeId = self::getRouteCacheId($route, $className, $methodName);
- return self::$docBlocks[$routeId] ?? null;
- }
- protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks)
- {
- $routeId = self::getRouteCacheId($route, $className, $methodName);
- self::$docBlocks[$routeId] = $docBlocks;
- }
- private static function getRouteCacheId(Route $route, string $className, string $methodName): string
- {
- return $route->uri()
- . ':'
- . implode(array_diff($route->methods(), ['HEAD']))
- . $className
- . $methodName;
- }
- }
|