/
Route.php
243 lines (211 loc) · 5.73 KB
/
Route.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php namespace Sugi;
/**
* Route
*
* @package Sugi
* @version 12.12.13
*/
/**
* \Sugi\Route
*/
class Route
{
// <segment> RegEx
const REGEX_KEY = '<([a-zA-Z0-9_]++)>';
// What <segment> accepts by default
const REGEX_SEGMENT = '[^/.,;?<>]++';
// Those should be escaped
const REGEX_ESCAPE = '[.\\+?[^\\]${}=!]';
protected static $routes = array();
public static $uri = false;
/**
* Creating Rule for routing
* Executing callback function if current URI matches given pattern
*
* <code>
* Route::add('(<lang>/)(<controller>(/<method>(/<param>)))', function ($segments) {
* {
* define('LANG', ($lang = Filter::key('lang', $segments, 'en')) ? $lang : 'en');
* $controller = Filter::key('controller', $segments, 'home');
* $method = Filter::key('method', $segments, 'index');
* $arguments = Filter::key('arguments', $segments, array());
*
* App::execute("Controller_$controller", "action_$method", $arguments) and exit;
* })->name('mvc')->segment('lang', 'en|bg')->segment('param', '.*');
* </code>
*
* @param string $pattern - simplified pattern
* @param function $callback
* @return \Sugi\Route\Router
*/
public static function add($pattern, $callback)
{
// echo '<pre>' . htmlspecialchars($pattern) . '</pre>';
$route = new Route($pattern, $callback);
static::$routes[] = $route;
return $route;
}
/**
* Processing Request
*
* @todo: add this to App::run();
* @return boolean
*/
public static function process_request()
{
$uri = (static::$uri !== false) ? static::$uri : URI::current();
$match = false;
foreach (static::$routes as $route) {
if ($route->match($uri)) $match = true;
}
return $match;
}
/**
* Finds a named route
*
* @param string $name
* @return \Sugi\Route
*/
public static function find($name)
{
foreach (static::$routes as $route) {
if ($route->name == $name) return $route;
}
return false;
}
/**
* Makes a URL based on the pattern provided by the named route
*
* @param string $name
* @param array $segments
* @return string
*/
public static function make($name, $segments = array())
{
if (!$route = static::find($name)) return '/';
$uri = static::replace_segments($route->pattern, $segments);
// add leading '/'
$uri = '/' . ltrim($uri, '/');
return $uri;
}
/**
* Replaces segments in the route pattern with the given values
*
* @param string $pattern
* @param array $segments
* @return string
*/
protected static function replace_segments($pattern, $segments)
{
// replacing NON mandatory segments/params
if (preg_match('#\(([^()]+)\)#uD', $pattern, $match)) {
//echo "<pre>" . htmlspecialchars(var_export($match, true)) . "</pre>";
$found = false;
$replace = $match[1];
if (preg_match_all('#'.static::REGEX_KEY.'#uD', $replace, $segmatch)) {
//echo "<pre>" . htmlspecialchars(var_export($segmatch, true)) . "</pre>";
foreach ($segmatch[1] as $key => $seg) {
if ($value = Filter::key($seg, $segments)) {
$replace = str_replace("<$seg>", $value, $replace);
$found = true;
}
else {
$replace = str_replace("<$seg>", $seg, $replace);
}
}
}
if (!$found) $replace = '';
$pattern = str_replace($match[0], $replace, $pattern);
return static::replace_segments($pattern, $segments);
}
// replacing mandatory segments/params
if (preg_match_all('#'.static::REGEX_KEY.'#uD', $pattern, $segmatch)) {
foreach ($segmatch[1] as $key => $seg) {
if ($value = Filter::key($seg, $segments)) {
$pattern = str_replace("<$seg>", $value, $pattern);
$found = true;
}
else {
$pattern = str_replace("<$seg>", $seg, $pattern);
}
}
}
return $pattern;
}
/****************
* Route *
****************/
protected $pattern;
protected $callback;
protected $regex;
protected $name;
public $segments;
/**
* Sets custom pattern for the segment
*
* @param string $segment
* @param string $pattern
*/
public function segment($segment, $pattern)
{
$this->segments[$segment] = $pattern;
return $this;
}
public function name($value)
{
$this->name = $value;
return $this;
}
/**
* Constructor which is invoked by \Sugi\Route class
*
* @param string $pattern
* @param Closure $callback
*/
protected function __construct($pattern, $callback)
{
$this->pattern = $pattern;
$this->callback = $callback;
}
/**
* Check the given URI matches the router's pattern and execute callback function.
*
* @param string $uri
* @return boolean
*/
protected function match($uri)
{
$this->compile();
if (preg_match($this->regex, $uri, $matches)) {
$segments = array();
foreach ($matches as $key => $value) {
// We need only named params
if (!is_int($key)) {
$segments[$key] = $value;
}
}
call_user_func_array($this->callback, array($segments));
return true;
}
return false;
}
/**
* Compile regular expression for the router
*/
protected function compile()
{
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for: ( ) < >
$regex = preg_replace('#'.static::REGEX_ESCAPE.'#', '\\\\$0', $this->pattern);
// Make optional parts of the URI non-capturing and optional
$regex = str_replace(array('(', ')'), array('(?:', ')?'), $regex);
// Transform segments and segment patterns
// $this is not available in Closures before PHP 5.4, so we'll use function instead of Closure
$regex = preg_replace_callback('#'.static::REGEX_KEY.'#uD', array($this, 'regex_key_prc'), $regex);
$this->regex = '#^'.$regex.'$#siuD';
}
protected function regex_key_prc($match)
{
return "(?P<{$match[1]}>" . Filter::key($match[1], $this->segments, static::REGEX_SEGMENT) . ')';
}
}