1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
class Loader |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Application Object |
9
|
|
|
* |
10
|
|
|
* @var \System\Application |
11
|
|
|
*/ |
12
|
|
|
private $app; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controllers container |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $controllers = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Models container |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $models = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Models container |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $middlewares = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* |
38
|
|
|
* @param \System\Application $app |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Application $app) |
41
|
|
|
{ |
42
|
|
|
$this->app = $app; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Call the given controller with the given method |
47
|
|
|
* and pass the given arguments to the controller method |
48
|
|
|
* |
49
|
|
|
* @param string $controller |
50
|
|
|
* @param string $method |
51
|
|
|
* @param array $arguments |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function action($controller, $method, array $arguments) |
55
|
|
|
{ |
56
|
|
|
$object = $this->controller($controller); |
57
|
|
|
|
58
|
|
|
return call_user_func([$object, $method], $arguments); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Call the given controller |
63
|
|
|
* |
64
|
|
|
* @param string $controller |
65
|
|
|
* @return object |
66
|
|
|
*/ |
67
|
|
|
public function controller($controller) |
68
|
|
|
{ |
69
|
|
|
$controller = $this->getControllerName($controller); |
70
|
|
|
|
71
|
|
|
if (!$this->hasController($controller)) { |
72
|
|
|
|
73
|
|
|
$this->addController($controller); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->getController($controller); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determine if the given class|controller exists |
81
|
|
|
* in the controllers container |
82
|
|
|
* |
83
|
|
|
* @param string $controller |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
private function hasController($controller) |
87
|
|
|
{ |
88
|
|
|
return array_key_exists($controller, $this->controllers); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create new object for the given controller and store it |
93
|
|
|
* in controllers container |
94
|
|
|
* |
95
|
|
|
* @param string $controller |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
private function addController($controller) |
99
|
|
|
{ |
100
|
|
|
$object = new $controller($this->app); |
101
|
|
|
|
102
|
|
|
$this->controllers[$controller] = $object; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the controller object |
107
|
|
|
* |
108
|
|
|
* @param string $controller |
109
|
|
|
* @return object |
110
|
|
|
*/ |
111
|
|
|
private function getController($controller) |
112
|
|
|
{ |
113
|
|
|
return $this->controllers[$controller]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the full class name for the given controller |
119
|
|
|
* |
120
|
|
|
* @param string $controller |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function getControllerName($controller) |
124
|
|
|
{ |
125
|
|
|
$controller .= strpos($controller, 'Controller') ? '' : 'Controller'; |
126
|
|
|
|
127
|
|
|
$controller = 'app\\Controllers\\' . $controller; |
128
|
|
|
|
129
|
|
|
return $controller; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Call the given model |
134
|
|
|
* |
135
|
|
|
* @param string $model |
136
|
|
|
* @return object |
137
|
|
|
*/ |
138
|
|
|
public function model($model) |
139
|
|
|
{ |
140
|
|
|
$model = $this->getModelName($model); |
141
|
|
|
|
142
|
|
|
if (!$this->hasModel($model)) { |
143
|
|
|
|
144
|
|
|
$this->addModel($model); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->getModel($model); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine if the given class|model exists |
152
|
|
|
* in the models container |
153
|
|
|
* |
154
|
|
|
* @param string $model |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
private function hasModel($model) |
158
|
|
|
{ |
159
|
|
|
return array_key_exists($model, $this->models); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Create new object for the given model and store it |
164
|
|
|
* in models container |
165
|
|
|
* |
166
|
|
|
* @param string $model |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
private function addModel($model) |
170
|
|
|
{ |
171
|
|
|
$object = new $model($this->app); |
172
|
|
|
|
173
|
|
|
$this->models[$model] = $object; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the model object |
178
|
|
|
* |
179
|
|
|
* @param string $model |
180
|
|
|
* @return object |
181
|
|
|
*/ |
182
|
|
|
private function getModel($model) |
183
|
|
|
{ |
184
|
|
|
return $this->models[$model]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the full class name for the given model |
189
|
|
|
* |
190
|
|
|
* @param string $model |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
private function getModelName($model) |
194
|
|
|
{ |
195
|
|
|
$model .= strpos($model, 'Model') ? '' : 'Model'; |
196
|
|
|
|
197
|
|
|
$model = 'app\\Models\\' . $model; |
198
|
|
|
|
199
|
|
|
return $model; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Call the given middleware |
204
|
|
|
* |
205
|
|
|
* @param string $middleware |
206
|
|
|
* @return object |
207
|
|
|
*/ |
208
|
|
|
public function middleware($middleware) |
209
|
|
|
{ |
210
|
|
|
$middleware = $this->getMiddlewareName($middleware); |
211
|
|
|
|
212
|
|
|
if (!$this->hasMiddleware($middleware)) { |
213
|
|
|
|
214
|
|
|
$this->addMiddleware($middleware); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $this->getMiddleware($middleware); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Determine if the given class|middleware exists |
222
|
|
|
* in the middlewares container |
223
|
|
|
* |
224
|
|
|
* @param string $middleware |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
private function hasMiddleware($middleware) |
228
|
|
|
{ |
229
|
|
|
return array_key_exists($middleware, $this->middlewares); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Create new object for the given middleware and store it |
234
|
|
|
* in middlewares container |
235
|
|
|
* |
236
|
|
|
* @param string $middleware |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
private function addMiddleware($middleware) |
240
|
|
|
{ |
241
|
|
|
$object = new $middleware($this->app); |
242
|
|
|
|
243
|
|
|
$this->middlewares[$middleware] = $object; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get the middleware object |
248
|
|
|
* |
249
|
|
|
* @param string $middleware |
250
|
|
|
* @return object |
251
|
|
|
*/ |
252
|
|
|
private function getMiddleware($middleware) |
253
|
|
|
{ |
254
|
|
|
return $this->middlewares[$middleware]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get the full class name for the given middleware |
259
|
|
|
* |
260
|
|
|
* @param string $middleware |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
private function getMiddlewareName($middleware) |
264
|
|
|
{ |
265
|
|
|
$middleware .= strpos($middleware, 'Middleware') ? '' : 'Middleware'; |
266
|
|
|
|
267
|
|
|
$middleware = 'app\\Middlewares\\' . $middleware; |
268
|
|
|
|
269
|
|
|
return $middleware; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|