1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Core; |
4
|
|
|
/** |
5
|
|
|
* Router, will take in url parameters in the form |
6
|
|
|
* index.php?url=Controller/Method/param1/param2 |
7
|
|
|
* |
8
|
|
|
* if we have a special section (admin section), load that namespace |
9
|
|
|
* |
10
|
|
|
* PHP version 7 |
11
|
|
|
*/ |
12
|
|
|
class Router |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* the default namespace |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $currentNamespace = 'App\Controllers\\'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* the default controller |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $currentController = 'Home'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* the default method |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $currentMethod = 'index'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* the current parameters |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $currentParams = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The special sections that have there own namespace. |
41
|
|
|
* All in lower, will be converted to camelCase later |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $sections = [ |
45
|
|
|
'admin', |
46
|
|
|
'ajax' |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
private $container; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Router constructor, will set the controller, method and params |
54
|
|
|
* |
55
|
|
|
* will then call the dispatcher to instantiate the controller and method |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function __construct(Container $container) |
59
|
|
|
{ |
60
|
|
|
$this->container = $container; |
61
|
|
|
//get the current url |
62
|
|
|
$url = $this->getUrl(); |
63
|
|
|
|
64
|
|
|
//checking if a special namespace is present at the start of the url. |
65
|
|
|
//if so, then strip and set the new namespace |
66
|
|
|
if (isset($url[0]) && in_array($url[0], $this->sections)) { |
67
|
|
|
$specialNamespace = array_shift($url); |
68
|
|
|
|
69
|
|
|
//making sure we have a single backslash |
70
|
|
|
$specialNamespace = rtrim($specialNamespace, '\\') . '\\'; |
71
|
|
|
|
72
|
|
|
//capitalize the special namespace |
73
|
|
|
$specialNamespace = $this->convertToStudlyCaps($specialNamespace); |
74
|
|
|
|
75
|
|
|
$this->currentNamespace .= $specialNamespace; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//applying the controllers and methods |
79
|
|
|
if (isset($url[0]) && $url[0] != null) { |
80
|
|
|
$this->currentController = $this->convertToStudlyCaps($url[0]); |
81
|
|
|
unset($url[0]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($url[1]) && $url[1] != null) { |
85
|
|
|
$this->currentMethod = $this->convertToCamelCase($url[1]); |
86
|
|
|
unset($url[1]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//grabbing the remaining parameters |
90
|
|
|
$this->currentParams = $url ? array_values($url) : []; |
91
|
|
|
$this->dispatch(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the controller, action and params from the url= string |
97
|
|
|
* |
98
|
|
|
* @return array decomposed url |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
|
protected function getUrl(): array |
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
//$url = $this->container->getRequest()->getData('url'); |
105
|
|
|
$url = $this->container->getRequest()->getUri(); |
106
|
|
|
if ($url) { |
107
|
|
|
//remove right slash |
108
|
|
|
$url = rtrim($url, '/'); |
109
|
|
|
$url = ltrim($url, '/'); |
110
|
|
|
|
111
|
|
|
//convert all to lower for easier comparing. Will convert to camelCase after |
112
|
|
|
//this will avoid cap probs with links and user input |
113
|
|
|
$url = strtolower($url); |
114
|
|
|
|
115
|
|
|
//sanitize the url for security |
116
|
|
|
$url = filter_var($url, FILTER_SANITIZE_URL); |
117
|
|
|
|
118
|
|
|
//EXPLODE, BOOM, TRANSFORMERS, MICHAEL BAY |
119
|
|
|
$url = explode('/', $url); |
120
|
|
|
|
121
|
|
|
return $url; |
122
|
|
|
} |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Run the router call and instantiate the controller + method |
129
|
|
|
* also takes care of throwing errors |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
* |
133
|
|
|
* @throws \exception if the controller or method doesn't exist |
134
|
|
|
*/ |
135
|
|
|
protected function dispatch(): void |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
//try to create the controller object |
139
|
|
|
$fullControllerName = $this->currentNamespace . $this->currentController; |
140
|
|
|
|
141
|
|
|
//make sure the class exists before continuing |
142
|
|
|
if (!class_exists($fullControllerName)) { |
143
|
|
|
throw new \Exception("Class $fullControllerName doesn't exist", 404); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
//instantiate our controller |
147
|
|
|
$controllerObj = new $fullControllerName($this->container); |
148
|
|
|
//try to run the associated method and the pass parameters |
149
|
|
|
$methodToRun = $this->currentMethod; |
150
|
|
|
|
151
|
|
|
//make sure our method exists before continuing |
152
|
|
|
if (!method_exists($controllerObj, $methodToRun)) { |
153
|
|
|
throw new \Exception("ERROR - Method $methodToRun() doesn't exist or is inaccessible"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
call_user_func_array([$controllerObj, $methodToRun], $this->currentParams); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Convert the string with hyphens to StudlyCaps, |
161
|
|
|
* e.g. post-authors => PostAuthors |
162
|
|
|
* |
163
|
|
|
* @param string $string The string to convert |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function convertToStudlyCaps($string): string |
168
|
|
|
{ |
169
|
|
|
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string))); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Convert the string with hyphens to camelCase, |
174
|
|
|
* e.g. add-new => addNew |
175
|
|
|
* |
176
|
|
|
* @param string $string The string to convert |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function convertToCamelCase($string): string |
181
|
|
|
{ |
182
|
|
|
return lcfirst($this->convertToStudlyCaps($string)); |
183
|
|
|
} |
184
|
|
|
} |