-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathroute.php
80 lines (69 loc) · 1.96 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
<?php
/**
* @author Jesse Boyer <contact@jream.com>
* @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 Class-wide items to clean
*/
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;
}
/**
* submit - Looks for a match for the URI and runs the related function
*/
public function submit()
//new version
{ $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
//$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 - if (preg_match("#^$listUri$#", $uri))
} # end - foreach ($this->_listUri as $listKey => $listUri)
} # end - public function submit()
}