| 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 Slim\App;
|
|
|
20 |
use Slim\Interfaces\RouteGroupInterface;
|
|
|
21 |
use Slim\Interfaces\RouteInterface;
|
|
|
22 |
use Slim\Routing\RouteCollectorProxy;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Route Loader and Discovery agent.
|
|
|
26 |
*
|
|
|
27 |
* @package core
|
|
|
28 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class route_loader extends abstract_route_loader implements route_loader_interface {
|
|
|
32 |
#[\Override]
|
|
|
33 |
public function configure_routes(App $app): array {
|
|
|
34 |
return [
|
|
|
35 |
route_loader_interface::ROUTE_GROUP_API => $this->configure_api_routes($app, route_loader_interface::ROUTE_GROUP_API),
|
|
|
36 |
route_loader_interface::ROUTE_GROUP_PAGE => $this->configure_standard_routes($app),
|
|
|
37 |
route_loader_interface::ROUTE_GROUP_SHIM => $this->configure_shim_routes($app),
|
|
|
38 |
];
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Configure all API routes.
|
|
|
43 |
*
|
|
|
44 |
* @param App $app
|
|
|
45 |
* @param string $path
|
|
|
46 |
* @return RouteGroupInterface
|
|
|
47 |
*/
|
|
|
48 |
protected function configure_api_routes(App $app, string $path): RouteGroupInterface {
|
|
|
49 |
return $app->group($path, function (
|
|
|
50 |
RouteCollectorProxy $group,
|
|
|
51 |
): void {
|
|
|
52 |
// Add all API routes located in the route\api L2\L3 namespace.
|
|
|
53 |
foreach ($this->get_all_api_routes() as $apiroute) {
|
|
|
54 |
$slimroute = $group->map(...$apiroute);
|
|
|
55 |
$this->set_route_name_for_callable($slimroute, $apiroute['callable']);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Add the OpenAPI docs route.
|
|
|
59 |
$callable = [apidocs::class, 'openapi_docs'];
|
|
|
60 |
$slimroute = $group->get('/openapi.json', $callable);
|
|
|
61 |
$this->set_route_name_for_callable($slimroute, $callable);
|
|
|
62 |
});
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Configure all standard routes.
|
|
|
67 |
*
|
|
|
68 |
* @param App $app
|
|
|
69 |
* @return RouteGroupInterface
|
|
|
70 |
*/
|
|
|
71 |
protected function configure_standard_routes(App $app): RouteGroupInterface {
|
|
|
72 |
return $app->group('', function (RouteCollectorProxy $group): void {
|
|
|
73 |
foreach ($this->get_all_standard_routes() as $moodleroute) {
|
|
|
74 |
$slimroute = $group->map(...$moodleroute);
|
|
|
75 |
$this->set_route_name_for_callable($slimroute, $moodleroute['callable']);
|
|
|
76 |
}
|
|
|
77 |
});
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Configure all route shims.
|
|
|
82 |
*
|
|
|
83 |
* @param App $app
|
|
|
84 |
* @return RouteGroupInterface
|
|
|
85 |
*/
|
|
|
86 |
protected function configure_shim_routes(App $app): RouteGroupInterface {
|
|
|
87 |
return $app->group('', function (RouteCollectorProxy $group): void {
|
|
|
88 |
foreach ($this->get_all_shimmed_routes() as $moodleroute) {
|
|
|
89 |
$slimroute = $group->map(...$moodleroute);
|
|
|
90 |
$this->set_route_name_for_callable($slimroute, $moodleroute['callable']);
|
|
|
91 |
}
|
|
|
92 |
});
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Fetch all API routes.
|
|
|
97 |
*
|
|
|
98 |
* Note: This method caches results in MUC.
|
|
|
99 |
*
|
|
|
100 |
* @return array[]
|
|
|
101 |
*/
|
|
|
102 |
protected function get_all_api_routes(): array {
|
|
|
103 |
$cache = \cache::make('core', 'routes');
|
|
|
104 |
|
|
|
105 |
if (!($routes = $cache->get('api_routes'))) {
|
|
|
106 |
$routes = $this->get_all_routes_in_namespace(
|
|
|
107 |
namespace: 'route\api',
|
|
|
108 |
componentpathcallback: $this->normalise_component_path(...),
|
|
|
109 |
);
|
|
|
110 |
|
|
|
111 |
$cache->set('api_routes', $routes);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return $routes;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Fetch all shimmed routes.
|
|
|
119 |
*
|
|
|
120 |
* Shimmed routes are routes that are not part of the standard route namespace but allow backwards compatibility with
|
|
|
121 |
* pages which have been moved to the new routing system.
|
|
|
122 |
*
|
|
|
123 |
* Note: This method caches results in MUC.
|
|
|
124 |
*
|
|
|
125 |
* @return array|bool|mixed
|
|
|
126 |
*/
|
|
|
127 |
protected function get_all_shimmed_routes(): array {
|
|
|
128 |
$cache = \cache::make('core', 'routes');
|
|
|
129 |
|
|
|
130 |
if (!($cachedata = $cache->get('shimmed_routes'))) {
|
|
|
131 |
$cachedata = $this->get_all_routes_in_namespace(
|
|
|
132 |
namespace: 'route\shim',
|
|
|
133 |
componentpathcallback: function (string $component): string {
|
|
|
134 |
global $CFG;
|
|
|
135 |
if ($component === 'core') {
|
|
|
136 |
// The core component is a special case.
|
|
|
137 |
// It can place routes _anywhere_ in the codebase.
|
|
|
138 |
return '';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// Use the component directory path listed in \core\componnet.
|
|
|
142 |
return substr(
|
|
|
143 |
\core_component::get_component_directory($component),
|
|
|
144 |
strlen($CFG->dirroot) + 1,
|
|
|
145 |
);
|
|
|
146 |
},
|
|
|
147 |
);
|
|
|
148 |
|
|
|
149 |
$cache->set('shimmed_routes', $cachedata);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
return $cachedata;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Fetch all standard routes.
|
|
|
157 |
*
|
|
|
158 |
* Note: This method caches results in MUC.
|
|
|
159 |
*
|
|
|
160 |
* @return array[]
|
|
|
161 |
*/
|
|
|
162 |
protected function get_all_standard_routes(): array {
|
|
|
163 |
$cache = \cache::make('core', 'routes');
|
|
|
164 |
|
|
|
165 |
if (!($cachedata = $cache->get('standard_routes'))) {
|
|
|
166 |
$cachedata = $this->get_all_routes_in_namespace(
|
|
|
167 |
namespace: 'route\controller',
|
|
|
168 |
componentpathcallback: $this->normalise_component_path(...),
|
|
|
169 |
filtercallback: fn(string $classname) => !str_contains($classname, '\\shim\\'),
|
|
|
170 |
);
|
|
|
171 |
|
|
|
172 |
$cache->set('standard_routes', $cachedata);
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
return $cachedata;
|
|
|
176 |
}
|
|
|
177 |
}
|