-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathroute.php
91 lines (79 loc) · 1.84 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
<?php
/**
* @author Jesse Boyer <[email protected]>
* @copyright Copyright (C), 2011-12 Jesse Boyer
* @license GNU General Public License 3 (http://www.gnu.org/licenses/)
* Refer to the LICENSE file distributed within the package.
*
* @link http://jream.com
*
* @internal Inspired by Klein @ https://github.com/chriso/klein.php
*/
class Route
{
/**
* @var array $_listUri List of URI's to match against
*/
private $_listUri = array();
/**
* @var array $_listCall List of closures to call
*/
private $_listCall = array();
/**
* @var string $_trim Used class-wide items to clean strings
*/
private $_trim = '/\^$';
/**
* add - Adds a URI and Function to the two lists
*
* @param string $uri A path such as about/system
* @param object $function An anonymous function
*/
public function add($uri, $function)
{
$uri = trim($uri, $this->_trim);
$this->_listUri[] = $uri;
$this->_listCall[] = $function;
}
/**
* listen
* @desc Looks for a match for the URI and runs the related function
*/
public function listen()
{
$uri = isset($_REQUEST['uri']) ? $_REQUEST['uri'] : '/';
$uri = trim($uri, $this->_trim);
$replacementValues = array();
/**
* List through the stored URI's
*/
foreach ($this->_listUri as $listKey => $listUri)
{
/**
* See if there is a match
*/
if (preg_match("#^$listUri$#", $uri))
{
/**
* Replace the values
*/
$realUri = explode('/', $uri);
$fakeUri = explode('/', $listUri);
/**
* Gather the .+ values with the real values in the URI
*/
foreach ($fakeUri as $key => $value)
{
if ($value == '.+')
{
$replacementValues[] = $realUri[$key];
}
}
/**
* Pass an array for arguments
*/
call_user_func_array($this->_listCall[$listKey], $replacementValues);
}
} // End of Loop
} // end of Listen
}