-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.php
431 lines (358 loc) · 14.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
/**
* Route library.
*
* Provides enhanced Routing capabilities to CodeIgniter-based applications.
*/
class Route {
/**
* Our built routes.
* @var array
*/
protected static $routes = array();
protected static $prefix = NULL;
protected static $named_routes = array();
protected static $default_home = 'home';
protected static $nested_prefix = '';
protected static $nested_depth = 0;
//--------------------------------------------------------------------
/**
* Combines the routes that we've defined with the Route class with the
* routes passed in. This is intended to be used after all routes have been
* defined to merge CI's default $route array with our routes.
*
* Example:
* $route['default_controller'] = 'home';
* Route::resource('posts');
* $route = Route::map($route);
*
* @param array $route The array to merge
* @return array The merge route array.
*/
public static function map($routes=array())
{
$controller = isset($routes['default_controller']) ? $routes['default_controller'] : self::$default_home;
foreach (self::$routes as $from => $to)
{
$routes[$from] = str_replace('{default_controller}', $controller, $to);
}
return $routes;
}
//--------------------------------------------------------------------
/**
* A single point to the basic routing. Can be used in place of CI's $route
* array if desired. Used internally by many of the methods.
*
* @param string $from
* @param string $to
* @return void
*/
public static function any($from, $to, $options=array(), $nested=false)
{
return self::createRoute($from, $to, $options, $nested);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// HTTP Verb-based routing
//--------------------------------------------------------------------
// Verb-based Routing works by only creating routes if the
// $_SERVER['REQUEST_METHOD'] is the proper type.
//
public static function get($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET')
{
self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function post($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function put($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PUT')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function delete($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'DELETE')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function head($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'HEAD')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function patch($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'PATCH')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
public static function options($from, $to, $options=array(), $nested=false)
{
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS')
{
return self::createRoute($from, $to, $options, $nested);
}
}
//--------------------------------------------------------------------
/**
* Creates HTTP-verb based routing for a controller.
*
* Generates the following routes, assuming a controller named 'photos':
*
* Route::resources('photos');
*
* Verb Path Action used for
* ------------------------------------------------------------------
* GET /photos index displaying a list of photos
* GET /photos/new create_new return an HTML form for creating a photo
* POST /photos create create a new photo
* GET /photos/{id} show display a specific photo
* GET /photos/{id}/edit edit return the HTML form for editing a single photo
* PUT /photos/{id} update update a specific photo
* DELETE /photos/{id} delete delete a specific photo
*
* @param string $name The name of the controller to route to.
* @param array $options An list of possible ways to customize the routing.
*/
public static function resources($name, $options=array(), $nested=false)
{
if (empty($name))
{
return;
}
$nest_offset = '';
// In order to allow customization of the route the
// resources are sent to, we need to have a new name
// to store the values in.
$new_name = $name;
// If a new controller is specified, then we replace the
// $name value with the name of the new controller.
if (isset($options['controller']))
{
$new_name = $options['controller'];
}
// If a new module was specified, simply put that path
// in front of the controller.
if (isset($options['module']))
{
$new_name = $options['module'] .'/'. $new_name;
}
// In order to allow customization of allowed id values
// we need someplace to store them.
$id = '([a-zA-Z0-9\-_]+)';
if (isset($options['constraint']))
{
$id = $options['constraint'];
}
// If the 'offset' option is passed in, it means that all of our
// parameter placeholders in the $to ($1, $2, etc), need to be
// offset by that amount. This is useful when we're using an API
// with versioning in the URL.
$offset = isset($options['offset']) ? (int)$options['offset'] : 0;
if (self::$nested_depth)
{
$nest_offset = '/$1';
$offset++;
}
self::get($name, $new_name .'/index'. $nest_offset, null, $nested);
self::get($name .'/new', $new_name .'/create_new'. $nest_offset, null, $nested);
self::get($name .'/'. $id .'/edit', $new_name .'/edit'. $nest_offset .'/$'. (1 + $offset), null, $nested);
self::get($name .'/'. $id, $new_name .'/show'. $nest_offset .'/$'. (1 + $offset), null, $nested);
self::post($name, $new_name .'/create'. $nest_offset, null, $nested);
self::put($name .'/'. $id, $new_name .'/update'. $nest_offset .'/$'. (1 + $offset), null, $nested);
self::delete($name .'/'. $id, $new_name .'/delete'. $nest_offset .'/$'. (1 + $offset), null, $nested);
}
//--------------------------------------------------------------------
/**
* Add a prefix to the $from portion of the route. This is handy for
* grouping items under a similar URL, like:
*
* Route::prefix('admin', function()
* {
* Route::resources('users');
* });
*
* @param string $name The prefix to add to the routes.
* @param Closure $callback
*/
public static function prefix($name, Closure $callback)
{
self::$prefix = $name;
call_user_func($callback);
self::$prefix = null;
}
//--------------------------------------------------------------------
/**
* Returns the $from portion of the route if it has been saved with a name
* previously.
*
* Example:
*
* Route::get('posts', 'posts/show', array('as' => 'posts'));
* redirect( Route::named('posts') );
*
* @param [type] $name [description]
* @return [type] [description]
*/
public static function named($name)
{
if (isset(self::$named_routes[$name]))
{
return self::$named_routes[$name];
}
return NULL;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Contexts
//--------------------------------------------------------------------
/**
* Contexts provide a way for modules to assign controllers to an area of the
* site based on the name of the controller. This can be used for making a
* '/developer' area of the site that all modules can create functionality into.
*
* @param string $name The name of the URL segment
* @param string $controller The name of the controller
* @param array $options
*
* @return void
*/
public static function context($name, $controller=null, $options=array())
{
// If $controller is an array, then it's actually the options array,
// so we'll reorganize parameters.
if (is_array($controller))
{
$options = $controller;
$controller = null;
}
// If $controller is empty, then we need to rename it to match
// the $name value.
if (empty($controller))
{
$controller = $name;
}
$offset = isset($options['offset']) ? (int)$options['offset'] : 0;
// Some helping hands
$first = 1 + $offset;
$second = 2 + $offset;
$third = 3 + $offset;
$fourth = 4 + $offset;
$fifth = 5 + $offset;
$sixth = 6 + $offset;
self::any($name .'/(:any)/(:any)/(:any)/(:any)/(:any)/(:any)', "\${$first}/{$controller}/\${$second}/\${$third}/\${$fourth}/\${$fifth}/\${$sixth}");
self::any($name .'/(:any)/(:any)/(:any)/(:any)/(:any)', "\${$first}/{$controller}/\${$second}/\${$third}/\${$fourth}/\${$fifth}");
self::any($name .'/(:any)/(:any)/(:any)/(:any)', "\${$first}/{$controller}/\${$second}/\${$third}/\${$fourth}");
self::any($name .'/(:any)/(:any)/(:any)', "\${$first}/{$controller}/\${$second}/\${$third}");
self::any($name .'/(:any)/(:any)', "\${$first}/{$controller}/\${$second}");
self::any($name .'/(:any)', "\${$first}/{$controller}");
unset($first, $second, $third, $fourth, $fifth, $sixth);
// Are we creating a home controller?
if (isset($options['home']) && ! empty($options['home']))
{
self::any($name, "{$name}/{$options['home']}");
}
}
//--------------------------------------------------------------------
/**
* Allows you to easily block access to any number of routes by setting
* that route to an empty path ('').
*
* Example:
* Route::block('posts', 'photos/(:num)');
*
* // Same as...
* $route['posts'] = '';
* $route['photos/(:num)'] = '';
*/
public static function block()
{
$paths = func_get_args();
if ( ! is_array($paths))
{
return;
}
foreach ($paths as $path)
{
self::createRoute($path, '');
}
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Utility Methods
//--------------------------------------------------------------------
/**
* Resets the class to a first-load state. Mainly useful during testing.
*
* @return void
*/
public static function reset()
{
self::$routes = array();
self::$named_routes = array();
self::$nested_depth = 0;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Private Methods
//--------------------------------------------------------------------
/**
* Does the heavy lifting of creating an actual route. You must specify
* the request method(s) that this route will work for. They can be separated
* by a pipe character "|" if there is more than one.
*
* @param string $from
* @param array $to
*
* @return array The built route.
*/
private static function createRoute($from, $to, $options=array(), $nested=false)
{
$prefix = is_null(self::$prefix) ? '' : self::$prefix .'/';
$from = self::$nested_prefix . $prefix . $from;
// Are we saving the name for this one?
if (isset($options['as']) && ! empty($options['as']))
{
self::$named_routes[ $options['as'] ] = $from;
}
self::$routes[$from] = $to;
// Do we have a nested function?
if ($nested && is_callable($nested) && self::$nested_depth === 0)
{
self::$nested_prefix .= rtrim($from, '/') .'/';
self::$nested_depth += 1;
call_user_func($nested);
self::$nested_prefix = '';
}
// Reset our prefix.
if ($nested_depth)
{
self::$nested_prefix = '';
}
else
{
self::$nested_depth = self::$nested_depth === 0 ? self::$nested_depth : self::$nested_depth -1;
}
}
//--------------------------------------------------------------------
}