Skip to content

Commit 39210be

Browse files
authored
Merge pull request #8604 from kenjis/feat-add-Boot-class
feat: add Boot class
2 parents 020da9b + 842fd6e commit 39210be

File tree

7 files changed

+276
-132
lines changed

7 files changed

+276
-132
lines changed

public/index.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
PHP_VERSION
1515
);
1616

17-
exit($message);
17+
header('HTTP/1.1 503 Service Unavailable.', true, 503);
18+
echo $message;
19+
20+
exit(1);
1821
}
1922

2023
/*
@@ -47,30 +50,9 @@
4750

4851
$paths = new Config\Paths();
4952

50-
// LOAD DOTENV FILE
51-
// Load environment settings from .env files into $_SERVER and $_ENV
52-
require_once $paths->systemDirectory . '/Config/DotEnv.php';
53-
(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load();
54-
55-
// DEFINE ENVIRONMENT
56-
if (! defined('ENVIRONMENT')) {
57-
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT');
58-
define('ENVIRONMENT', ($env !== false) ? $env : 'production');
59-
unset($env);
60-
}
61-
62-
// LOAD ENVIRONMENT BOOTSTRAP
63-
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
64-
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
65-
} else {
66-
header('HTTP/1.1 503 Service Unavailable.', true, 503);
67-
echo 'The application environment is not set correctly.';
68-
69-
exit(EXIT_ERROR); // EXIT_ERROR
70-
}
71-
7253
// LOAD THE FRAMEWORK BOOTSTRAP FILE
73-
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
54+
require $paths->systemDirectory . '/Boot.php';
55+
CodeIgniter\Boot::BootWeb($paths);
7456

7557
// Load Config Cache
7658
// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();

spark

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
* --------------------------------------------------------------------
1717
* The main entry point into the CLI system and allows you to run
1818
* commands and perform maintenance on your application.
19-
*
20-
* Because CodeIgniter can handle CLI requests as just another web request
21-
* this class mainly acts as a passthru to the framework itself.
19+
*/
20+
21+
/*
22+
*---------------------------------------------------------------
23+
* CHECK SERVER API
24+
*---------------------------------------------------------------
2225
*/
2326

2427
// Refuse to run when called from php-cgi
@@ -75,30 +78,9 @@ require FCPATH . '../app/Config/Paths.php';
7578

7679
$paths = new Config\Paths();
7780

78-
// LOAD DOTENV FILE
79-
// Load environment settings from .env files into $_SERVER and $_ENV
80-
require_once $paths->systemDirectory . '/Config/DotEnv.php';
81-
(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load();
82-
83-
// DEFINE ENVIRONMENT
84-
if (! defined('ENVIRONMENT')) {
85-
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT');
86-
define('ENVIRONMENT', ($env !== false) ? $env : 'production');
87-
unset($env);
88-
}
89-
90-
// LOAD ENVIRONMENT BOOTSTRAP
91-
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
92-
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
93-
} else {
94-
header('HTTP/1.1 503 Service Unavailable.', true, 503);
95-
echo 'The application environment is not set correctly.';
96-
97-
exit(EXIT_ERROR); // EXIT_ERROR
98-
}
99-
10081
// LOAD THE FRAMEWORK BOOTSTRAP FILE
101-
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
82+
require $paths->systemDirectory . '/Boot.php';
83+
CodeIgniter\Boot::BootSpark($paths);
10284

10385
/*
10486
* ---------------------------------------------------------------

system/Boot.php

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter;
15+
16+
use CodeIgniter\Config\DotEnv;
17+
use Config\Autoload;
18+
use Config\Modules;
19+
use Config\Paths;
20+
use Config\Services;
21+
22+
/**
23+
* Bootstrap for the application
24+
*
25+
* @codeCoverageIgnore
26+
*/
27+
class Boot
28+
{
29+
/**
30+
* Used by `public/index.php`
31+
*
32+
* Context
33+
* web: Invoked by HTTP request
34+
* php-cli: Invoked by CLI via `php public/index.php`
35+
*/
36+
public static function bootWeb(Paths $paths): void
37+
{
38+
static::boot($paths);
39+
}
40+
41+
/**
42+
* Used by `spark`
43+
*/
44+
public static function bootSpark(Paths $paths): void
45+
{
46+
static::boot($paths);
47+
}
48+
49+
protected static function boot(Paths $paths): void
50+
{
51+
static::definePathConstants($paths);
52+
if (! defined('APP_NAMESPACE')) {
53+
static::loadConstants();
54+
}
55+
static::checkMissingExtensions();
56+
57+
static::loadDotEnv($paths);
58+
static::defineEnvironment();
59+
static::loadEnvironmentBootstrap($paths);
60+
61+
static::loadCommonFunctions();
62+
static::loadAutoloader();
63+
static::setExceptionHandler();
64+
static::initializeKint();
65+
}
66+
67+
/**
68+
* Used by `system/Test/bootstrap.php`
69+
*/
70+
public static function bootTest(Paths $paths): void
71+
{
72+
static::loadConstants();
73+
static::checkMissingExtensions();
74+
75+
static::loadDotEnv($paths);
76+
static::loadEnvironmentBootstrap($paths, false);
77+
78+
static::loadCommonFunctions();
79+
static::loadAutoloader();
80+
static::setExceptionHandler();
81+
static::initializeKint();
82+
}
83+
84+
/**
85+
* Load environment settings from .env files into $_SERVER and $_ENV
86+
*/
87+
protected static function loadDotEnv(Paths $paths): void
88+
{
89+
require_once $paths->systemDirectory . '/Config/DotEnv.php';
90+
(new DotEnv($paths->appDirectory . '/../'))->load();
91+
}
92+
93+
protected static function defineEnvironment(): void
94+
{
95+
if (! defined('ENVIRONMENT')) {
96+
// @phpstan-ignore-next-line
97+
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT']
98+
?? getenv('CI_ENVIRONMENT')
99+
?: 'production';
100+
101+
define('ENVIRONMENT', $env);
102+
}
103+
}
104+
105+
protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = true): void
106+
{
107+
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
108+
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
109+
110+
return;
111+
}
112+
113+
if ($exit) {
114+
header('HTTP/1.1 503 Service Unavailable.', true, 503);
115+
echo 'The application environment is not set correctly.';
116+
117+
exit(EXIT_ERROR);
118+
}
119+
}
120+
121+
/**
122+
* The path constants provide convenient access to the folders throughout
123+
* the application. We have to set them up here, so they are available in
124+
* the config files that are loaded.
125+
*/
126+
protected static function definePathConstants(Paths $paths): void
127+
{
128+
// The path to the application directory.
129+
if (! defined('APPPATH')) {
130+
define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
131+
}
132+
133+
// The path to the project root directory. Just above APPPATH.
134+
if (! defined('ROOTPATH')) {
135+
define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR);
136+
}
137+
138+
// The path to the system directory.
139+
if (! defined('SYSTEMPATH')) {
140+
define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
141+
}
142+
143+
// The path to the writable directory.
144+
if (! defined('WRITEPATH')) {
145+
define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
146+
}
147+
148+
// The path to the tests directory
149+
if (! defined('TESTPATH')) {
150+
define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
151+
}
152+
}
153+
154+
protected static function loadConstants(): void
155+
{
156+
require_once APPPATH . 'Config/Constants.php';
157+
}
158+
159+
protected static function loadCommonFunctions(): void
160+
{
161+
// Require app/Common.php file if exists.
162+
if (is_file(APPPATH . 'Common.php')) {
163+
require_once APPPATH . 'Common.php';
164+
}
165+
166+
// Require system/Common.php
167+
require_once SYSTEMPATH . 'Common.php';
168+
}
169+
170+
/**
171+
* The autoloader allows all the pieces to work together in the framework.
172+
* We have to load it here, though, so that the config files can use the
173+
* path constants.
174+
*/
175+
protected static function loadAutoloader(): void
176+
{
177+
if (! class_exists(Autoload::class, false)) {
178+
require_once SYSTEMPATH . 'Config/AutoloadConfig.php';
179+
require_once APPPATH . 'Config/Autoload.php';
180+
require_once SYSTEMPATH . 'Modules/Modules.php';
181+
require_once APPPATH . 'Config/Modules.php';
182+
}
183+
184+
require_once SYSTEMPATH . 'Autoloader/Autoloader.php';
185+
require_once SYSTEMPATH . 'Config/BaseService.php';
186+
require_once SYSTEMPATH . 'Config/Services.php';
187+
require_once APPPATH . 'Config/Services.php';
188+
189+
// Initialize and register the loader with the SPL autoloader stack.
190+
Services::autoloader()->initialize(new Autoload(), new Modules())->register();
191+
Services::autoloader()->loadHelpers();
192+
}
193+
194+
protected static function setExceptionHandler(): void
195+
{
196+
Services::exceptions()->initialize();
197+
}
198+
199+
protected static function checkMissingExtensions(): void
200+
{
201+
if (is_file(COMPOSER_PATH)) {
202+
return;
203+
}
204+
205+
// Run this check for manual installations
206+
$missingExtensions = [];
207+
208+
foreach ([
209+
'intl',
210+
'json',
211+
'mbstring',
212+
] as $extension) {
213+
if (! extension_loaded($extension)) {
214+
$missingExtensions[] = $extension;
215+
}
216+
}
217+
218+
if ($missingExtensions === []) {
219+
return;
220+
}
221+
222+
$message = sprintf(
223+
'The framework needs the following extension(s) installed and loaded: %s.',
224+
implode(', ', $missingExtensions)
225+
);
226+
227+
header('HTTP/1.1 503 Service Unavailable.', true, 503);
228+
echo $message;
229+
230+
exit(EXIT_ERROR);
231+
}
232+
233+
protected static function initializeKint(): void
234+
{
235+
Services::autoloader()->initializeKint(CI_DEBUG);
236+
}
237+
}

system/Exceptions/FrameworkException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public static function forCopyError(string $path)
5959

6060
/**
6161
* @return static
62+
*
63+
* @deprecated 4.5.0 No longer used.
6264
*/
6365
public static function forMissingExtension(string $extension)
6466
{

0 commit comments

Comments
 (0)