-
Notifications
You must be signed in to change notification settings - Fork 8
/
Router.php
101 lines (90 loc) · 2.21 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
<?php
/**
*
* @version 1.0
* @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 {
/**
* The matched route
*
* @var string
*/
protected $_matchedRoute = null;
/**
* The file to get the routes from
*
* @var string $_routingFile
*/
protected $_routingFile = null;
/**
* The constructor
*/
public function __construct(array $options = array()) {
if (isset($options['routingFile'])) {
$this->_routingFile = $options['routingFile'];
}
}
/**
* Initialise the router and start grabbing routes
*
* @return void
*/
public function init() {
include $this->getRoutingFile();
$uri = str_replace(PPI_Helper::getConfig()->system->base_url, '/', PPI_Helper::getFullUrl());
$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);
}
$route = $val;
break;
}
}
// 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;
}
}