forked from avdg/ppi-framework-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
176 lines (151 loc) · 3.44 KB
/
Router.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
<?php
/**
* The default router class for the PPI Framework
*
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @package Core
* @link www.ppiframework.com
*
*/
class PPI_Router implements PPI_Router_Interface {
/**
* An array of all the routes to be used
*
* @var null|array
*/
protected $_allRoutes = null;
/**
* The matched route
*
* @var string
*/
protected $_matchedRoute = null;
/**
* The file to get the routes from
*
* @var null|string
*/
protected $_routingFile = null;
/**
* Check if we have ran the init() function before or not.
*
* @var bool
*/
protected $_ranInit = false;
/**
* Check if we have found a match or not from our custom routes.
*
* @var bool
*/
protected $_foundMatch = false;
/**
* The URI to be used for routing matches
*
* @var null|string
*/
protected $_uri = null;
/**
* The constructor
*/
public function __construct(array $options = array()) {
if (isset($options['routingFile'])) {
$this->_routingFile = $options['routingFile'];
}
}
public function match() {
if(!$this->_ranInit) {
$this->init();
}
return $this->_matchedRoute !== null;
}
/**
* Initialise the router and start grabbing routes
*
* @return void
*/
public function init() {
include $this->getRoutingFile();
// So all other functions have access to the routes.
$this->_allRoutes = $routes;
$uri = '/' . $this->_uri;
$route = $uri;
// Loop through the route array looking for wild-cards
foreach ($routes as $key => $val) {
// Convert wild-cards to RegEx
$key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
// Does the RegEx match?
if (preg_match('#^' . $key . '$#', $uri)) {
// Do we have a back-reference?
if (false !== strpos($val, '$') && false !== strpos($key, '(')) {
$val = preg_replace('#^' . $key . '$#', $val, $uri);
}
$this->_foundMatch = true;
$route = $val;
break;
}
}
// If we are on the homepage, we need to match the __default__ route.
if(!$this->_foundMatch && $this->_uri === '' && isset($routes['__default__'])) {
$this->_foundMatch = true;
$route = $routes['__default__'];
}
// We don't currently want to send query string data back up the chain, this can only effectively
// Be matched currently using a route and our routes have matched nothing. Remove query string values.
if( ($pos = strpos($route, '?')) !== false) {
$route = substr($route, 0, $pos);
}
$this->setMatchedRoute($route);
}
/**
* Get the route currently matched
*
* @return string
*/
public function getMatchedRoute() {
return $this->_matchedRoute;
}
/**
* Set the route currently matched
*
* @param string $route
* @return void
*/
public function setMatchedRoute($route) {
$this->_matchedRoute = $route;
}
/**
* Get the routing file
*
* @return string
*/
public function getRoutingFile() {
if ($this->_routingFile === null) {
$this->_routingFile = APPFOLDER . 'Config/routes.php';
}
return $this->_routingFile;
}
/**
* Set the uri
*
* @param $uri
* @return void
*/
public function setUri($uri) {
$this->_uri = $uri;
}
/**
* Get the uri
*
* @return
*/
public function getUri() {
return $this->_uri;
}
public function getDefaultRoute() {
return $this->_allRoutes['__default__'];
}
public function get404Route() {
return $this->_allRoutes['__404__'];
}
}