-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBoot.php
355 lines (293 loc) · 9.9 KB
/
Boot.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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter;
use CodeIgniter\Cache\FactoriesCache;
use CodeIgniter\CLI\Console;
use CodeIgniter\Config\DotEnv;
use Config\Autoload;
use Config\Modules;
use Config\Optimize;
use Config\Paths;
use Config\Services;
/**
* Bootstrap for the application
*
* @codeCoverageIgnore
*/
class Boot
{
/**
* Used by `public/index.php`
*
* Context
* web: Invoked by HTTP request
* php-cli: Invoked by CLI via `php public/index.php`
*
* @return int Exit code.
*/
public static function bootWeb(Paths $paths): int
{
static::definePathConstants($paths);
if (! defined('APP_NAMESPACE')) {
static::loadConstants();
}
static::checkMissingExtensions();
static::loadDotEnv($paths);
static::defineEnvironment();
static::loadEnvironmentBootstrap($paths);
static::loadCommonFunctions();
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
$configCacheEnabled = class_exists(Optimize::class)
&& (new Optimize())->configCacheEnabled;
if ($configCacheEnabled) {
$factoriesCache = static::loadConfigCache();
}
static::autoloadHelpers();
$app = static::initializeCodeIgniter();
static::runCodeIgniter($app);
if ($configCacheEnabled) {
static::saveConfigCache($factoriesCache);
}
// Exits the application, setting the exit code for CLI-based
// applications that might be watching.
return EXIT_SUCCESS;
}
/**
* Used by `spark`
*
* @return int Exit code.
*/
public static function bootSpark(Paths $paths): int
{
static::definePathConstants($paths);
if (! defined('APP_NAMESPACE')) {
static::loadConstants();
}
static::checkMissingExtensions();
static::loadDotEnv($paths);
static::defineEnvironment();
static::loadEnvironmentBootstrap($paths);
static::loadCommonFunctions();
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
static::autoloadHelpers();
static::initializeCodeIgniter();
$console = static::initializeConsole();
return static::runCommand($console);
}
/**
* Used by `system/Test/bootstrap.php`
*/
public static function bootTest(Paths $paths): void
{
static::loadConstants();
static::checkMissingExtensions();
static::loadDotEnv($paths);
static::loadEnvironmentBootstrap($paths, false);
static::loadCommonFunctions();
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
static::autoloadHelpers();
}
/**
* Used by `preload.php`
*/
public static function preload(Paths $paths): void
{
static::definePathConstants($paths);
static::loadConstants();
static::defineEnvironment();
static::loadEnvironmentBootstrap($paths, false);
static::loadAutoloader();
}
/**
* Load environment settings from .env files into $_SERVER and $_ENV
*/
protected static function loadDotEnv(Paths $paths): void
{
require_once $paths->systemDirectory . '/Config/DotEnv.php';
(new DotEnv($paths->appDirectory . '/../'))->load();
}
protected static function defineEnvironment(): void
{
if (! defined('ENVIRONMENT')) {
// @phpstan-ignore-next-line
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT']
?? getenv('CI_ENVIRONMENT')
?: 'production';
define('ENVIRONMENT', $env);
}
}
protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = true): void
{
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
return;
}
if ($exit) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'The application environment is not set correctly.';
exit(EXIT_ERROR);
}
}
/**
* The path constants provide convenient access to the folders throughout
* the application. We have to set them up here, so they are available in
* the config files that are loaded.
*/
protected static function definePathConstants(Paths $paths): void
{
// The path to the application directory.
if (! defined('APPPATH')) {
define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
}
// The path to the project root directory. Just above APPPATH.
if (! defined('ROOTPATH')) {
define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR);
}
// The path to the system directory.
if (! defined('SYSTEMPATH')) {
define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
}
// The path to the writable directory.
if (! defined('WRITEPATH')) {
define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
}
// The path to the tests directory
if (! defined('TESTPATH')) {
define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
}
}
protected static function loadConstants(): void
{
require_once APPPATH . 'Config/Constants.php';
}
protected static function loadCommonFunctions(): void
{
// Require app/Common.php file if exists.
if (is_file(APPPATH . 'Common.php')) {
require_once APPPATH . 'Common.php';
}
// Require system/Common.php
require_once SYSTEMPATH . 'Common.php';
}
/**
* The autoloader allows all the pieces to work together in the framework.
* We have to load it here, though, so that the config files can use the
* path constants.
*/
protected static function loadAutoloader(): void
{
if (! class_exists(Autoload::class, false)) {
require_once SYSTEMPATH . 'Config/AutoloadConfig.php';
require_once APPPATH . 'Config/Autoload.php';
require_once SYSTEMPATH . 'Modules/Modules.php';
require_once APPPATH . 'Config/Modules.php';
}
require_once SYSTEMPATH . 'Autoloader/Autoloader.php';
require_once SYSTEMPATH . 'Config/BaseService.php';
require_once SYSTEMPATH . 'Config/Services.php';
require_once APPPATH . 'Config/Services.php';
// Initialize and register the loader with the SPL autoloader stack.
Services::autoloader()->initialize(new Autoload(), new Modules())->register();
}
protected static function autoloadHelpers(): void
{
service('autoloader')->loadHelpers();
}
protected static function setExceptionHandler(): void
{
service('exceptions')->initialize();
}
protected static function checkMissingExtensions(): void
{
if (is_file(COMPOSER_PATH)) {
return;
}
// Run this check for manual installations
$missingExtensions = [];
foreach ([
'intl',
'json',
'mbstring',
] as $extension) {
if (! extension_loaded($extension)) {
$missingExtensions[] = $extension;
}
}
if ($missingExtensions === []) {
return;
}
$message = sprintf(
'The framework needs the following extension(s) installed and loaded: %s.',
implode(', ', $missingExtensions)
);
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo $message;
exit(EXIT_ERROR);
}
protected static function initializeKint(): void
{
service('autoloader')->initializeKint(CI_DEBUG);
}
protected static function loadConfigCache(): FactoriesCache
{
$factoriesCache = new FactoriesCache();
$factoriesCache->load('config');
return $factoriesCache;
}
/**
* The CodeIgniter class contains the core functionality to make
* the application run, and does all the dirty work to get
* the pieces all working together.
*/
protected static function initializeCodeIgniter(): CodeIgniter
{
$app = service('codeigniter');
$app->initialize();
$context = is_cli() ? 'php-cli' : 'web';
$app->setContext($context);
return $app;
}
/**
* Now that everything is set up, it's time to actually fire
* up the engines and make this app do its thang.
*/
protected static function runCodeIgniter(CodeIgniter $app): void
{
$app->run();
}
protected static function saveConfigCache(FactoriesCache $factoriesCache): void
{
$factoriesCache->save('config');
}
protected static function initializeConsole(): Console
{
$console = new Console();
// Show basic information before we do anything else.
// @phpstan-ignore-next-line
if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
unset($_SERVER['argv'][$suppress]); // @phpstan-ignore-line
$suppress = true;
}
$console->showHeader($suppress);
return $console;
}
protected static function runCommand(Console $console): int
{
$exit = $console->run();
return is_int($exit) ? $exit : EXIT_SUCCESS;
}
}