|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Klein (klein.php) - A fast & flexible router for PHP |
|
4
|
|
|
* |
|
5
|
|
|
* @author Chris O'Hara <[email protected]> |
|
6
|
|
|
* @author Trevor Suarez (Rican7) (contributor and v2 refactorer) |
|
7
|
|
|
* @copyright (c) Chris O'Hara |
|
8
|
|
|
* @link https://github.com/chriso/klein.php |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Klein; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* RouteFactory |
|
16
|
|
|
* |
|
17
|
|
|
* The default implementation of the AbstractRouteFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
class RouteFactory extends AbstractRouteFactory |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constants |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The value given to path's when they are entered as null values |
|
28
|
|
|
* |
|
29
|
|
|
* @type string |
|
30
|
|
|
*/ |
|
31
|
|
|
const NULL_PATH_VALUE = '*'; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Methods |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Check if the path is null or equal to our match-all, null-like value |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $path |
|
42
|
|
|
* @return boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function pathIsNull($path) |
|
45
|
|
|
{ |
|
46
|
|
|
return (static::NULL_PATH_VALUE === $path || null === $path); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Quick check to see whether or not to count the route |
|
51
|
|
|
* as a match when counting total matches |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $path |
|
54
|
|
|
* @return boolean |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function shouldPathStringCauseRouteMatch($path) |
|
57
|
|
|
{ |
|
58
|
|
|
// Only consider a request to be matched when not using 'matchall' |
|
59
|
|
|
return !$this->pathIsNull($path); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Pre-process a path string |
|
64
|
|
|
* |
|
65
|
|
|
* This method wraps the path string in a regular expression syntax baesd |
|
66
|
|
|
* on whether the string is a catch-all or custom regular expression. |
|
67
|
|
|
* It also adds the namespace in a specific part, based on the style of expression |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $path |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function preprocessPathString($path) |
|
73
|
|
|
{ |
|
74
|
|
|
// If the path is null, make sure to give it our match-all value |
|
75
|
|
|
$path = (null === $path) ? static::NULL_PATH_VALUE : (string) $path; |
|
76
|
|
|
|
|
77
|
|
|
// If a custom regular expression (or negated custom regex) |
|
78
|
|
|
if ($this->namespace && |
|
79
|
|
|
(isset($path[0]) && $path[0] === '@') || |
|
80
|
|
|
(isset($path[0]) && $path[0] === '!' && isset($path[1]) && $path[1] === '@') |
|
81
|
|
|
) { |
|
82
|
|
|
// Is it negated? |
|
83
|
|
|
if ($path[0] === '!') { |
|
84
|
|
|
$negate = true; |
|
85
|
|
|
$path = substr($path, 2); |
|
86
|
|
|
} else { |
|
87
|
|
|
$negate = false; |
|
88
|
|
|
$path = substr($path, 1); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Regex anchored to front of string |
|
92
|
|
|
if ($path[0] === '^') { |
|
93
|
|
|
$path = substr($path, 1); |
|
94
|
|
|
} else { |
|
95
|
|
|
$path = '.*' . $path; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($negate) { |
|
99
|
|
|
$path = '@^' . $this->namespace . '(?!' . $path . ')'; |
|
100
|
|
|
} else { |
|
101
|
|
|
$path = '@^' . $this->namespace . $path; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} elseif ($this->namespace && $this->pathIsNull($path)) { |
|
105
|
|
|
// Empty route with namespace is a match-all |
|
106
|
|
|
$path = '@^' . $this->namespace . '(/|$)'; |
|
107
|
|
|
} else { |
|
108
|
|
|
// Just prepend our namespace |
|
109
|
|
|
$path = $this->namespace . $path; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $path; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Build a Route instance |
|
117
|
|
|
* |
|
118
|
|
|
* @param callable $callback Callable callback method to execute on route match |
|
119
|
|
|
* @param string $path Route URI path to match |
|
120
|
|
|
* @param string|array $method HTTP Method to match |
|
121
|
|
|
* @param boolean $count_match Whether or not to count the route as a match when counting total matches |
|
122
|
|
|
* @param string $name The name of the route |
|
123
|
|
|
* @return Route |
|
124
|
|
|
*/ |
|
125
|
|
|
public function build($callback, $path = null, $method = null, $count_match = true, $name = null) |
|
126
|
|
|
{ |
|
127
|
|
|
return new Route( |
|
128
|
|
|
$callback, |
|
129
|
|
|
$this->preprocessPathString($path), |
|
130
|
|
|
$method, |
|
131
|
|
|
$this->shouldPathStringCauseRouteMatch($path) // Ignore the $count_match boolean that they passed |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|