| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\router;
|
|
|
18 |
|
|
|
19 |
use core\component;
|
|
|
20 |
use core\router\schema\specification;
|
|
|
21 |
use Psr\Http\Message\ResponseInterface;
|
|
|
22 |
use ReflectionClass;
|
|
|
23 |
use Throwable;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Moodle Router.
|
|
|
27 |
*
|
|
|
28 |
* @package core
|
|
|
29 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class apidocs {
|
|
|
33 |
/**
|
|
|
34 |
* Generate the API docs for the API.
|
|
|
35 |
*
|
|
|
36 |
* @param ResponseInterface $response
|
|
|
37 |
* @return ResponseInterface
|
|
|
38 |
*/
|
|
|
39 |
public function openapi_docs(
|
|
|
40 |
ResponseInterface $response,
|
|
|
41 |
): ResponseInterface {
|
|
|
42 |
global $PAGE;
|
|
|
43 |
$PAGE->set_context(\core\context\system::instance());
|
|
|
44 |
|
|
|
45 |
$api = new specification();
|
|
|
46 |
|
|
|
47 |
$classes = $this->get_openapi_classes_names();
|
|
|
48 |
foreach (array_keys($classes) as $classname) {
|
|
|
49 |
$classinfo = new ReflectionClass($classname);
|
|
|
50 |
[$component] = explode('\\', $classinfo->getNamespaceName());
|
|
|
51 |
|
|
|
52 |
$classroutes = $classinfo->getAttributes(route::class);
|
|
|
53 |
|
|
|
54 |
if ($classroutes) {
|
|
|
55 |
foreach ($classroutes as $classroute) {
|
|
|
56 |
$parentroute = $classroute->newInstance();
|
|
|
57 |
$this->get_api_docs_for_route(
|
|
|
58 |
component: $component,
|
|
|
59 |
classinfo: $classinfo,
|
|
|
60 |
api: $api,
|
|
|
61 |
parentcontexts: [$parentroute],
|
|
|
62 |
);
|
|
|
63 |
}
|
|
|
64 |
} else {
|
|
|
65 |
$this->get_api_docs_for_route(
|
|
|
66 |
component: $component,
|
|
|
67 |
classinfo: $classinfo,
|
|
|
68 |
api: $api,
|
|
|
69 |
);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// At the moment only json is supported. This could be extended to support other formats in future.
|
|
|
74 |
return $response
|
|
|
75 |
->withHeader('Content-Type', 'application/json')
|
|
|
76 |
->withBody(\GuzzleHttp\Psr7\Utils::streamFor(
|
|
|
77 |
json_encode(
|
|
|
78 |
$api,
|
|
|
79 |
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES,
|
|
|
80 |
),
|
|
|
81 |
));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Get the list of OpenAPI Class Names.
|
|
|
86 |
*
|
|
|
87 |
* @return string[]
|
|
|
88 |
*/
|
|
|
89 |
protected function get_openapi_classes_names(): array {
|
|
|
90 |
global $CFG;
|
|
|
91 |
|
|
|
92 |
$classes = [];
|
|
|
93 |
foreach (component::get_component_names(true) as $component) {
|
|
|
94 |
try {
|
|
|
95 |
$classes = array_merge(
|
|
|
96 |
$classes,
|
|
|
97 |
component::get_component_classes_in_namespace(
|
|
|
98 |
component: $component,
|
|
|
99 |
namespace: 'route\api',
|
|
|
100 |
),
|
|
|
101 |
);
|
|
|
102 |
// @codeCoverageIgnoreStart
|
|
|
103 |
} catch (Throwable $error) {
|
|
|
104 |
// Some kind of error occurred whilst loading routes in this component.
|
|
|
105 |
// When debugging, this is useful to know.
|
|
|
106 |
// When not, log to error_log.
|
|
|
107 |
if (!$CFG->debugdisplay) {
|
|
|
108 |
debugging('Error loading route data: ' . $error->getMessage());
|
|
|
109 |
} else {
|
|
|
110 |
default_exception_handler($error);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
// @codeCoverageIgnoreEnd
|
|
|
114 |
}
|
|
|
115 |
return $classes;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Get the API Docs for the specified Route.
|
|
|
121 |
*
|
|
|
122 |
* @param string $component The component that the route relates to
|
|
|
123 |
* @param ReflectionClass $classinfo
|
|
|
124 |
* @param specification $api
|
|
|
125 |
* @param array $parentcontexts
|
|
|
126 |
* @return self
|
|
|
127 |
*/
|
|
|
128 |
protected function get_api_docs_for_route(
|
|
|
129 |
string $component,
|
|
|
130 |
ReflectionClass $classinfo,
|
|
|
131 |
specification $api,
|
|
|
132 |
array $parentcontexts = [],
|
|
|
133 |
): self {
|
|
|
134 |
$methods = $classinfo->getMethods();
|
|
|
135 |
foreach ($methods as $method) {
|
|
|
136 |
if (!$method->isPublic()) {
|
|
|
137 |
continue;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
// Get the route attribute for this method.
|
|
|
141 |
$routeattribute = util::get_route_instance_for_method(
|
|
|
142 |
[$classinfo->getName(), $method->getName()],
|
|
|
143 |
);
|
|
|
144 |
|
|
|
145 |
if ($routeattribute === null) {
|
|
|
146 |
// This method has no route attribute. Maybe just a helper method.
|
|
|
147 |
continue;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Add this path to the OpenAPI specification.
|
|
|
151 |
$api->add_path(
|
|
|
152 |
component: $component,
|
|
|
153 |
route: $routeattribute,
|
|
|
154 |
);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
return $this;
|
|
|
158 |
}
|
|
|
159 |
}
|