13use Wikimedia\ObjectFactory;
104 $cacheData = $this->cacheBag->get( $this->
getCacheKey() );
105 if ( $cacheData && $cacheData[
'CONFIG-HASH'] === $this->
getConfigHash() ) {
106 unset( $cacheData[
'CONFIG-HASH'] );
117 return $this->cacheBag->makeKey( __CLASS__,
'1' );
126 if ( $this->configHash ===
null ) {
127 $this->configHash = md5( json_encode( [
141 if ( $this->routesFromFiles ===
null ) {
142 $this->routeFileTimestamps = [];
143 foreach ( $this->routeFiles as $fileName ) {
144 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
145 $routes = json_decode( file_get_contents( $fileName ),
true );
146 if ( $this->routesFromFiles ===
null ) {
147 $this->routesFromFiles = $routes;
149 $this->routesFromFiles = array_merge( $this->routesFromFiles, $routes );
162 if ( $this->routeFileTimestamps ===
null ) {
163 $this->routeFileTimestamps = [];
164 foreach ( $this->routeFiles as $fileName ) {
165 $this->routeFileTimestamps[$fileName] = filemtime( $fileName );
178 $iterator =
new AppendIterator;
180 $iterator->append(
new \ArrayIterator( $this->extraRoutes ) );
190 if ( $this->matchers ===
null ) {
194 foreach ( $cacheData as $method => $data ) {
199 $methods = $spec[
'method'] ?? [
'GET' ];
200 if ( !is_array( $methods ) ) {
201 $methods = [ $methods ];
203 foreach ( $methods as $method ) {
212 foreach (
$matchers as $method => $matcher ) {
213 $cacheData[$method] = $matcher->getCacheData();
215 $this->cacheBag->set( $this->
getCacheKey(), $cacheData );
230 if ( strlen( $this->rootPath ) > strlen(
$path ) ||
231 substr_compare(
$path, $this->rootPath, 0, strlen( $this->rootPath ) ) !== 0
235 return substr(
$path, strlen( $this->rootPath ) );
248 public function getRouteUrl( $route, $pathParams = [], $queryParams = [] ) {
249 foreach ( $pathParams as $param => $value ) {
252 $route = str_replace(
'{' . $param .
'}', rawurlencode( $value ), $route );
255 $url = $this->baseUrl . $this->rootPath . $route;
268 if ( $relPath ===
false ) {
269 return $this->responseFactory->createLocalizedHttpError( 404,
271 ->plaintextParams(
$path, $this->rootPath )
277 $matcher =
$matchers[$requestMethod] ??
null;
278 $match = $matcher ? $matcher->
match( $relPath ) :
null;
282 if ( !$match && $requestMethod ===
'HEAD' && isset(
$matchers[
'GET'] ) ) {
289 foreach (
$matchers as $allowedMethod => $allowedMatcher ) {
290 if ( $allowedMethod === $requestMethod ) {
293 if ( $allowedMatcher->match( $relPath ) ) {
294 $allowed[] = $allowedMethod;
298 $response = $this->responseFactory->createLocalizedHttpError( 405,
300 ->textParams( $requestMethod )
301 ->commaListParams( $allowed )
302 ->numParams( count( $allowed ) )
304 $response->setHeader(
'Allow', $allowed );
308 return $this->responseFactory->createLocalizedHttpError( 404,
310 ->plaintextParams( $relPath )
316 $request->
setPathParams( array_map(
'rawurldecode', $match[
'params'] ) );
317 $handler = $this->
createHandler( $request, $match[
'userData'] );
322 return $this->responseFactory->createFromException( $e );
333 $objectFactorySpec = array_intersect_key( $spec,
334 [
'factory' =>
true,
'class' =>
true,
'args' =>
true,
'services' =>
true ] );
336 $handler = $this->objectFactory->createObject( $objectFactorySpec );
337 $handler->
init( $this, $request, $spec, $this->responseFactory, $this->hookContainer );
350 $authResult = $this->basicAuth->authorize( $handler->getRequest(), $handler );
352 return $this->responseFactory->createHttpError( 403, [
'error' => $authResult ] );
356 $handler->validate( $this->restValidator );
359 $earlyResponse = $handler->checkPreconditions();
360 if ( $earlyResponse ) {
361 return $earlyResponse;
365 $response = $handler->execute();
366 if ( !( $response instanceof ResponseInterface ) ) {
367 $response = $this->responseFactory->createFromReturnValue( $response );
371 $handler->applyConditionalResponseHeaders( $response );
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Class representing a cache/ephemeral data store.