-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.php
356 lines (302 loc) · 10.8 KB
/
Loader.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* DframeFramework
* Copyright (c) Sławomir Kaleta.
*
* @license https://github.com/dframe/dframe/blob/master/LICENCE (MIT)
*/
namespace Dframe\Loader;
use Bootstrap;
use Dframe\Config\Config;
use Dframe\Loader\Exceptions\LoaderException;
use Dframe\Router\Response;
use Dframe\Router\Router;
use Exception;
/**
* Loader Class.
*
* @author Sławomir Kaleta <slaszka@gmail.com>
*/
class Loader
{
/**
* Path logs
*/
protected const LOG_DIR = \APP_DIR . 'View/cache/logs/';
/**
* @var Router
*/
public Router $router;
/**
* @var Bootstrap|null
*/
public $baseClass;
/**
* @var string
*/
protected string $fileExtension = '.php';
/**
* @var string
*/
protected string $namespaceSeparator = '\\';
/**
* Loader constructor.
*
* @param null|object|Bootstrap $bootstrap
*
* @throws LoaderException
*/
public function __construct($bootstrap = null)
{
spl_autoload_register([$this, 'autoload']);
if (!defined('APP_DIR')) {
throw new LoaderException('Please Define APP_DIR in Main config.php', 500);
}
if (!defined('SALT')) {
throw new LoaderException('Please Define SALT in Main config.php', 500);
}
$this->baseClass = empty($bootstrap) ? new Bootstrap() : $bootstrap;
$baseClass = $this->baseClass;
foreach ($baseClass->providers['core'] ?? [] as $key => $value) {
$this->$key = new $value($this);
if (method_exists($this->$key, 'boot') or is_callable([$this->$key, 'boot'])) {
$this->$key->boot($this);
}
}
if (is_null($bootstrap)) {
foreach ($baseClass->providers['baseClass'] ?? [] as $key => $value) {
$this->baseClass->$key = new $value($this->baseClass);
}
$this->baseClass->modules = (object)[];
foreach ($baseClass->providers['modules'] ?? [] as $key => $value) {
$this->baseClass->modules->$key = new $value($this);
$this->baseClass->modules->$key->register();
$this->baseClass->modules->$key->boot($this->baseClass->modules->$key->app);
}
} else {
foreach ($this->baseClass->providers['modules'] ?? [] as $key => $value) {
foreach ($baseClass->providers['core'] ?? [] as $key2 => $value2) {
if (method_exists($this->$key2, 'boot') or is_callable([$this->$key2, 'boot'])) {
$this->$key2->boot($this->baseClass->modules->$key->app);
}
}
}
}
return $this;
}
/**
* @param $class
*
* @return bool|mixed
* @throws LoaderException
*/
public static function autoload($class)
{
$args = $class;
if (substr($class, -4) == "View") {
$class = substr($class, 0, -4);
} elseif (substr($class, -5) == "Model") {
$class = substr($class, 0, -5);
} elseif (substr($class, -10) == "Controller") {
$class = substr($class, 0, -10);
} else {
return false;
}
$directory = explode('/', str_replace('\\', '/', ltrim($class, '\\')));
$class = array_pop($directory);
$directory = array_merge($directory, explode('/', str_replace('_', '/', $class)));
$class = array_pop($directory);
$directory = rtrim(\APP_DIR . join('/', $directory), '/');
if (!empty($class)) {
if (is_file($path = $directory . '/' . $class . '.php')) {
return require_once $path;
}
if (realpath(__DIR__ . '/' . $directory)) {
throw new LoaderException('Couldn\'t locate ' . $args);
}
}
}
/**
* Model Loader
*
* @param string $name
* @param null|string $namespace
*
* @return mixed
*/
public function loadModel($name, $namespace = null)
{
return $this->loadObject($name, 'Model', $namespace);
}
/**
* Loading files
*
* @param string $name
* @param string $type
* @param null|string $namespace
*
* @return mixed
*/
protected function loadObject($name, $type, $namespace = null)
{
try {
if (!$this->isCamelCaps($name)) {
if (!defined('CODING_STYLE') or (defined('CODING_STYLE') and CODING_STYLE === true)) {
throw new LoaderException(
'Camel Sensitive is on. Can not use ' . $type . ' ' . $name . ' try to use StudlyCaps or CamelCase'
);
}
}
if (!in_array($type, (['Model', 'View']))) {
return false;
}
if (!empty($namespace)) {
$name = '\\' . $namespace . '\\' . $type . '\\' . $name;
} else {
$name = $namespace . '\\' . $type . '\\' . $name . $type;
}
$name = str_replace(DIRECTORY_SEPARATOR, $this->namespaceSeparator, $name);
$name = str_replace('/', $this->namespaceSeparator, $name);
$ob = new $name($this->baseClass);
if (method_exists($ob, 'start')) {
$ob->start();
}
if (method_exists($ob, 'init')) {
$ob->init();
}
} catch (LoaderException $e) {
if (ini_get('display_errors') === "1") {
$msg = null;
$msg .= '<pre>';
$msg .= 'Message: <b>' . $e->getMessage() . '</b><br><br>';
$msg .= 'Accept: ' . $_SERVER['HTTP_ACCEPT'] . '<br>';
if (isset($_SERVER['HTTP_REFERER'])) {
$msg .= 'Referer: ' . $_SERVER['HTTP_REFERER'] . '<br><br>';
}
$msg .= 'Request Method: ' . $_SERVER['REQUEST_METHOD'] . '<br><br>';
$msg .= 'Current file Path: <b>' . $this->router->currentPath() . '</b><br>';
$msg .= 'File Exception: ' . $e->getFile() . ':' . $e->getLine() . '<br><br>';
$msg .= 'Trace: <br>' . $e->getTraceAsString() . '<br>';
$msg .= '</pre>';
return Response::create($msg)->display();
}
$routes = Config::load('router')->get('routes');
if (!empty($routes['error/:code'])) {
return Response::redirect('error/:code?code=400', 400)->display();
}
return Response::create()->status(500)->display();
}
return $ob;
}
/**
* @param string $string
*
* @return bool
*/
public static function isCamelCaps($string): bool
{
preg_match_all('/^(?\'isCamelCaps\'(([A-Z]|([\\\\|\/\/][A-Z]))[a-zA-Z]+)+)$/', $string, $matches);
return !empty($matches['isCamelCaps']);
}
/**
* Method init that works like __construct called at the beginning of the code.
*/
public function init()
{
}
/**
* View Loader
*
* @param string $name
* @param null|string $namespace
*
* @return mixed
*/
public function loadView($name, $namespace = null)
{
return $this->loadObject($name, 'View', $namespace);
}
/**
* Establish the requested controller as an object.
*
* @param string $controller
* @param null|string $namespace
*
* @return mixed
*/
public function loadController($controller, $namespace = null)
{
try {
$subController = null;
if (strstr($controller, ',') !== false) {
$url = explode(',', $controller);
$urlCount = count($url) - 1;
$subController = '';
for ($i = 0; $i < $urlCount; $i++) {
$subController .= (!defined('CODING_STYLE') or
(defined('CODING_STYLE') and CODING_STYLE === true)) ?
($url[$i]) . DIRECTORY_SEPARATOR : $url[$i] . DIRECTORY_SEPARATOR;
}
$controller = $url[$urlCount];
}
if (!defined('CODING_STYLE') or (defined('CODING_STYLE') and CODING_STYLE === true)) {
$controller = ucfirst($controller);
}
$controller = str_replace(DIRECTORY_SEPARATOR, $this->namespaceSeparator, $controller);
if (!empty($namespace) && $namespace == '\\') {
$load = $controller;
} elseif (!empty($namespace)) {
$class = '\\' . $namespace . '\\Controller\\' . $subController . $controller;
$load = str_replace('/', $this->namespaceSeparator, $class);
} else {
$load = $this->namespaceSeparator . 'Controller' . $this->namespaceSeparator . $subController . $controller . 'Controller';
$load = str_replace(DIRECTORY_SEPARATOR, $this->namespaceSeparator, $load);
}
if (isset($this->debug)) {
$this->debug->addHeader(['X-DF-Debug-Controller' => $load]);
}
$controller = new $load($this->baseClass);
} catch (Exception $e) {
return $this->processLoadControllerException($e);
}
return $controller;
}
/**
* @param $e Exception
*
* @return string|null
* @throws Exception
*/
protected function processLoadControllerException($e): ?string
{
if (ini_get('display_errors') === "1") {
if (PHP_SAPI === 'cli') {
throw new Exception($e->getMessage());
} else {
$msg = '<pre>';
$msg .= 'Message: <b>' . $e->getMessage() . '</b><br><br>';
$msg .= 'Accept: ' . $_SERVER['HTTP_ACCEPT'] . '<br>';
if (isset($_SERVER['HTTP_REFERER'])) {
$msg .= 'Referer: ' . $_SERVER['HTTP_REFERER'] . '<br><br>';
}
$msg .= 'Request Method: ' . $_SERVER['REQUEST_METHOD'] . '<br><br>';
$msg .= 'Current file Path: <b>' . $this->router->currentPath() . '</b><br>';
$msg .= 'File Exception: ' . $e->getFile() . ':' . $e->getLine() . '<br><br>';
$msg .= 'Trace: <br>' . $e->getTraceAsString() . '<br>';
$msg .= '</pre>';
return Response::create($msg)->display();
}
}
$routes = Config::load('router')->get('routes');
if (!empty($routes['error/:code'])) {
return Response::redirect('error/:code?code=400', 400)->display();
}
return Response::create()->status(500)->display();
}
/**
* A method that works like __destruct called at the end of the code.
*/
public function end()
{
}
}