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