-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFamework.php
360 lines (308 loc) · 11 KB
/
Famework.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Famework;
use Famework\Request\Famework_Request;
use Famework\Session\Famework_Session;
use Famework\Handler\Famework_Handler;
use Famework\Registry\Famework_Registry;
use Famework\Config\Famework_Config;
use Famework\Db\Famework_Database;
use Famework\View\Famework_View;
if (!defined('FAMEWORK_ROOT')) {
define('FAMEWORK_ROOT', dirname(__FILE__));
}
class Famework {
const ENV_DEV = 1;
const ENV_PROD = 2;
/**
* @var Famework_Config
*/
private $_routes;
private $_controller;
private $_action;
// from config
private $_http_root;
private $_view_path;
// params
private $_params;
/**
* @var \Famework\Request\Famework_Request
*/
private $_request;
private $_ctrl_namespace = '';
/**
* load the config.ini and routes.ini (or the ini strings)
* <b>Format for routes.ini:</b><br><br>
* <code>
* [index]<br>
* famework_route = {root}<br>
* famework_controller = index<br>
* famework_action = index<br>
* <br>
* [default]<br>
* famework_route = {root}/:controller/:action<br>
* famework_controller = :controller<br>
* famework_action = :action<br>
* <br>
* [custom]<br>
* famework_route = {root}/test/playground<br>
* famework_controller = tester<br>
* famework_action = index<br>
* </code>
* @param \Famework\Config\Famework_Config $config
* @param \Famework\Config\Famework_Config $routes
*/
public function __construct(Famework_Config $config, Famework_Config $routes) {
// set encoding
mb_internal_encoding('UTF-8');
// set application environment
$env = $config->getValue('famework', 'env');
if (strtolower($env) === 'dev' || strtolower($env) === 'development' || strtolower($env) === '1') {
Famework_Registry::setEnv(self::ENV_DEV);
} elseif (Famework_Registry::getEnv() === NULL) {
Famework_Registry::setEnv(self::ENV_PROD);
}
// interpret the configuration data
$this->useConfig($config);
// save route for further use
$this->_routes = $routes;
// connect with database if required
$dbinstance = Famework_Database::loadInstance($config);
if ($dbinstance !== NULL) {
Famework_Registry::setDb($dbinstance);
}
}
private function useConfig($config) {
$this->_http_root = $config->getValue('famework', 'public_root');
if ($this->_http_root === NULL) {
$this->_http_root = '/';
}
$this->_view_path = $config->getValue('famework', 'view_path');
if ($this->_view_path === NULL) {
$this->_view_path = '/';
}
if ($config->getValue('famework', 'use_session') == TRUE) {
Famework_Session::start();
}
}
/**
* Call this to handle a request.
* Then call Famework::loadController();
*/
public function handleRequest($path = NULL, $allowSlashInParam = FALSE) {
$result = array();
if ($path === NULL) {
$requestUri = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL);
} else {
$requestUri = $path;
}
// remove GET params
$pos = strpos($requestUri, '?');
if ($pos !== FALSE) {
$requestUri = substr($requestUri, 0, $pos);
}
$routes = $this->_routes->getByKey('famework_route');
foreach ($routes as $route) {
$route['value'] = normalizePath(str_replace('{root}', $this->_http_root, $route['value']));
if (substr($requestUri, -1, 1) !== '/') {
$requestUri .= '/';
}
if (substr($route['value'], -1, 1) !== '/') {
$route['value'] .= '/';
}
if ($route['value'] === $requestUri) {
$result['controller'] = $this->_routes->getValue($route['group'], 'famework_controller');
$result['action'] = $this->_routes->getValue($route['group'], 'famework_action');
break;
} else {
$pattern = $this->getRoutePattern($route['value']);
// find params
preg_match_all('/:(\w+)/', $pattern, $params);
foreach ($params[1] as $param) {
$pattern = str_replace(':' . $param, '(?<' . $param . '>.*?)', $pattern);
}
if (($pregres = preg_match($pattern, $requestUri, $matches)) !== FALSE && $pregres === 1) {
if (!isset($matches['crtl'])) {
$matches['crtl'] = '';
}
if (!isset($matches['act'])) {
$matches['act'] = '';
}
$result['controller'] = str_replace(':controller', $matches['crtl'], $this->_routes->getValue($route['group'], 'famework_controller'));
$result['action'] = str_replace(':action', $matches['act'], $this->_routes->getValue($route['group'], 'famework_action'));
// add params
$this->_params = array();
foreach ($params[1] as $param) {
if (strpos($matches[$param], '/') !== FALSE && !$allowSlashInParam) {
// something went wrong because param is URI fragment
unset($result['controller']);
unset($result['action']);
break;
}
$this->_params[$param] = $matches[$param];
}
break;
}
}
}
if (isset($result['controller']) && isset($result['action'])) {
if (strpos($result['controller'], '/') !== FALSE || strpos($result['action'], '/') !== FALSE) {
// something went wrong because controller or action is URI fragment
unset($result['controller']);
unset($result['action']);
} else {
$this->setController($result['controller']);
$this->setAction($result['action']);
}
}
$this->_request = new Famework_Request($this->_params);
}
private function getRoutePattern($route) {
$pattern = str_replace(':controller', '(?<crtl>.*?)', $route);
$pattern = str_replace(':action', '(?<act>.*?)', $pattern);
$pattern = str_replace('/', '\/', $pattern);
$pattern = '/^' . $pattern . '$/';
return $pattern;
}
private function setController($string) {
$string = ucfirst(strtolower($string)) . 'Controller';
$this->_controller = $string;
}
private function setAction($string) {
$actionparts = explode('.', $string);
$action = '';
foreach ($actionparts as $part) {
$action .= strtolower($part);
}
$this->_action = $action . 'Action';
}
public function truncateRequest() {
$this->_controller = NULL;
$this->_action = NULL;
}
public function setControllerNamespace($namespace) {
// add prefix slashes
if (strpos($namespace, '\\') !== 0) {
$namespace = '\\' . $namespace;
}
// add suffix slashes
if (strpos($namespace, '\\') !== strlen($namespace) - 1) {
$namespace .= '\\';
}
$this->_ctrl_namespace = $namespace;
}
/**
* Call this to load the controller, action and view.
* Call Famework::handleRequest() <b>before</b>!
*/
public function loadController($nooutput = FALSE) {
if ($this->_controller === NULL || $this->_action === NULL) {
$this->_controller = 'IndexController';
$this->_action = 'notfoundAction';
header('HTTP/1.0 404 Not Found');
}
// compose controller
$controller = $this->_ctrl_namespace . $this->_controller;
// we need the default 404 error page, because developer provides none
if (!class_exists($controller, TRUE) || !in_array('Famework\Controller\Famework_Controller', class_parents($controller))) {
$this->default404($nooutput);
}
// get view
$pageview = new Famework_View($this->_request);
$pageview->setController($this->_controller);
$pageview->setAction($this->_action);
// save view in registry
Famework_Registry::setView($pageview);
// get controller
$ctrlClass = new $controller($pageview);
if (!method_exists($ctrlClass, $this->_action)) {
$this->default404($nooutput);
}
// run action
$action = $this->_action;
$ctrlClass->$action();
// render view
$pageview->render($this->_view_path);
}
private function default404($nooutput) {
if ($nooutput === TRUE) {
exit();
}
header('HTTP/1.0 404 Not Found');
echo '<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body>
</html>';
exit();
}
/**
* register the default error- and exceptionhandler for developers
*/
public static function registerDefaultHandler() {
Famework_Handler::registerDefaultHandler();
}
/**
* Get the value of an URI path param
* @param string $name The key which was set in the routes.ini
* @return string The value
*/
public function getRequestParam($name) {
if ($this->_request !== NULL) {
return $this->_request->getRequestParam($name);
}
return NULL;
}
public function getController() {
return $this->_controller;
}
public function getAction() {
return $this->_action;
}
}
/**
* Combine several path parts to one path string
* @param array $paths The path parts
* @return string the combined path
*/
function combinePath(array $paths = NULL) {
if (count($paths) === 0) {
return NULL;
}
if (count($paths) === 1) {
return $paths[0];
}
$result = $paths[0];
for ($i = 1; $i < count($paths); $i++) {
if (substr($result, -1) !== '/') {
if (substr($paths[$i], 0, 1) !== '/') {
$result .= '/' . $paths[$i];
} else {
$result .= $paths[$i];
}
} else {
if (substr($paths[$i], 0, 1) !== '/') {
$result .= $paths[$i];
} else {
$result .= substr($paths[$i], 1);
}
}
}
return str_replace('\\', '/', normalizePath($result));
}
/**
* Remove double, trible, ... slashes from a string
* @param string $path
* @return string
*/
function normalizePath($path) {
while (strpos($path, '//') !== FALSE) {
$path = str_replace('//', '/', $path);
}
return $path;
}
require 'loader.php';