67 private array $routeFiles;
72 private array $extraRoutes;
80 private ?array $routesFromFiles =
null;
83 private ?array $routeFileTimestamps =
null;
85 private ?
string $configHash =
null;
101 ObjectFactory $objectFactory,
116 $this->routeFiles = $routeFiles;
117 $this->extraRoutes = $extraRoutes;
125 if ( $this->configHash === null ) {
126 $this->configHash = md5( json_encode( [
127 'class' => __CLASS__,
129 'extraRoutes' => $this->extraRoutes,
130 'fileTimestamps' => $this->getRouteFileTimestamps()
133 return $this->configHash;
143 private function getRoutesFromFiles(): array {
144 if ( $this->routesFromFiles !== null ) {
145 return $this->routesFromFiles;
148 $this->routesFromFiles = [];
149 $this->routeFileTimestamps = [];
150 foreach ( $this->routeFiles as $fileName ) {
151 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
153 $routes = $this->loadJsonFile( $fileName );
155 $this->routesFromFiles = array_merge( $this->routesFromFiles, $routes );
158 return $this->routesFromFiles;
166 private function getRouteFileTimestamps(): array {
167 if ( $this->routeFileTimestamps === null ) {
168 $this->routeFileTimestamps = [];
169 foreach ( $this->routeFiles as $fileName ) {
170 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
173 return $this->routeFileTimestamps;
181 foreach ( $this->getAllRoutes() as $spec ) {
182 $key = $spec[
'path'];
184 $methods = isset( $spec[
'method'] ) ? (array)$spec[
'method'] : [
'GET' ];
186 $paths[$key] = array_merge( $paths[$key] ?? [], $methods );
195 private function getAllRoutes() {
196 $iterator =
new AppendIterator;
197 $iterator->append(
new ArrayIterator( $this->getRoutesFromFiles() ) );
198 $iterator->append(
new ArrayIterator( $this->extraRoutes ) );
203 $routeDefs = $this->getAllRoutes();
205 foreach ( $routeDefs as $route ) {
206 if ( !isset( $route[
'path'] ) ) {
210 $path = $route[
'path'];
211 $method = $route[
'method'] ??
'GET';
212 $info = $this->makeRouteInfo( $route );
214 $this->addRoute( $method,
$path, $info );
223 private function makeRouteInfo( array $route ): array {
224 static $objectSpecKeys = [
232 if ( isset( $route[
'redirect'] ) ) {
235 'spec' => [
'class' => RedirectHandler::class ],
241 'spec' => array_intersect_key( $route, array_flip( $objectSpecKeys ) ),
242 'config' => array_diff_key( $route, array_flip( $objectSpecKeys ) ),
245 if ( isset( $route[
'openApiSpec'] ) ) {
246 $info[
'openApiSpec'] = $this->jsonLocalizer->localizeJson( $route[
'openApiSpec'] );
249 $info[
'path'] = $route[
'path'];
257 'title' => $this->jsonLocalizer->getFormattedMessage(
'rest-module-extra-routes-title' ),
258 'description' => $this->jsonLocalizer->getFormattedMessage(
'rest-module-extra-routes-desc' ),
259 'version' =>
'undefined',