-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoload.php
207 lines (168 loc) · 5.89 KB
/
autoload.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
<?php
/**
* Leave manager : Simple app for contract and leave management.
*
* @copyright Copyright (c) Silevester D. (https://github.com/SilverD3)
* @link https://github.com/SilverD3/leave-manager Leave Manager Project
* @since 1.0 (2022)
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'paths.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'functions.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
/**
* Classes autoloader
*/
class AutoLoader
{
public function __autoload($className)
{
if (strpos($className, '\\') !== false) {
$fqnParts = explode('\\', $className);
if ($fqnParts[0] === 'App') {
switch ($fqnParts[1]) {
case 'Entity':
$this->__autoLoadModel($className);
break;
case 'Controller':
$this->__autoLoadController($className);
break;
case 'Service':
$this->__autoLoadService($className);
break;
case 'View':
$this->__autoLoadView($className);
break;
default:
$this->__autoloadClass($className);
break;
}
} elseif ($fqnParts[0] === 'Core') {
$this->__autoLoadCoreClass($className);
} else {
$this->__autoloadClass($className);
}
} else {
$this->__autoloadClass($className);
}
}
public function __autoloadClass($className)
{
$filePath = $className . '.php';
if (strpos($className, '\\') !== false) {
$fqnParts = explode('\\', $className);
$filePath = implode(DS, $fqnParts) . '.php';
}
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load class "%s" . Class file not found', $className));
}
}
public function __autoLoadCoreClass($className)
{
$fqnParts = explode('\\', $className);
unset($fqnParts[0]);
$filePath = CORE_PATH . implode(DS, $fqnParts) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load Core class "%s".', $className));
}
}
public function __autoLoadModel($className)
{
$fqnParts = explode('\\', $className);
unset($fqnParts[0]);
unset($fqnParts[1]);
$filePath = MODEL_PATH . implode(DS, $fqnParts) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load Model "%s" . Model class file not found', $className));
}
}
public function __autoLoadController($className)
{
$fqnParts = explode('\\', $className);
unset($fqnParts[0]);
unset($fqnParts[1]);
$filePath = CONTROLLER_PATH . implode(DS, $fqnParts) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load Controller "%s" . Controller class file not found', $className));
}
}
public function __autoLoadService($className)
{
$fqnParts = explode('\\', $className);
unset($fqnParts[0]);
unset($fqnParts[1]);
$filePath = SERVICE_PATH . implode(DS, $fqnParts) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load Service "%s" . Service class file not found', $className));
}
}
public function __autoLoadView($className)
{
$fqnParts = explode('\\', $className);
unset($fqnParts[0]);
unset($fqnParts[1]);
$filePath = VIEW_PATH . implode(DS, $fqnParts) . '.php';
if (file_exists($filePath)) {
require_once $filePath;
} else {
throw new \Exception(sprintf('Could not load View class "%s".', $className));
}
}
/**
* Register all autoloaders
*
* @return void
*/
public function register()
{
try {
spl_autoload_register(array($this, "__autoload"), true);
} catch (\Exception $e) {
$e->getMessage();
}
}
}
// Register autoload
(new AutoLoader())->register();
use Core\Configure;
// Load .env files
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$configure = new Configure();
// Debugging and warnings
$debugOptions = $configure->read('Debug', ['enable' => true]);
if ($debugOptions['enable'] === true) {
ini_set('xdebug.remote_enable', true);
ini_set('xdebug.idebug.remote_host', 'localhost');
ini_set('xdebug.idebug.remote_port', 9003);
} else {
error_reporting(E_ALL & ~E_DEPRECATED);
}
// Start session
if (session_status() === PHP_SESSION_NONE) {
$default_session_cookie_params = [
'timeout' => 60 * 60 * 24 * 2, // 2 days
'path' => '/',
'domain' => getFullDomainUrl(),
'secure' => true,
'httponly' => true
];
$session_cookie_params = array_merge($configure->read('Session', $default_session_cookie_params), $default_session_cookie_params);
ini_set('session.gc_maxlifetime', $session_cookie_params['timeout']);
ini_set('session.cookie_lifetime', $session_cookie_params['timeout']);
//session_set_cookie_params(...array_values($session_cookie_params));
session_start();
$_SESSION['__configure__'] = serialize($configure);
}