-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
92 lines (77 loc) · 2.99 KB
/
index.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
<?php
// -- ######################## --
// -- # # # # --
// -- # # # # --
// -- ####### # # --
// -- ####### # # --
// -- # # # # --
// -- # # # # --
// -- ######################## --
// Saka Wibawa Putra 2017
require_once('config/include.php');
class DAMVP {
const DEFAULT_CONTROLLER = "index";
const DEFAULT_ACTION = "index";
protected $controller;
protected $action = self::DEFAULT_ACTION;
protected $params = array();
protected $baseLocation = "damvp";
protected $baseAccess = "/index.php";
public function __construct() {
$this->parseUri();
}
protected function parseUri() {
$path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/");
$path = preg_replace('/[^a-zA-Z0-9]\//', "", $path);
if (strpos($path, $this->baseLocation) === 0) {
$path = substr($path, strlen($this->baseLocation));
}
if (strpos($path, $this->baseAccess) === 0) {
$path = substr($path, strlen($this->baseAccess));
}
// Trim request, especially preceding "/"
$path = trim($path, "/");
@list($controller, $action, $params) = explode("/", $path, 3);
if (isset($controller) && !empty($controller)) {
$this->setController($controller);
} else {
// A bug, setController must always be called.
$this->setController(self::DEFAULT_CONTROLLER);
}
if (isset($action)) {
$this->setAction($action);
}
if (isset($params)) {
$this->setParams(explode("/", $params));
}
}
public function setController($controller) {
$controller = ucfirst($controller) . 'Controller';
if (!class_exists($controller)) {
throw new InvalidArgumentException(
"The controller '$controller' has not been defined.");
}
$this->controller = $controller;
return $this;
}
public function setAction($action) {
$reflector = new ReflectionClass($this->controller);
if (!$reflector->hasMethod($action)) {
throw new InvalidArgumentException(
"The controller action '$action' has been not defined.");
}
$this->action = $action;
return $this;
}
public function setParams(array $params) {
$this->params = $params;
return $this;
}
public function run() {
call_user_func_array(array(new $this->controller, $this->action), $this->params);
}
}
// Run the engine!
$frontController = new DAMVP();
$frontController->run();
?>