-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
103 lines (85 loc) · 2.55 KB
/
Copy pathapp.php
File metadata and controls
103 lines (85 loc) · 2.55 KB
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
<?php
class App
{
static public function run()
{
// get baseurl
if(isset($_SERVER["BASEURL"]))
$baseurl = $_SERVER["BASEURL"];
else
$baseurl = "/";
define("__BASEURL__", $baseurl);
// get url path
$url_path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
// strip the trailing slash
$url_path = rtrim($url_path, "/");
// strip the baseurl from url path
$baseurl_pattern = str_replace("/", '\/', $baseurl);
$url_path = preg_replace("/^{$baseurl_pattern}/", "", "{$url_path}/");
// strip the trailing slash (again)
$url_path = rtrim($url_path, "/");
// search for apropriate controller
$found_route = false;
require dirname(__FILE__) . "/../cfg/routes.php";
foreach($routes as $class_path => $url_patterns)
{
foreach($url_patterns as $url_pattern => $method)
{
if($url_pattern == $url_path || fnmatch($url_pattern, $url_path, FNM_PATHNAME))
{
$found_route = true;
break;
}
}
if($found_route)
break;
}
// path to controller (if defined any path)
$class_name = basename($class_path);
if($class_name != $class_path)
define("__CONTROLLER_PATH__", dirname($class_path));
// instantiate controller object
$class_name .= "Controller";
$controller = new $class_name;
// populate controller with properties
$controller->baseurl = $baseurl;
$controller->url_path = $url_path;
$controller->is_post = ("POST" == $_SERVER["REQUEST_METHOD"]);
// trim string POST variables
if($controller->is_post)
{
foreach($_POST as $key => $var)
if(is_string($var))
$_POST[$key] = trim($var);
}
// capture arguments from URI, if any
$args = array();
if(false !== strpos($url_pattern, "*"))
{
$pattern = str_replace("/", '\/', $url_pattern);
$pattern = str_replace("*", "(.*)", $pattern);
preg_match("/$pattern/", $url_path, $args);
array_shift($args);
}
// call initializaton method, if exists
if(method_exists($controller, "__initialize"))
$controller->__initialize();
// call special POST method, if exists
if($controller->is_post && method_exists($controller, "{$method}Post"))
$method = "{$method}Post";
if(method_exists($controller, $method))
{
// call method, with arguments
call_user_func_array(array($controller, $method), $args);
}
else
{
error_log("Method not found for \"{$baseurl}{$url_path}\": {$class_name}::{$method}()");
header("HTTP/1.0 404 Not Found");
die("Error 404: Page not found.");
}
// call finalization method, if exists
if(method_exists($controller, "__finalize"))
$controller->__finalize();
}
}