-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArikaim.php
379 lines (336 loc) · 10.6 KB
/
Arikaim.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core;
use Arikaim\Core\Framework\Router\ArikaimRouter;
use Arikaim\Core\App\AppContainer;
use Arikaim\Core\Http\Session;
use Arikaim\Core\Utils\Path;
use Arikaim\Core\Framework\Application;
use ErrorException;
/**
* Arikaim core class
*/
class Arikaim
{
/**
* Url scheme
*
* @var string|null
*/
protected static $scheme;
/**
* Application object
*
* @var object
*/
public static $app;
/**
* Get container service
*
* @param string $name Service name
* @param array $arguments Service params
* @return mixed service|null
*/
public static function __callStatic($name, $arguments)
{
return Self::get($name);
}
/**
* Get item from app container
*
* @param string $name
* @return mixed
*/
public static function get(string $name)
{
return $GLOBALS['container']->get($name);
}
/**
* Get item from service container
*
* @param string $name
* @return mixed
*/
public static function getService(string $name)
{
return $GLOBALS['container']->get('service')->get($name);
}
/**
* Check item exists in container
*
* @param string $name Item name.
* @return boolean
*/
public static function has(string $name): bool
{
return $GLOBALS['container']->has($name);
}
/**
* Return service container object.
*
* @return ContainerInterface
*/
public static function getContainer()
{
return $GLOBALS['container'];
}
/**
* System init
*
* @param bool $showErrors
* @param bool $console
* @param array|null $config
* @return void
*/
public static function systemInit(bool $showErrors = false, bool $console = false, ?array $config = null): void
{
// scheme
Self::$scheme = (
(isset($_SERVER['HTTPS']) == true && $_SERVER['HTTPS'] !== 'off') ||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https') ||
((isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') == true)
) ? 'https' : 'http';
\ini_set('display_errors',(int)$showErrors);
\ini_set('display_startup_errors',(int)$showErrors);
\error_reporting(($showErrors == true) ? E_ALL : 0);
// Init constants
(\defined('ROOT_PATH') == false) ? \define('ROOT_PATH',Self::getRootPath($console)) : null;
\define('HOST',($config['environment']['host'] ?? Self::resolveHost($_SERVER)));
\define('DOMAIN',Self::$scheme . '://' . HOST);
\define('SUB_DOMAIN',\explode('.',HOST)[0]);
if ($console == false) {
\define('BASE_PATH',$config['environment']['basePath'] ?? Self::resolveBasePath($_SERVER));
} else {
\define('BASE_PATH','');
}
\define('APP_PATH',ROOT_PATH . BASE_PATH . DIRECTORY_SEPARATOR . 'arikaim');
\define('APP_URL',DOMAIN . BASE_PATH . '/arikaim');
\define('CORE_NAMESPACE','Arikaim\\Core');
$loader = new \Arikaim\Core\System\ClassLoader(BASE_PATH,ROOT_PATH,'Arikaim\Core',[
'Arikaim\Extensions',
'Arikaim\Modules'
]);
$loader->register();
// load config
$config = $config ?? include (Path::CONFIG_PATH . 'config.php');
// Datetime zone
\date_default_timezone_set($config['settings']['timeZone'] ?? \date_default_timezone_get());
// Define date, time, number constants
\define('CURRENT_NUMBER_FORMAT',$config['settings']['numberFormat'] ?? null);
\define('CURRENT_DATE_FORMAT',$config['settings']['dateFormat'] ?? null);
\define('CURRENT_TIME_FORMAT',$config['settings']['timeFormat'] ?? null);
// Create app
$GLOBALS['container'] = AppContainer::create($console,$config);
$GLOBALS['container']->add('class.loader',$loader);
$GLOBALS['arikaim'] = &$GLOBALS['container'];
if ($console == true) {
// boot db
Self::get('db')->init();
return;
}
// create app
Self::$app = new Application(
$GLOBALS['arikaim'],
new ArikaimRouter(
$GLOBALS['arikaim'],
$config['settings']
),
null,
null,
$config['headers'] ?? []
);
}
/**
* Init session storage handler
*
* @param string $name
* @return void
*/
protected static function initSessionHandler(string $name): void
{
global $arikaim;
switch ($name) {
case 'predis':
// predis session storage
$handler = new \Predis\Session\Handler((new \Predis\Client()),[
'gc_maxlifetime' => 86400
]);
$handler->register();
break;
case 'database':
// db session storage
$handler = new \Arikaim\Core\System\Session\DbSessionStorage(
$arikaim->get('db')->getCapsule()->getConnection()
);
$handler->register();
break;
}
}
/**
* Create Arikaim system. Create container services, load system routes
*
* @param boolean $showErrors
* @param array|null $config
* @return void
*/
public static function init(bool $showErrors = false, ?array $config = null): void
{
Self::systemInit($showErrors,false,$config);
\set_error_handler(function($num, $message, $file, $line) {
throw new ErrorException($message,0,$num,$file,$line);
});
// add global middlewares
$middlewares = $config['middleware'] ?? Self::config()->get('middleware',[]);
Self::$app->setMiddlewares($middlewares);
// boot db
Self::get('db')->init();
// set tenant db
if (($config['tenant'] ?? false) == true) {
Self::get('service')->withService('tenant',function($service) {
$service->createTenantDbConnection();
});
}
// Session storage handler
$sessionHnadler = $config['settings']['sessionHandler'] ?? null;
if (empty($sessionHnadler) == false) {
Self::initSessionHandler($sessionHnadler);
}
// Session init
Session::start();
}
/**
* Create response object
*
* @return ResponseInterface
*/
public static function response()
{
return Self::$app->getFactory()->createResponse();
}
/**
* Start Arikaim
*
* @param bool $showErrors
* @param array|null $config
* @return void
*/
public static function run(bool $showErrors = false, ?array $config = null): void
{
Self::init($showErrors,$config);
Self::$app->run(null,$config['settings']);
}
/**
* Return error message
*
* @param string $errorCode Error code
* @param array $params Erorr params
* @param string|null $default
* @return string
*/
public static function getError(string $errorCode, array $params = [], ?string $default = 'UNKNOWN_ERROR'): string
{
return Self::errors()->getError($errorCode,$params,$default);
}
/**
* Return console root path.
*
* @return string
*/
public static function getConsoleRootPath(): string
{
return \constant('ROOT_PATH') ?? $_SERVER['PWD'];
}
/**
* Return root path.
*
* @param bool $console
* @return string
*/
public static function getRootPath(bool $console): string
{
// get root path for console run
return ($console == false) ? \rtrim(\realpath($_SERVER['DOCUMENT_ROOT']),DIRECTORY_SEPARATOR) : Self::getConsoleRootPath();
}
/**
* Return base path.
*
* @return string
*/
public static function getBasePath(): string
{
return \constant('BASE_PATH') ?? '';
}
/**
* Return domain url.
*
* @return string
*/
public static function getDomain(): string
{
return \constant('DOMAIN') ?? (Self::$scheme . '://' . Self::resolveHost($_SERVER));
}
/**
* Get host
*
* @return string
*/
public static function getHost(): string
{
return \parse_url(DOMAIN,PHP_URL_HOST);
}
/**
* Get port
*
* @param string $prefix
* @return string
*/
public static function getPort(string $prefix = ':'): string
{
return (($_SERVER['SERVER_PORT'] ?? 80) == 80) ? '' : $prefix . $_SERVER['SERVER_PORT'];
}
/**
* Return true if script is run from console.
*
* @return boolean
*/
public static function isConsole(): bool
{
return (\php_sapi_name() == 'cli');
}
/**
* Resolve site host
*
* @param array $env
* @return string
*/
public static function resolveHost(array $env): string
{
// host
if (empty($env['HTTP_HOST']) == false) {
return $env['HTTP_HOST'];
}
$host = $env['SERVER_NAME'] ?? '';
if (\preg_match('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/',$host,$matches) == false) {
return (\strpos($host,':') !== false) ? \strstr($host,':', true) : $host;
}
return $host;
}
/**
* Resolve site base path
*
* @param array $env
* @return string
*/
public static function resolveBasePath(array $env): string
{
$basePath = \dirname((string)\parse_url($env['SCRIPT_NAME'],PHP_URL_PATH));
return ($basePath == '/' || $basePath == '.') ? '' : $basePath;
}
}