MediaWiki REL1_39
MediaWiki\Rest\PathTemplateMatcher\PathMatcher Class Reference

A tree-based path routing algorithm. More...

Public Member Functions

 add ( $template, $userData)
 Add a template to the matcher.
 
 getCacheData ()
 Get a data array for later use by newFromCache().
 
 match ( $path)
 Match a path against the current match trees.
 

Static Public Member Functions

static newFromCache ( $data)
 Create a PathMatcher from cache data.
 

Detailed Description

A tree-based path routing algorithm.

This container builds defined routing templates into a tree, allowing paths to be efficiently matched against all templates. The match time is independent of the number of registered path templates.

Efficient matching comes at the cost of a potentially significant setup time. We measured ~10ms for 1000 templates. Using getCacheData() and newFromCache(), this setup time may be amortized over multiple requests.

Definition at line 16 of file PathMatcher.php.

Member Function Documentation

◆ add()

MediaWiki\Rest\PathTemplateMatcher\PathMatcher::add ( $template,
$userData )

Add a template to the matcher.

The path template consists of components separated by "/". Each component may be either a parameter of the form {paramName}, or a literal string. A parameter matches any input path component, whereas a literal string matches itself.

Path templates must not conflict with each other, that is, any input path must match at most one path template. If a path template conflicts with another already registered, this function throws a PathConflict exception.

Parameters
string$templateThe path template
mixed$userDataUser data used to identify the matched route to the caller of match()
Exceptions
PathConflict

Definition at line 149 of file PathMatcher.php.

◆ getCacheData()

MediaWiki\Rest\PathTemplateMatcher\PathMatcher::getCacheData ( )

Get a data array for later use by newFromCache().

The internal format is private to PathMatcher, but note that it includes any data passed as $userData to add(). The array returned will be serializable as long as all $userData values are serializable.

Returns
array

Definition at line 57 of file PathMatcher.php.

◆ match()

MediaWiki\Rest\PathTemplateMatcher\PathMatcher::match ( $path)

Match a path against the current match trees.

If the path matches a previously added path template, an array will be returned with the following keys:

  • params: An array mapping parameter names to their detected values
  • userData: The user data passed to add(), which identifies the route

If the path does not match any template, false is returned.

Parameters
string$path
Returns
array|false

Definition at line 196 of file PathMatcher.php.

References $path.

◆ newFromCache()

static MediaWiki\Rest\PathTemplateMatcher\PathMatcher::newFromCache ( $data)
static

Create a PathMatcher from cache data.

Parameters
array$dataThe data array previously returned by getCacheData()
Returns
PathMatcher

Definition at line 42 of file PathMatcher.php.


The documentation for this class was generated from the following file: