-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap.php
executable file
·84 lines (68 loc) · 2.82 KB
/
bootstrap.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
<?php
if (session_status() == PHP_SESSION_NONE && php_sapi_name() !== 'cli') {
session_name('appSession');
session_start();
}
chdir(__DIR__);
require_once(__DIR__ . '/app/system/constants.php');
require_once(ROOT_PATH . '/vendor/autoload.php');
use App\Services\SystemService;
use App\Utilities\LoggerUtility;
use App\Services\DatabaseService;
use App\Registries\ContainerRegistry;
// Dependency Injection
require_once(APPLICATION_PATH . '/system/di.php');
// Global functions
require_once(APPLICATION_PATH . '/system/functions.php');
// Just putting $db and SYSTEM_CONFIG here in case there are
// some old scripts that are still depending on these.
$db = ContainerRegistry::get(DatabaseService::class);
defined('SYSTEM_CONFIG') ||
define('SYSTEM_CONFIG', ContainerRegistry::get('applicationConfig'));
set_error_handler(function ($severity, $message, $file, $line) {
$exception = new ErrorException($message, 0, $severity, $file, $line);
$trace = debug_backtrace();
// Check if debug mode is enabled
if (SYSTEM_CONFIG['system']['debug_mode'] || APPLICATION_ENV === 'development') {
// In debug mode, log all error levels but only throw exceptions for severe errors
LoggerUtility::log('error', $exception->getMessage(), [
'exception' => $exception,
'trace' => $trace
]);
if (in_array($severity, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE])) {
throw $exception;
}
} else {
// In production mode, log and throw exceptions only for severe errors
if (in_array($severity, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE])) {
LoggerUtility::log('error', $exception->getMessage(), [
'exception' => $exception,
'trace' => $trace
]);
throw $exception;
}
// Optionally, log other errors without throwing exceptions
// LoggerUtility::log('warning', $exception->getMessage(), ['exception' => $exception]);
}
});
set_exception_handler(function ($exception) {
LoggerUtility::log('error', $exception->getMessage(), [
'exception' => $exception,
'trace' => $exception->getTraceAsString()
]);
// Handle the final response for uncaught exceptions here or exit gracefully.
});
register_shutdown_function(function () {
$error = error_get_last();
if ($error && ($error['type'] === E_ERROR || $error['type'] === E_PARSE || $error['type'] === E_CORE_ERROR || $error['type'] === E_COMPILE_ERROR)) {
LoggerUtility::log('critical', $error['message'], [
'error' => $error,
'trace' => debug_backtrace()
]);
}
});
/** @var SystemService $system */
$system = ContainerRegistry::get(SystemService::class);
$system
->bootstrap()
->debug(SYSTEM_CONFIG['system']['debug_mode'] ?: false);