|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zewa; |
|
4
|
|
|
|
|
5
|
|
|
use Zewa\Exception\LookupException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class is the starting point for application |
|
9
|
|
|
* |
|
10
|
|
|
* <code> |
|
11
|
|
|
* |
|
12
|
|
|
* $out = new core\App(); |
|
13
|
|
|
* print $out; |
|
14
|
|
|
* |
|
15
|
|
|
* </code> |
|
16
|
|
|
* |
|
17
|
|
|
* @author Zechariah Walden<zech @ zewadesign.com> |
|
18
|
|
|
*/ |
|
19
|
|
|
class App |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Reference to instantiated controller object. |
|
23
|
|
|
* |
|
24
|
|
|
* @var object |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $instance = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Available configuration files |
|
30
|
|
|
* |
|
31
|
|
|
* @var object |
|
32
|
|
|
*/ |
|
33
|
|
|
private $files; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* System configuration |
|
37
|
|
|
* |
|
38
|
|
|
* @var object |
|
39
|
|
|
*/ |
|
40
|
|
|
public $configuration; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* System service management |
|
44
|
|
|
* |
|
45
|
|
|
* @var object |
|
46
|
|
|
*/ |
|
47
|
|
|
private $services; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Events |
|
51
|
|
|
*/ |
|
52
|
|
|
private static $events; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return value from application |
|
56
|
|
|
* |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $output = false; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Namespaced controller path |
|
63
|
|
|
* |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
private $class; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Instantiated class object |
|
70
|
|
|
* |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
private $instantiatedClass; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Module being accessed |
|
77
|
|
|
* |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
private $module; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Controller being accessed |
|
84
|
|
|
* |
|
85
|
|
|
* @var string |
|
86
|
|
|
*/ |
|
87
|
|
|
private $controller; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Method being accessed |
|
91
|
|
|
* |
|
92
|
|
|
* @var string |
|
93
|
|
|
*/ |
|
94
|
|
|
private $method; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Params being passed |
|
98
|
|
|
* |
|
99
|
|
|
* @var array |
|
100
|
|
|
*/ |
|
101
|
|
|
private $params; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Instantiated router class |
|
105
|
|
|
* |
|
106
|
|
|
* @var object |
|
107
|
|
|
*/ |
|
108
|
|
|
private $router; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Instantiated request class |
|
112
|
|
|
* |
|
113
|
|
|
* @var object |
|
114
|
|
|
*/ |
|
115
|
|
|
private $request; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Application bootstrap process |
|
119
|
|
|
* |
|
120
|
|
|
* The application registers the configuration in the app/config/core.php |
|
121
|
|
|
* and then processes, and makes available the configured resources |
|
122
|
|
|
*/ |
|
123
|
|
|
public function __construct() |
|
124
|
|
|
{ |
|
125
|
|
|
self::$instance = $this; |
|
126
|
|
|
//@TODO: unset unnececessary vars/profile/unit testing..? how? |
|
127
|
|
|
//@TODO: better try/catch usage |
|
128
|
|
|
//@TODO: setup custom routing based on regex |
|
129
|
|
|
// (can't we get away without using regex tho?)!!!!!!! routesssssss!!!!!!!! |
|
130
|
|
|
$files = (object)glob(APP_PATH . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . '*.php'); |
|
131
|
|
|
$oFiles = []; |
|
132
|
|
|
|
|
133
|
|
|
foreach ($files as $index => $filename) { |
|
134
|
|
|
$pieces = explode('/', $filename); |
|
135
|
|
|
$file = $pieces[count($pieces) - 1]; |
|
136
|
|
|
$fileProperties = explode('.', $file); |
|
137
|
|
|
$currentFile = $fileProperties[0]; |
|
138
|
|
|
$oFiles[$currentFile] = $filename; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->files = $oFiles; |
|
|
|
|
|
|
142
|
|
|
$this->configuration = new \stdClass(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Calls the proper shell for app execution |
|
147
|
|
|
* |
|
148
|
|
|
* @access private |
|
149
|
|
|
*/ |
|
150
|
|
|
public function initialize() |
|
151
|
|
|
{ |
|
152
|
|
|
$appConfig = $this->getConfiguration('app'); |
|
153
|
|
|
|
|
154
|
|
|
if ($appConfig->environment == 'development') { |
|
155
|
|
|
ini_set('display_errors', 1); |
|
156
|
|
|
ini_set('display_startup_errors', 1); |
|
157
|
|
|
error_reporting(E_ALL); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->prepare(); |
|
161
|
|
|
$this->start(); |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* App preparation cycle |
|
168
|
|
|
*/ |
|
169
|
|
|
private function prepare() |
|
170
|
|
|
{ |
|
171
|
|
|
App::callEvent('preApplication'); |
|
172
|
|
|
|
|
173
|
|
|
$this->registerSession(); |
|
174
|
|
|
|
|
175
|
|
|
$this->router = new Router(); |
|
176
|
|
|
$this->request = new Request(); |
|
177
|
|
|
$this->database = new Database(); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
$this->setService('router', $this->router); |
|
180
|
|
|
$this->setService('request', $this->request); |
|
181
|
|
|
$this->setService('database', $this->database); |
|
182
|
|
|
|
|
183
|
|
|
$this->module = ucfirst($this->configuration->router->module); |
|
184
|
|
|
$this->controller = ucfirst($this->configuration->router->controller); |
|
185
|
|
|
$this->method = $this->configuration->router->method; |
|
186
|
|
|
$this->params = $this->configuration->router->params; |
|
187
|
|
|
$this->class = '\\App\\Modules\\' . $this->module . '\\Controllers\\' . ucfirst($this->controller); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
private function prepareServices() |
|
191
|
|
|
{ |
|
192
|
|
|
if (isset($this->files['services'])) { |
|
193
|
|
|
$services = include $this->files['services']; |
|
194
|
|
|
if ($services === false) { |
|
195
|
|
|
$this->services = []; |
|
|
|
|
|
|
196
|
|
|
} else { |
|
197
|
|
|
$this->services = (object)$services; |
|
198
|
|
|
} |
|
199
|
|
|
} else { |
|
200
|
|
|
throw new LookupException('No service configuration found.'); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function getService($service, $new = false, $options = []) |
|
205
|
|
|
{ |
|
206
|
|
|
if ($this->services === null) { |
|
207
|
|
|
$this->prepareServices(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if ($new === false) { |
|
211
|
|
|
return $this->services->$service; |
|
212
|
|
|
} elseif ($new === true || empty($this->services->$service)) { |
|
213
|
|
|
$this->services->$service = call_user_func_array($this->services->$service, $options); |
|
214
|
|
|
return $this->services->$service; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function setService($service, $class) |
|
219
|
|
|
{ |
|
220
|
|
|
if ($this->services === null) { |
|
221
|
|
|
$this->prepareServices(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$this->services->$service = $class; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param mixed string with reference to config |
|
229
|
|
|
* @return mixed bool or config values |
|
230
|
|
|
*/ |
|
231
|
28 |
|
public function getConfiguration($config = null) |
|
232
|
|
|
{ |
|
233
|
28 |
|
if ($config !== null) { |
|
234
|
28 |
|
if (! empty($this->configuration->$config)) { |
|
235
|
28 |
|
return $this->configuration->$config; |
|
236
|
1 |
|
} elseif (! empty($this->files{$config})) { |
|
237
|
1 |
|
$vars = include $this->files{$config}; |
|
238
|
|
|
|
|
239
|
1 |
|
if ($vars === false) { |
|
240
|
|
|
$this->configuration->{$config} = false; |
|
241
|
|
|
} else { |
|
242
|
|
|
//turn array into object the dirty way? |
|
243
|
1 |
|
$this->configuration->{$config} = json_decode(json_encode($vars)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
1 |
|
return $this->configuration->$config; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $this->configuration; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param $config mixed null|string |
|
257
|
|
|
* @param null|object|array optional array of configuration data |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
* @throws Exception\StateException |
|
261
|
|
|
*/ |
|
262
|
18 |
|
public function setConfiguration($config = null, $configObject = null) |
|
263
|
|
|
{ |
|
264
|
18 |
|
if ($config !== null && $configObject !== null && !empty($configObject)) { |
|
265
|
18 |
|
$this->configuration->$config = $configObject; |
|
266
|
18 |
|
return true; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
throw new Exception\StateException('You must provide the configuration key, and its value.'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Registers the session object |
|
274
|
|
|
* |
|
275
|
|
|
* @access private |
|
276
|
|
|
*/ |
|
277
|
|
|
private function registerSession() |
|
278
|
|
|
{ |
|
279
|
|
|
$session = $this->getConfiguration('session'); |
|
280
|
|
|
|
|
281
|
|
|
if ($session !== false) { |
|
282
|
|
|
App::callEvent('preSession'); |
|
283
|
|
|
new SessionHandler( |
|
284
|
|
|
$session->interface, |
|
285
|
|
|
$session->securityCode, |
|
286
|
|
|
$session->expiration, |
|
287
|
|
|
$session->domain, |
|
288
|
|
|
$session->lockToUserAgent, |
|
289
|
|
|
$session->lockToIP, |
|
290
|
|
|
$session->gcProbability, |
|
291
|
|
|
$session->gcDivisor, |
|
292
|
|
|
$session->tableName |
|
293
|
|
|
); |
|
294
|
|
|
App::callEvent('postSession'); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Verifies the provided application request is a valid request |
|
302
|
|
|
* |
|
303
|
|
|
* @access private |
|
304
|
|
|
*/ |
|
305
|
|
|
private function processRequest() |
|
306
|
|
|
{ |
|
307
|
|
|
$moduleExist = file_exists(APP_PATH . '/Modules/' . $this->module); |
|
308
|
|
|
$classExist = class_exists($this->class); |
|
309
|
|
|
$methodExist = method_exists($this->class, $this->method); |
|
310
|
|
|
|
|
311
|
|
|
if (!$moduleExist || !$classExist || !$methodExist) { |
|
312
|
|
|
$view = new View(); |
|
313
|
|
|
$this->output = $view->render404(['Invalid method requests']); //Router::show404( |
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
return true; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Processes the application request |
|
322
|
|
|
* |
|
323
|
|
|
* @access private |
|
324
|
|
|
*/ |
|
325
|
|
|
private function start() |
|
326
|
|
|
{ |
|
327
|
|
|
if (!$this->processRequest()) { |
|
328
|
|
|
return false; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
App::callEvent('preController'); |
|
332
|
|
|
$this->instantiatedClass = new $this->class(); |
|
|
|
|
|
|
333
|
|
|
App::callEvent('postController'); |
|
334
|
|
|
|
|
335
|
|
|
$this->output = call_user_func_array( |
|
336
|
|
|
array(&$this->instantiatedClass, $this->method), |
|
337
|
|
|
$this->params |
|
338
|
|
|
); |
|
339
|
|
|
} |
|
340
|
|
|
/** |
|
341
|
|
|
* Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called. |
|
342
|
|
|
* |
|
343
|
|
|
* @param string $event name |
|
344
|
|
|
* @param mixed $value the optional value to pass to each callback |
|
345
|
|
|
* @param mixed $callback the method or function to call - FALSE to remove all callbacks for event |
|
|
|
|
|
|
346
|
|
|
*/ |
|
347
|
|
|
|
|
348
|
|
|
public static function addEvent($event, $callback = false) |
|
349
|
|
|
{ |
|
350
|
|
|
// Adding or removing a callback? |
|
351
|
|
|
if ($callback !== false) { |
|
352
|
|
|
self::$events[$event][] = $callback; |
|
353
|
|
|
} else { |
|
354
|
|
|
unset(self::$events[$event]); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
public function callEvent($event, $method = false, $arguments = []) |
|
359
|
|
|
{ |
|
360
|
|
|
if (isset(self::$events[$event])) { |
|
361
|
|
|
foreach (self::$events[$event] as $e) { |
|
362
|
|
|
if ($method !== false) { // class w/ method specified |
|
363
|
|
|
$object = new $e(); |
|
364
|
|
|
$value = call_user_func_array( |
|
365
|
|
|
[&$object, $method], |
|
366
|
|
|
$arguments |
|
367
|
|
|
); |
|
368
|
|
|
} else { |
|
369
|
|
|
if (class_exists($e)) { |
|
370
|
|
|
$value = new $e($arguments); // class w/o method specified |
|
371
|
|
|
} else { |
|
372
|
|
|
$value = call_user_func($e, $arguments); // function yuk |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
return $value; |
|
|
|
|
|
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Prepare application return value into a string |
|
384
|
|
|
* |
|
385
|
|
|
* @access public |
|
386
|
|
|
* @return string |
|
387
|
|
|
*/ |
|
388
|
|
|
public function __toString() |
|
389
|
|
|
{ |
|
390
|
|
|
if (!$this->output) { |
|
391
|
|
|
$this->output = ''; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
App::callEvent('postApplication'); |
|
395
|
|
|
|
|
396
|
|
|
return $this->output; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Returns a reference of object once instantiated |
|
402
|
|
|
* |
|
403
|
|
|
* @access public |
|
404
|
|
|
* @return object |
|
405
|
|
|
*/ |
|
406
|
28 |
|
public static function getInstance() |
|
407
|
|
|
{ |
|
408
|
28 |
|
if (self::$instance === null) { |
|
409
|
|
|
return false; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
28 |
|
return self::$instance; |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..