|
| 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 | +} |
0 commit comments