90 protected function doAdd(
$path, $params, $options, $key =
null ) {
92 if (
$path[0] !==
'/' ) {
96 if ( !isset( $options[
'strict'] ) || !$options[
'strict'] ) {
98 if ( strpos(
$path,
'$1' ) ===
false ) {
99 if ( substr(
$path, -1 ) !==
'/' ) {
108 if ( !isset( $params[
'title'] ) && strpos(
$path,
'$1' ) !==
false ) {
109 $params[
'title'] =
'$1';
112 if ( isset( $params[
'title'] ) && $params[
'title'] ===
false ) {
113 unset( $params[
'title'] );
118 foreach ( $params as $paramName => $paramData ) {
119 if ( is_string( $paramData ) ) {
120 if ( preg_match(
'/\$(\d+|key)/u', $paramData ) ) {
121 $paramArrKey =
'pattern';
125 $paramArrKey =
'value';
127 $params[$paramName] = [
128 $paramArrKey => $paramData
135 foreach ( $options as $optionName => $optionData ) {
136 if ( preg_match(
'/^\$\d+$/u', $optionName ) && !is_array( $optionData ) ) {
137 $options[$optionName] = [ $optionData ];
144 'options' => $options,
148 $this->patterns[] = $pattern;
158 public function add(
$path, $params = [], $options = [] ) {
159 if ( is_array(
$path ) ) {
160 foreach (
$path as $key => $onePath ) {
161 $this->
doAdd( $onePath, $params, $options, $key );
176 $options[
'strict'] =
true;
177 $this->
add(
$path, $params, $options );
186 foreach ( $this->patterns as $key => $pattern ) {
187 $weights[$key] = $pattern->weight;
189 array_multisort( $weights, SORT_DESC, SORT_NUMERIC, $this->patterns );
197 # Start with a weight of 0
201 $path = explode(
'/', $pattern->path );
203 # For each level of the path
204 foreach (
$path as $piece ) {
205 if ( preg_match(
'/^\$(\d+|key)$/u', $piece ) ) {
206 # For a piece that is only a $1 variable add 1 points of weight
208 } elseif ( preg_match(
'/\$(\d+|key)/u', $piece ) ) {
209 # For a piece that simply contains a $1 variable add 2 points of weight
212 # For a solid piece add a full 3 points of weight
217 foreach ( $pattern->options as $key => $option ) {
218 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
219 # Add 0.5 for restrictions to values
220 # This way given two separate "/$2/$1" patterns the
221 # one with a limited set of $2 values will dominate
222 # the one that'll match more loosely
265 foreach ( $this->patterns as $pattern ) {
281 $regexp = preg_quote( $pattern->path,
'#' );
283 $regexp = preg_replace(
'#\\\\\$1#u',
'(?P<par1>.*)', $regexp );
285 $regexp = preg_replace(
'#\\\\\$(\d+)#u',
'(?P<par$1>.+?)', $regexp );
286 $regexp =
"#^{$regexp}$#";
292 if ( preg_match( $regexp,
$path, $m ) ) {
295 foreach ( $pattern->options as $key => $option ) {
296 if ( preg_match(
'/^\$\d+$/u', $key ) ) {
297 $n = intval( substr( $key, 1 ) );
298 $value = rawurldecode( $m[
"par{$n}"] );
299 if ( !in_array( $value, $option ) ) {
308 foreach ( $m as $matchKey => $matchValue ) {
309 if ( preg_match(
'/^par\d+$/u', $matchKey ) ) {
310 $n = intval( substr( $matchKey, 3 ) );
311 $data[
'$' . $n] = rawurldecode( $matchValue );
315 if ( isset( $pattern->key ) ) {
316 $data[
'$key'] = $pattern->key;
320 foreach ( $pattern->params as $paramName => $paramData ) {
324 if ( preg_match(
'/^data:/u', $paramName ) ) {
326 $key = substr( $paramName, 5 );
332 if ( isset( $paramData[
'value'] ) ) {
334 $value = $paramData[
'value'];
335 } elseif ( isset( $paramData[
'pattern'] ) ) {
338 $paramData[
'pattern'] );
339 if ( $value ===
false ) {
347 $data[$key] = $value;
354 if ( isset( $pattern->options[
'callback'] ) ) {
355 call_user_func_array( $pattern->options[
'callback'], [ &
$matches, $data ] );
376 $replacer =
function ( $m ) use ( $pathMatches, $key, &$error ) {
377 if ( $m[1] ==
"key" ) {
378 if ( is_null( $key ) ) {
387 if ( !isset( $pathMatches[
"par$d"] ) ) {
393 return rawurldecode( $pathMatches[
"par$d"] );
397 $value = preg_replace_callback(
'/\$(\d+|key)/u', $replacer, $value );
412 if ( !$actionPaths ) {
417 if ( !isset( $actionPaths[
'view'] ) ) {
418 $actionPaths[
'view'] = $articlePath;