MediaWiki
master
Toggle main menu visibility
MatcherBasedModule.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\Rest\Module;
4
5
use InvalidArgumentException;
6
use MediaWiki\Rest\PathTemplateMatcher\PathMatcher;
7
16
abstract
class
MatcherBasedModule
extends
Module
{
17
19
private
?array $matchers = [];
20
21
private
bool
$matchersInitialized =
false
;
22
23
public
function
getCacheData
(): array {
24
$cacheData = [];
25
26
foreach
( $this->
getMatchers
() as $method => $matcher ) {
27
$cacheData[$method] = $matcher->getCacheData();
28
}
29
30
$cacheData[self::CACHE_CONFIG_HASH_KEY] = $this->
getConfigHash
();
31
return
$cacheData;
32
}
33
34
public
function
initFromCacheData
( array $cacheData ): bool {
35
if
( $cacheData[self::
CACHE_CONFIG_HASH_KEY
] !== $this->
getConfigHash
() ) {
36
return
false
;
37
}
38
39
unset( $cacheData[self::CACHE_CONFIG_HASH_KEY] );
40
$this->matchers = [];
41
42
foreach
( $cacheData as $method => $data ) {
43
$this->matchers[$method] =
PathMatcher::newFromCache
( $data );
44
}
45
46
$this->matchersInitialized =
true
;
47
return
true
;
48
}
49
55
abstract
protected
function
getConfigHash
(): string;
56
62
protected
function
getMatchers
() {
63
if
( !$this->matchersInitialized ) {
64
$this->
initRoutes
();
65
$this->matchersInitialized =
true
;
66
}
67
68
return
$this->matchers;
69
}
70
74
abstract
protected
function
initRoutes
(): void;
75
83
protected
function
addRoute
( $method,
string
$path
, array $info ) {
84
$methods = (array)$method;
85
86
// Make sure the matched path is known.
87
if
( !isset( $info[
'spec'
] ) ) {
88
throw
new
InvalidArgumentException(
'Missing key in $info: "spec"'
);
89
}
90
91
$info[
'path'
] =
$path
;
92
93
foreach
( $methods as $method ) {
94
$method = strtoupper( $method );
95
$this->matchers[$method] ??=
new
PathMatcher
();
96
97
$info[
'config'
][
'method'
] = $method;
98
$this->matchers[$method]->add(
$path
, $info );
99
}
100
}
101
105
public
function
findHandlerMatch
(
106
string
$path
,
107
string
$requestMethod
108
): array {
109
$requestMethod = strtoupper( $requestMethod );
110
111
$matchers = $this->
getMatchers
();
112
$matcher = $matchers[$requestMethod] ??
null
;
113
$match = $matcher ? $matcher->match(
$path
) :
null
;
114
115
if
( !$match ) {
116
// Return allowed methods, to support CORS and 405 responses.
117
return
[
118
'found'
=>
false
,
119
'methods'
=> $this->
getAllowedMethods
(
$path
),
120
];
121
}
else
{
122
$info = $match[
'userData'
];
123
$info[
'found'
] =
true
;
124
$info[
'method'
] = $requestMethod;
125
$info[
'params'
] = $match[
'params'
] ?? [];
126
127
return
$info;
128
}
129
}
130
138
public
function
getAllowedMethods
(
string
$relPath ): array {
139
$allowed = [];
140
foreach
( $this->
getMatchers
() as $allowedMethod => $allowedMatcher ) {
141
if
( $allowedMatcher->match( $relPath ) ) {
142
$allowed[] = $allowedMethod;
143
}
144
}
145
146
return
array_unique(
147
in_array(
'GET'
, $allowed ) ? array_merge( [
'HEAD'
], $allowed ) : $allowed
148
);
149
}
150
151
}
$path
$path
Definition
NoLocalSettings.php:14
if
if(!defined('MW_SETUP_CALLBACK'))
Definition
WebStart.php:72
MediaWiki\Rest\Module\MatcherBasedModule
MatcherBasedModules respond to requests by matching the requested path against a list of known routes...
Definition
MatcherBasedModule.php:16
MediaWiki\Rest\Module\MatcherBasedModule\addRoute
addRoute( $method, string $path, array $info)
Definition
MatcherBasedModule.php:83
MediaWiki\Rest\Module\MatcherBasedModule\getConfigHash
getConfigHash()
Get a config version hash for cache invalidation.
MediaWiki\Rest\Module\MatcherBasedModule\getMatchers
getMatchers()
Get an array of PathMatcher objects indexed by HTTP method.
Definition
MatcherBasedModule.php:62
MediaWiki\Rest\Module\MatcherBasedModule\initFromCacheData
initFromCacheData(array $cacheData)
Initialize from the given cache data if possible.
Definition
MatcherBasedModule.php:34
MediaWiki\Rest\Module\MatcherBasedModule\findHandlerMatch
findHandlerMatch(string $path, string $requestMethod)
Determines which handler to use for the given path and returns an array describing the handler and in...
Definition
MatcherBasedModule.php:105
MediaWiki\Rest\Module\MatcherBasedModule\getAllowedMethods
getAllowedMethods(string $relPath)
Get the allowed methods for a path.
Definition
MatcherBasedModule.php:138
MediaWiki\Rest\Module\MatcherBasedModule\getCacheData
getCacheData()
Return data that can later be used to initialize a new instance of this module in a fast and efficien...
Definition
MatcherBasedModule.php:23
MediaWiki\Rest\Module\MatcherBasedModule\initRoutes
initRoutes()
Initialize matchers by calling addRoute() for each known route.
MediaWiki\Rest\Module\Module
A REST module represents a collection of endpoints.
Definition
Module.php:43
MediaWiki\Rest\Module\Module\CACHE_CONFIG_HASH_KEY
const CACHE_CONFIG_HASH_KEY
Definition
Module.php:49
MediaWiki\Rest\PathTemplateMatcher\PathMatcher
A tree-based path routing algorithm.
Definition
PathMatcher.php:16
MediaWiki\Rest\PathTemplateMatcher\PathMatcher\newFromCache
static newFromCache( $data)
Create a PathMatcher from cache data.
Definition
PathMatcher.php:42
includes
Rest
Module
MatcherBasedModule.php
Generated on
for MediaWiki by
1.18.0