-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.php
159 lines (148 loc) · 6.25 KB
/
app.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
<?php
// main app class. This is created in the main index.php file and everything is loaded from here.
class App
{
/** @var null The controller */
private $urlController = null;
/** @var null The method (of the above controller), often also named "action" */
private $urlAction = null;
/** @var null Parameter one */
private $urlParameter1 = null;
private $urlParameter2 = null;
private $urlParameter3 = null;
private $urlParameter4 = null;
private $urlParameter5 = null;
private $urlParameter6 = null;
// construct the App class
public function __construct()
{
// start session
Session::start();
// create array with URL parts in $url
if(isset($_GET['url'])) {
// split URL
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
// Put URL parts into according properties
// note the first one is always /root because the mod-rewrite sends us there
$this->urlController = (isset($url[0]) ? strtolower($url[0]) : null);
$this->urlAction = (isset($url[1]) ? strtolower($url[1]) : null);
$this->urlParameter1 = (isset($url[2]) ? strtolower($url[2]) : null);
$this->urlParameter2 = (isset($url[3]) ? strtolower($url[3]) : null);
$this->urlParameter3 = (isset($url[4]) ? strtolower($url[4]) : null);
$this->urlParameter4 = (isset($url[5]) ? strtolower($url[5]) : null);
$this->urlParameter5 = (isset($url[6]) ? strtolower($url[6]) : null);
$this->urlParameter6 = (isset($url[7]) ? strtolower($url[7]) : null);
}
// check for controller: does such a controller exist ?
// Also make sure we are not trying to load / (index) because this will try and load the
// plain controller.php and shit will die
$fileName = CONTROLLER_DIR . $this->urlController . '-controller.php';
if(file_exists($fileName) && $this->urlController != '') {
// get the controller file
require_once($fileName);
// create controller name by converting underscored words to camelcase
$controllerName = str_replace(
' ', '', ucwords(str_replace('-', ' ', $this->urlController))
) . 'Controller';
$this->urlController = new $controllerName();
$action = str_replace('-', '_', $this->urlAction);
// check for method: does such a method exist in the controller ?
if(method_exists($this->urlController, $action)) {
// call the method and pass the arguments to it
if(isset($this->urlParameter1)) {
if(isset($this->urlParameter2)) {
if(isset($this->urlParameter3)) {
if(isset($this->urlParameter4)) {
if(isset($this->urlParameter5)) {
if(isset($this->urlParameter6)) {
// will translate to something like:
// $this->home->method($param1, $param2, $param3, $param4, $param5, $param6);
$this->urlController->{$action}(
$this->urlParameter1,
$this->urlParameter2,
$this->urlParameter3,
$this->urlParameter4,
$this->urlParameter5,
$this->urlParameter6
);
} else {
// will translate to something like:
// $this->home->method($param1, $param2, $param3, $param4, $param5);
$this->urlController->{$action}(
$this->urlParameter1,
$this->urlParameter2,
$this->urlParameter3,
$this->urlParameter4,
$this->urlParameter5
);
}
} else {
// will translate to something like:
// $this->home->method($param1, $param2, $param3, $param3);
$this->urlController->{$action}(
$this->urlParameter1,
$this->urlParameter2,
$this->urlParameter3,
$this->urlParameter4
);
}
} else {
// will translate to something like:
// $this->home->method($param_1, $param_2, $param_3);
$this->urlController->{$action}(
$this->urlParameter1,
$this->urlParameter2,
$this->urlParameter3
);
}
} else {
// will translate to something like: $this->home->method($param_1, $param_2);
$this->urlController->{$action}($this->urlParameter1, $this->urlParameter2);
}
} else {
// will translate to something like: $this->home->method($param_1);
$this->urlController->{$action}($this->urlParameter1);
}
} else {
// if no parameters given, just call the method without parameters, like:
// $this->home->method();
$this->urlController->{$action}();
}
} else {
// cause error if method not found or load index if blank
if($action != '') {
// make sure it exists, render it
header('HTTP/1.0 404 Not Found');
// get the controller file
require_once(CONTROLLER_DIR . 'root-controller.php');
$rootController = new RootController();
debug('<h2>Missing controller method</h2><p>The method <strong>'
. $action
. '()</strong> does not exist. Create method inside '
. $controllerName
. ' and make sure it has a view file.</p>');
$rootController->status404();
} else {
$this->urlController->index();
}
}
} else {
if(!isset($url)) {
// invalid URL, so simply show root/index
require_once(CONTROLLER_DIR . 'root-controller.php');
$rootController = new RootController();
$rootController->index();
} else {
header('HTTP/1.0 404 Not Found');
// invalid URL, so simply show root/index
require_once(CONTROLLER_DIR . 'root-controller.php');
$rootController = new RootController();
$rootController->status404();
}
}
// end session
Session::close();
}
}