-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteParser.php
More file actions
189 lines (165 loc) · 4.72 KB
/
RouteParser.php
File metadata and controls
189 lines (165 loc) · 4.72 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Routes;
use Exception;
/**
* Parses route strings:
*
* "/user/{name}[/{id:[0-9]+}]"
*/
class RouteParser
{
const VARIABLE_REGEX = <<<'REGEX'
\{
\s* ([a-zA-Z_][a-zA-Z0-9_-]*) \s*
(?:
: \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*)
)?
\}
REGEX;
const DEFAULT_DISPATCH_REGEX = '[^/]+';
/**
* Parse route pattern
*
* @param string $routePattern
* @return array
*/
public static function parse($routePattern)
{
$routeWithoutClosingOptionals = \rtrim($routePattern, ']');
$numOptionals = \strlen($routePattern) - \strlen($routeWithoutClosingOptionals);
$segments = \preg_split('~' . Self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $routeWithoutClosingOptionals);
if ($numOptionals !== count($segments) - 1) {
if (\preg_match('~' . Self::VARIABLE_REGEX . '(*SKIP)(*F) | \]~x', $routeWithoutClosingOptionals)) {
throw new Exception('Optional segments can only occur at the end of a route');
}
throw new Exception("Number of opening '[' and closing ']' does not match");
}
$currentRoute = '';
$data = [];
foreach ($segments as $n => $segment) {
if ($segment === '' && $n !== 0) {
throw new Exception('Empty optional part');
}
$currentRoute .= $segment;
$data[] = Self::parsePlaceholders($currentRoute);
}
return $data;
}
/**
* Parses a route string that does not contain optional segments.
*
* @param string
* @return array
*/
public static function parsePlaceholders($route)
{
if (!preg_match_all(
'~' . self::VARIABLE_REGEX . '~x', $route, $matches,
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
)) {
return [$route];
}
$offset = 0;
$routeData = [];
foreach ($matches as $set) {
if ($set[0][1] > $offset) {
$routeData[] = \substr($route, $offset, $set[0][1] - $offset);
}
$routeData[] = [
$set[1][0],
isset($set[2]) ? \trim($set[2][0]) : Self::DEFAULT_DISPATCH_REGEX
];
$offset = $set[0][1] + \strlen($set[0][0]);
}
if ($offset !== \strlen($route)) {
$routeData[] = \substr($route, $offset);
}
return $routeData;
}
/**
* Create route regex
*
* @param array|string $routeData
* @return string
*/
public static function createRegex($routeData)
{
$routeData = (\is_string($routeData) == true) ? Self::parse($routeData) : $routeData;
if (Self::isStaticRoute($routeData) == true) {
return $routeData[0];
}
$regex = Self::buildRegexForRoute($routeData);
return ($regex !== false) ? $regex[0] : false;
}
/**
* Create route regex
*
* @param array
* @return array
*/
public static function buildRegexForRoute(array $routeData)
{
$regex = '';
$variables = [];
foreach ($routeData as $part) {
if (\is_string($part) == true) {
$regex .= \preg_quote($part,'~');
continue;
}
[$varName,$regexPart] = $part;
if (isset($variables[$varName]) == true) {
return false;
}
if (Self::regexHasCapturingGroups($regexPart) == true) {
return false;
}
$variables[$varName] = $varName;
$regex .= '(' . $regexPart . ')';
}
return [$regex,$variables];
}
/**
* Test for capturing groups
* @param string
* @return bool
*/
public static function regexHasCapturingGroups($regex)
{
if (false === \strpos($regex,'(')) {
return false;
}
return (bool)preg_match(
'~
(?:
\(\?\(
| \[ [^\]\\\\]* (?: \\\\ . [^\]\\\\]* )* \]
| \\\\ .
) (*SKIP)(*FAIL) |
\(
(?!
\? (?! <(?![!=]) | P< | \' )
| \*
)
~x',
$regex
);
}
/**
*
* Return true if route is static
* @param array
* @return bool
*/
public static function isStaticRoute($routeData)
{
return (count($routeData) === 1 && \is_string($routeData[0]));
}
}