1 <?php namespace BookStack\Api;
3 use BookStack\Http\Controllers\Api\ApiController;
4 use Illuminate\Support\Collection;
5 use Illuminate\Support\Facades\Route;
7 use ReflectionException;
10 class ApiDocsGenerator
13 protected $reflectionClasses = [];
14 protected $controllerClasses = [];
17 * Generate API documentation.
19 public function generate(): Collection
21 $apiRoutes = $this->getFlatApiRoutes();
22 $apiRoutes = $this->loadDetailsFromControllers($apiRoutes);
23 $apiRoutes = $this->loadDetailsFromFiles($apiRoutes);
24 $apiRoutes = $apiRoutes->groupBy('base_model');
29 * Load any API details stored in static files.
31 protected function loadDetailsFromFiles(Collection $routes): Collection
33 return $routes->map(function (array $route) {
34 $exampleTypes = ['request', 'response'];
35 foreach ($exampleTypes as $exampleType) {
36 $exampleFile = base_path("dev/api/{$exampleType}s/{$route['name']}.json");
37 $exampleContent = file_exists($exampleFile) ? file_get_contents($exampleFile) : null;
38 $route["example_{$exampleType}"] = $exampleContent;
45 * Load any details we can fetch from the controller and its methods.
47 protected function loadDetailsFromControllers(Collection $routes): Collection
49 return $routes->map(function (array $route) {
50 $method = $this->getReflectionMethod($route['controller'], $route['controller_method']);
51 $comment = $method->getDocComment();
52 $route['description'] = $comment ? $this->parseDescriptionFromMethodComment($comment) : null;
53 $route['body_params'] = $this->getBodyParamsFromClass($route['controller'], $route['controller_method']);
59 * Load body params and their rules by inspecting the given class and method name.
60 * @throws \Illuminate\Contracts\Container\BindingResolutionException
62 protected function getBodyParamsFromClass(string $className, string $methodName): ?array
64 /** @var ApiController $class */
65 $class = $this->controllerClasses[$className] ?? null;
66 if ($class === null) {
67 $class = app()->make($className);
68 $this->controllerClasses[$className] = $class;
71 $rules = $class->getValdationRules()[$methodName] ?? [];
72 foreach ($rules as $param => $ruleString) {
73 $rules[$param] = explode('|', $ruleString);
75 return count($rules) > 0 ? $rules : null;
79 * Parse out the description text from a class method comment.
81 protected function parseDescriptionFromMethodComment(string $comment)
84 preg_match_all('/^\s*?\*\s((?![@\s]).*?)$/m', $comment, $matches);
85 return implode(' ', $matches[1] ?? []);
89 * Get a reflection method from the given class name and method name.
90 * @throws ReflectionException
92 protected function getReflectionMethod(string $className, string $methodName): ReflectionMethod
94 $class = $this->reflectionClasses[$className] ?? null;
95 if ($class === null) {
96 $class = new ReflectionClass($className);
97 $this->reflectionClasses[$className] = $class;
100 return $class->getMethod($methodName);
104 * Get the system API routes, formatted into a flat collection.
106 protected function getFlatApiRoutes(): Collection
108 return collect(Route::getRoutes()->getRoutes())->filter(function ($route) {
109 return strpos($route->uri, 'api/') === 0;
110 })->map(function ($route) {
111 [$controller, $controllerMethod] = explode('@', $route->action['uses']);
112 $baseModelName = explode('.', explode('/', $route->uri)[1])[0];
113 $shortName = $baseModelName . '-' . $controllerMethod;
115 'name' => $shortName,
116 'uri' => $route->uri,
117 'method' => $route->methods[0],
118 'controller' => $controller,
119 'controller_method' => $controllerMethod,
120 'base_model' => $baseModelName,