-
Notifications
You must be signed in to change notification settings - Fork 1
/
ControllerFront.php
181 lines (148 loc) · 5.13 KB
/
ControllerFront.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
<?php
class ControllerFront
{
protected static $instance = null;
protected static $requestUri;
protected static $requestA;
protected static $route;
private function __construct()
{
self::setUri();
}
public static function setUri()
{
//mix and match, gets the job done, should be cleaned up
self::$requestUri = ($qsa = strpos($_SERVER['REQUEST_URI'],'?')) ? substr($_SERVER['REQUEST_URI'],0,$qsa) : $_SERVER['REQUEST_URI'];
self::$requestA = array_slice(explode('/',self::$requestUri),1);
//setConfig("CMS_ROOT",substr($_SERVER['PHP_SELF'],0,-strlen('index.php')));
//script name = last item in array
$a = explode("/",$_SERVER['PHP_SELF']);
$t = $a[count($a)-1];
/*
if(isset($_SERVER["HTTP_REFERER"])){
$this->refA = explode("/",$_SERVER["HTTP_REFERER"]);
$t = array_search($_SERVER['SERVER_NAME'],$this->refA);
$this->refA = array_slice($this->refA, ($t+1));
}
*/
$tA = explode("/",substr($_SERVER['PHP_SELF'],1,-(strlen($t) + 1)));
define('BASE',substr($_SERVER['PHP_SELF'],0,-strlen($t)));
if($tA[0] != ''){
array_splice(self::$requestA,0,count($tA));
/*
if(isset($this->refA)){
$this->refA = array_slice($this->refA,count($tA));
}
*/
}
self::$requestUri = '/' . join('/',self::$requestA);
}
public static function getInstance()
{
if(!self::$instance){
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
public static function getUri()
{
return array('string'=>self::$requestUri,'array'=>self::$requestA);
}
private static function findRoute($custom_routes=array())
{
// Default routes (use these if they are not overridden in the router.php file)
$default_routes = array();
// Default homepage (index)
if (!isset($custom_routes['default_home'])) $default_routes['default_home'] = array('uri' => "/^\/$/");
// Default controller
if (!isset($custom_routes['default_controller'])) $default_routes['default_controller'] = array('uri' => "/^\/(?P<controller>[a-z0-9_-]+)$/i");
// Default controller+action
if (!isset($custom_routes['default_controller_action'])) $default_routes['default_controller_action'] = array('uri' => "/^\/(?P<controller>[a-z0-9_-]+)\/(?P<action>[a-z0-9_-]+)$/i");
// Combine default and custom routes
$routes = array_merge($custom_routes,$default_routes);
// Try to find a match
foreach ($routes as $route) {
if (preg_match($route['uri'],self::$requestUri,$regs)) {
// Make sure match is exact
if ($regs[0] == self::$requestUri) {
//Merge and overwrite
//$regs = array_merge($regs,$route);
// Set key 'uri' with uri match
$regs['uri'] = $regs[0];
// Make sure controller is set, or use DEFAULT_CONTROLLER
if (!isset($regs['controller'])) {
$regs['controller'] = isset($route['controller']) ? $route['controller'] : DEFAULT_CONTROLLER;
}
// Make sure action is set, or use DEFAULT_ACTION
if (!isset($regs['action'])) {
$regs['action'] = isset($route['action']) ? $route['action'] : DEFAULT_ACTION;
}
// Find diffs and merge - don't overwrite!
if($diffs = array_diff_assoc($route, $regs)){
foreach($diffs as $key=>$value){
if(!array_key_exists($key,$regs)){
$regs[$key] = $value;
}
}
}
// Only return a found route if the controller exists!
if (file_exists(CONTROLLERS . ucfirst($regs['controller']) . 'Controller.php')) {
return $regs;
}
}
}
}
}
public static function getRoute()
{
return self::$route;
}
//needs to do the dispatching here so we can grab entire output and filter with a pre and post render plugin
public static function dispatch($routes)
{
$output = '';
// Find a matching route
if ($route = self::findRoute($routes)) {
self::$route = $route;
//format the file name of the controller - camel case, append Controlller
$name = ucfirst($route['controller']) . 'Controller';
$file = CONTROLLERS . $name . '.php';
if (file_exists($file) && require_once $file) {
//action
$action = DEFAULT_ACTION;
if(isset($route['action'])){
$action = $route['action'];
}
$action = ucfirst($action);
$controller = new $name($route);
//could force index to always exist by using an interface or abstract class
//however, this needs to be here to catch human error in a route
if (method_exists($controller,$action)) {
$controller->$action();
$output .= $controller->render();
if (function_exists('plugin__pre_render')) {
$output = plugin__pre_render($output);
}
print $output;
if (function_exists('plugin__post_render')) {
plugin__post_render($output);
}
//stop matching
return;
} else {
//action doesn't exist
//self::$error->code = 404;
ErrorHandler::message($action.' action doesn\'t exist in '.$name);
}
} else {
//self::$error->code = 404;
ErrorHandler::message($name.' controller doesn\'t exist');
}
} else {
//self::$error->code = 404;
//ErrorHandler::message('Router did not match any controllers');
ErrorHandler::message('No valid route found!');
}
}
}