MediaWiki master
ExtraRoutesModule.php
Go to the documentation of this file.
1<?php
2
4
5use AppendIterator;
6use ArrayIterator;
7use Iterator;
16use Wikimedia\ObjectFactory\ObjectFactory;
17
63
65 private array $routeFiles;
66
70 private array $extraRoutes;
71
76 private ?array $routesFromFiles = null;
77
79 private ?array $routeFileTimestamps = null;
80
82 private ?string $configHash = null;
83
92 public function __construct(
93 array $routeFiles,
94 array $extraRoutes,
95 Router $router,
97 BasicAuthorizerInterface $basicAuth,
98 ObjectFactory $objectFactory,
99 Validator $restValidator,
100 ErrorReporter $errorReporter
101 ) {
102 parent::__construct(
103 $router,
104 '',
106 $basicAuth,
107 $objectFactory,
108 $restValidator,
109 $errorReporter
110 );
111 $this->routeFiles = $routeFiles;
112 $this->extraRoutes = $extraRoutes;
113 }
114
120 protected function getConfigHash(): string {
121 if ( $this->configHash === null ) {
122 $this->configHash = md5( json_encode( [
123 'class' => __CLASS__,
124 'version' => 1,
125 'extraRoutes' => $this->extraRoutes,
126 'fileTimestamps' => $this->getRouteFileTimestamps()
127 ] ) );
128 }
129 return $this->configHash;
130 }
131
139 private function getRoutesFromFiles(): array {
140 if ( $this->routesFromFiles !== null ) {
141 return $this->routesFromFiles;
142 }
143
144 $this->routesFromFiles = [];
145 $this->routeFileTimestamps = [];
146 foreach ( $this->routeFiles as $fileName ) {
147 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
148
149 $routes = $this->loadJsonFile( $fileName );
150
151 $this->routesFromFiles = array_merge( $this->routesFromFiles, $routes );
152 }
153
154 return $this->routesFromFiles;
155 }
156
162 private function getRouteFileTimestamps(): array {
163 if ( $this->routeFileTimestamps === null ) {
164 $this->routeFileTimestamps = [];
165 foreach ( $this->routeFiles as $fileName ) {
166 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
167 }
168 }
169 return $this->routeFileTimestamps;
170 }
171
175 public function getDefinedPaths(): array {
176 $paths = [];
177 foreach ( $this->getAllRoutes() as $spec ) {
178 $key = $spec['path'];
179
180 $methods = isset( $spec['method'] ) ? (array)$spec['method'] : [ 'GET' ];
181
182 $paths[$key] = array_merge( $paths[$key] ?? [], $methods );
183 }
184
185 return $paths;
186 }
187
191 private function getAllRoutes() {
192 $iterator = new AppendIterator;
193 $iterator->append( new ArrayIterator( $this->getRoutesFromFiles() ) );
194 $iterator->append( new ArrayIterator( $this->extraRoutes ) );
195 return $iterator;
196 }
197
198 protected function initRoutes(): void {
199 $routeDefs = $this->getAllRoutes();
200
201 foreach ( $routeDefs as $route ) {
202 if ( !isset( $route['path'] ) ) {
203 throw new RouteDefinitionException( 'Missing path' );
204 }
205
206 $path = $route['path'];
207 $method = $route['method'] ?? 'GET';
208 $info = $this->makeRouteInfo( $route );
209
210 $this->addRoute( $method, $path, $info );
211 }
212 }
213
219 private function makeRouteInfo( array $route ): array {
220 static $objectSpecKeys = [
221 'class',
222 'factory',
223 'services',
224 'optional_services',
225 'args',
226 ];
227
228 if ( isset( $route['redirect'] ) ) {
229 // Redirect shorthand
230 $info = [
231 'spec' => [ 'class' => RedirectHandler::class ],
232 'config' => $route,
233 ];
234 } else {
235 // Object spec at the top level
236 $info = [
237 'spec' => array_intersect_key( $route, array_flip( $objectSpecKeys ) ),
238 'config' => array_diff_key( $route, array_flip( $objectSpecKeys ) ),
239 ];
240 }
241
242 $info['path'] = $route['path'];
243 return $info;
244 }
245
246}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
A Module that is based on flat route definitions in the form originally introduced in MW 1....
__construct(array $routeFiles, array $extraRoutes, Router $router, ResponseFactory $responseFactory, BasicAuthorizerInterface $basicAuth, ObjectFactory $objectFactory, Validator $restValidator, ErrorReporter $errorReporter)
initRoutes()
Initialize matchers by calling addRoute() for each known route.
getConfigHash()
Get a config version hash for cache invalidation.
MatcherBasedModules respond to requests by matching the requested path against a list of known routes...
ResponseFactory $responseFactory
Definition Module.php:41
Exception indicating incorrect REST module configuration.
Generates standardized response objects.
The REST router is responsible for gathering module configuration, matching an input path against the...
Definition Router.php:29
Wrapper for ParamValidator.
Definition Validator.php:37
An interface used by Router to ensure that the client has "basic" access, i.e.
An ErrorReporter internally reports an error that happened during the handling of a request.