1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Application |
5
|
|
|
* The heart of the application |
6
|
|
|
*/ |
7
|
|
|
class Application |
8
|
|
|
{ |
9
|
|
|
/** @var mixed Instance of the controller */ |
10
|
|
|
private $controller; |
11
|
|
|
|
12
|
|
|
/** @var array URL parameters, will be passed to used controller-method */ |
13
|
|
|
private $parameters = array(); |
14
|
|
|
|
15
|
|
|
/** @var string Just the name of the controller, useful for checks inside the view ("where am I ?") */ |
16
|
|
|
private $controller_name; |
17
|
|
|
|
18
|
|
|
/** @var string Just the name of the controller's method, useful for checks inside the view ("where am I ?") */ |
19
|
|
|
private $action_name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Start the application, analyze URL elements, call according controller/method or relocate to fallback location |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
// create array with URL parts in $url |
27
|
|
|
$this->splitUrl(); |
28
|
|
|
|
29
|
|
|
// creates controller and action names (from URL input) |
30
|
|
|
$this->createControllerAndActionNames(); |
31
|
|
|
|
32
|
|
|
// does such a controller exist ? |
33
|
|
|
if (file_exists(Config::get('PATH_CONTROLLER') . $this->controller_name . '.php')) { |
34
|
|
|
|
35
|
|
|
// load this file and create this controller |
36
|
|
|
// example: if controller would be "car", then this line would translate into: $this->car = new car(); |
37
|
|
|
require Config::get('PATH_CONTROLLER') . $this->controller_name . '.php'; |
38
|
|
|
$this->controller = new $this->controller_name(); |
39
|
|
|
|
40
|
|
|
// check are controller and method existing and callable? |
41
|
|
|
if (is_callable(array($this->controller, $this->action_name))) { |
42
|
|
|
if (!empty($this->parameters)) { |
43
|
|
|
// call the method and pass arguments to it |
44
|
|
|
call_user_func_array(array($this->controller, $this->action_name), $this->parameters); |
45
|
|
|
} else { |
46
|
|
|
// if no parameters are given, just call the method without parameters, like $this->index->index(); |
47
|
|
|
$this->controller->{$this->action_name}(); |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
// load 404 error page |
51
|
|
|
require Config::get('PATH_CONTROLLER') . 'ErrorController.php'; |
52
|
|
|
$this->controller = new ErrorController; |
53
|
|
|
$this->controller->error404(); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
// load 404 error page |
57
|
|
|
require Config::get('PATH_CONTROLLER') . 'ErrorController.php'; |
58
|
|
|
$this->controller = new ErrorController; |
59
|
|
|
$this->controller->error404(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get and split the URL |
65
|
|
|
*/ |
66
|
|
|
private function splitUrl() |
67
|
|
|
{ |
68
|
|
|
if (Request::get('url')) { |
69
|
|
|
|
70
|
|
|
// split URL |
71
|
|
|
$url = trim(Request::get('url'), '/'); |
72
|
|
|
$url = filter_var($url, FILTER_SANITIZE_URL); |
73
|
|
|
$url = explode('/', $url); |
74
|
|
|
|
75
|
|
|
// put URL parts into according properties |
76
|
|
|
$this->controller_name = isset($url[0]) ? $url[0] : null; |
77
|
|
|
$this->action_name = isset($url[1]) ? $url[1] : null; |
78
|
|
|
|
79
|
|
|
// remove controller name and action name from the split URL |
80
|
|
|
unset($url[0], $url[1]); |
81
|
|
|
|
82
|
|
|
// rebase array keys and store the URL parameters |
83
|
|
|
$this->parameters = array_values($url); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Checks if controller and action names are given. If not, default values are put into the properties. |
89
|
|
|
* Also renames controller to usable name. |
90
|
|
|
*/ |
91
|
|
|
private function createControllerAndActionNames() |
92
|
|
|
{ |
93
|
|
|
// check for controller: no controller given ? then make controller = default controller (from config) |
94
|
|
|
if (!$this->controller_name) { |
95
|
|
|
$this->controller_name = Config::get('DEFAULT_CONTROLLER'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// check for action: no action given ? then make action = default action (from config) |
99
|
|
|
if (!$this->action_name OR (strlen($this->action_name) == 0)) { |
100
|
|
|
$this->action_name = Config::get('DEFAULT_ACTION'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// rename controller name to real controller class/file name ("index" to "IndexController") |
104
|
|
|
$this->controller_name = ucwords($this->controller_name) . 'Controller'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|