67 private array $routeFiles;
72 private array $extraRoutes;
78 private ?array $routesFromFiles =
null;
81 private ?array $routeFileTimestamps =
null;
83 private ?
string $configHash =
null;
99 ObjectFactory $objectFactory,
114 $this->routeFiles = $routeFiles;
115 $this->extraRoutes = $extraRoutes;
122 if ( $this->configHash === null ) {
123 $this->configHash = md5( json_encode( [
124 'class' => __CLASS__,
126 'extraRoutes' => $this->extraRoutes,
127 'fileTimestamps' => $this->getRouteFileTimestamps()
130 return $this->configHash;
140 private function getRoutesFromFiles(): array {
141 if ( $this->routesFromFiles !== null ) {
142 return $this->routesFromFiles;
145 $this->routesFromFiles = [];
146 $this->routeFileTimestamps = [];
147 foreach ( $this->routeFiles as $fileName ) {
148 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
150 $routes = $this->loadJsonFile( $fileName );
152 $this->routesFromFiles = array_merge( $this->routesFromFiles, $routes );
155 return $this->routesFromFiles;
163 private function getRouteFileTimestamps(): array {
164 if ( $this->routeFileTimestamps === null ) {
165 $this->routeFileTimestamps = [];
166 foreach ( $this->routeFiles as $fileName ) {
167 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
170 return $this->routeFileTimestamps;
178 foreach ( $this->getAllRoutes() as $spec ) {
179 $key = $spec[
'path'];
181 $methods = isset( $spec[
'method'] ) ? (array)$spec[
'method'] : [
'GET' ];
183 $paths[$key] = array_merge( $paths[$key] ?? [], $methods );
192 private function getAllRoutes() {
193 $iterator =
new AppendIterator;
194 $iterator->append(
new ArrayIterator( $this->getRoutesFromFiles() ) );
195 $iterator->append(
new ArrayIterator( $this->extraRoutes ) );
200 $routeDefs = $this->getAllRoutes();
202 foreach ( $routeDefs as $route ) {
203 if ( !isset( $route[
'path'] ) ) {
207 $path = $route[
'path'];
208 $method = $route[
'method'] ??
'GET';
209 $info = $this->makeRouteInfo( $route );
211 $this->addRoute( $method,
$path, $info );
220 private function makeRouteInfo( array $route ): array {
221 static $objectSpecKeys = [
229 if ( isset( $route[
'redirect'] ) ) {
232 'spec' => [
'class' => RedirectHandler::class ],
238 'spec' => array_intersect_key( $route, array_flip( $objectSpecKeys ) ),
239 'config' => array_diff_key( $route, array_flip( $objectSpecKeys ) ),
243 $info[
'path'] = $route[
'path'];
252 'title' => $localizer->getFormattedMessage(
'rest-extra-routes-module-title' ),
253 'description' => $localizer->getFormattedMessage(
'rest-extra-routes-module-desc' ),
254 'version' =>
'undefined',