• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12704370970

10 Jan 2025 06:18AM UTC coverage: 84.454%. Remained the same
12704370970

Pull #9395

github

web-flow
Merge f916c0a32 into 708fb6d70
Pull Request #9395: chore: add more trailing commas in more places

337 of 397 new or added lines in 117 files covered. (84.89%)

1 existing line in 1 file now uncovered.

20464 of 24231 relevant lines covered (84.45%)

189.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/system/Boot.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
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\Cache\FactoriesCache;
17
use CodeIgniter\CLI\Console;
18
use CodeIgniter\Config\DotEnv;
19
use Config\Autoload;
20
use Config\Modules;
21
use Config\Optimize;
22
use Config\Paths;
23
use Config\Services;
24

25
/**
26
 * Bootstrap for the application
27
 *
28
 * @codeCoverageIgnore
29
 */
30
class Boot
31
{
32
    /**
33
     * Used by `public/index.php`
34
     *
35
     * Context
36
     *   web:     Invoked by HTTP request
37
     *   php-cli: Invoked by CLI via `php public/index.php`
38
     *
39
     * @return int Exit code.
40
     */
41
    public static function bootWeb(Paths $paths): int
42
    {
43
        static::definePathConstants($paths);
×
44
        if (! defined('APP_NAMESPACE')) {
×
45
            static::loadConstants();
×
46
        }
47
        static::checkMissingExtensions();
×
48

49
        static::loadDotEnv($paths);
×
50
        static::defineEnvironment();
×
51
        static::loadEnvironmentBootstrap($paths);
×
52

53
        static::loadCommonFunctions();
×
54
        static::loadAutoloader();
×
55
        static::setExceptionHandler();
×
56
        static::initializeKint();
×
57

58
        $configCacheEnabled = class_exists(Optimize::class)
×
59
            && (new Optimize())->configCacheEnabled;
×
60
        if ($configCacheEnabled) {
×
61
            $factoriesCache = static::loadConfigCache();
×
62
        }
63

64
        static::autoloadHelpers();
×
65

66
        $app = static::initializeCodeIgniter();
×
67
        static::runCodeIgniter($app);
×
68

69
        if ($configCacheEnabled) {
×
70
            static::saveConfigCache($factoriesCache);
×
71
        }
72

73
        // Exits the application, setting the exit code for CLI-based
74
        // applications that might be watching.
75
        return EXIT_SUCCESS;
×
76
    }
77

78
    /**
79
     * Used by `spark`
80
     *
81
     * @return int Exit code.
82
     */
83
    public static function bootSpark(Paths $paths): int
84
    {
85
        static::definePathConstants($paths);
×
86
        if (! defined('APP_NAMESPACE')) {
×
87
            static::loadConstants();
×
88
        }
89
        static::checkMissingExtensions();
×
90

91
        static::loadDotEnv($paths);
×
92
        static::defineEnvironment();
×
93
        static::loadEnvironmentBootstrap($paths);
×
94

95
        static::loadCommonFunctions();
×
96
        static::loadAutoloader();
×
97
        static::setExceptionHandler();
×
98
        static::initializeKint();
×
99
        static::autoloadHelpers();
×
100

101
        static::initializeCodeIgniter();
×
102
        $console = static::initializeConsole();
×
103

104
        return static::runCommand($console);
×
105
    }
106

107
    /**
108
     * Used by `system/Test/bootstrap.php`
109
     */
110
    public static function bootTest(Paths $paths): void
111
    {
112
        static::loadConstants();
×
113
        static::checkMissingExtensions();
×
114

115
        static::loadDotEnv($paths);
×
116
        static::loadEnvironmentBootstrap($paths, false);
×
117

118
        static::loadCommonFunctions();
×
119
        static::loadAutoloader();
×
120
        static::setExceptionHandler();
×
121
        static::initializeKint();
×
122
        static::autoloadHelpers();
×
123
    }
124

125
    /**
126
     * Used by `preload.php`
127
     */
128
    public static function preload(Paths $paths): void
129
    {
130
        static::definePathConstants($paths);
×
131
        static::loadConstants();
×
132
        static::defineEnvironment();
×
133
        static::loadEnvironmentBootstrap($paths, false);
×
134

135
        static::loadAutoloader();
×
136
    }
137

138
    /**
139
     * Load environment settings from .env files into $_SERVER and $_ENV
140
     */
141
    protected static function loadDotEnv(Paths $paths): void
142
    {
143
        require_once $paths->systemDirectory . '/Config/DotEnv.php';
×
144
        (new DotEnv($paths->appDirectory . '/../'))->load();
×
145
    }
146

147
    protected static function defineEnvironment(): void
148
    {
149
        if (! defined('ENVIRONMENT')) {
×
150
            // @phpstan-ignore-next-line
151
            $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT']
×
152
                ?? getenv('CI_ENVIRONMENT')
×
153
                ?: 'production';
×
154

155
            define('ENVIRONMENT', $env);
×
156
        }
157
    }
158

159
    protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = true): void
160
    {
161
        if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
×
162
            require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
×
163

164
            return;
×
165
        }
166

167
        if ($exit) {
×
168
            header('HTTP/1.1 503 Service Unavailable.', true, 503);
×
169
            echo 'The application environment is not set correctly.';
×
170

171
            exit(EXIT_ERROR);
×
172
        }
173
    }
174

175
    /**
176
     * The path constants provide convenient access to the folders throughout
177
     * the application. We have to set them up here, so they are available in
178
     * the config files that are loaded.
179
     */
180
    protected static function definePathConstants(Paths $paths): void
181
    {
182
        // The path to the application directory.
183
        if (! defined('APPPATH')) {
×
184
            define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
×
185
        }
186

187
        // The path to the project root directory. Just above APPPATH.
188
        if (! defined('ROOTPATH')) {
×
189
            define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR);
×
190
        }
191

192
        // The path to the system directory.
193
        if (! defined('SYSTEMPATH')) {
×
194
            define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
×
195
        }
196

197
        // The path to the writable directory.
198
        if (! defined('WRITEPATH')) {
×
199
            $writePath = realpath(rtrim($paths->writableDirectory, '\\/ '));
×
200

201
            if ($writePath === false) {
×
202
                header('HTTP/1.1 503 Service Unavailable.', true, 503);
×
203
                echo 'The WRITEPATH is not set correctly.';
×
204

205
                // EXIT_ERROR is not yet defined
206
                exit(1);
×
207
            }
208
            define('WRITEPATH', $writePath . DIRECTORY_SEPARATOR);
×
209
        }
210

211
        // The path to the tests directory
212
        if (! defined('TESTPATH')) {
×
213
            define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR);
×
214
        }
215
    }
216

217
    protected static function loadConstants(): void
218
    {
219
        require_once APPPATH . 'Config/Constants.php';
×
220
    }
221

222
    protected static function loadCommonFunctions(): void
223
    {
224
        // Require app/Common.php file if exists.
225
        if (is_file(APPPATH . 'Common.php')) {
×
226
            require_once APPPATH . 'Common.php';
×
227
        }
228

229
        // Require system/Common.php
230
        require_once SYSTEMPATH . 'Common.php';
×
231
    }
232

233
    /**
234
     * The autoloader allows all the pieces to work together in the framework.
235
     * We have to load it here, though, so that the config files can use the
236
     * path constants.
237
     */
238
    protected static function loadAutoloader(): void
239
    {
240
        if (! class_exists(Autoload::class, false)) {
×
241
            require_once SYSTEMPATH . 'Config/AutoloadConfig.php';
×
242
            require_once APPPATH . 'Config/Autoload.php';
×
243
            require_once SYSTEMPATH . 'Modules/Modules.php';
×
244
            require_once APPPATH . 'Config/Modules.php';
×
245
        }
246

247
        require_once SYSTEMPATH . 'Autoloader/Autoloader.php';
×
248
        require_once SYSTEMPATH . 'Config/BaseService.php';
×
249
        require_once SYSTEMPATH . 'Config/Services.php';
×
250
        require_once APPPATH . 'Config/Services.php';
×
251

252
        // Initialize and register the loader with the SPL autoloader stack.
253
        Services::autoloader()->initialize(new Autoload(), new Modules())->register();
×
254
    }
255

256
    protected static function autoloadHelpers(): void
257
    {
258
        service('autoloader')->loadHelpers();
×
259
    }
260

261
    protected static function setExceptionHandler(): void
262
    {
263
        service('exceptions')->initialize();
×
264
    }
265

266
    protected static function checkMissingExtensions(): void
267
    {
268
        if (is_file(COMPOSER_PATH)) {
×
269
            return;
×
270
        }
271

272
        // Run this check for manual installations
273
        $missingExtensions = [];
×
274

275
        foreach ([
×
276
            'intl',
×
277
            'json',
×
278
            'mbstring',
×
279
        ] as $extension) {
×
280
            if (! extension_loaded($extension)) {
×
281
                $missingExtensions[] = $extension;
×
282
            }
283
        }
284

285
        if ($missingExtensions === []) {
×
286
            return;
×
287
        }
288

289
        $message = sprintf(
×
290
            'The framework needs the following extension(s) installed and loaded: %s.',
×
NEW
291
            implode(', ', $missingExtensions)
×
292
        );
×
293

294
        header('HTTP/1.1 503 Service Unavailable.', true, 503);
×
295
        echo $message;
×
296

297
        exit(EXIT_ERROR);
×
298
    }
299

300
    protected static function initializeKint(): void
301
    {
302
        service('autoloader')->initializeKint(CI_DEBUG);
×
303
    }
304

305
    protected static function loadConfigCache(): FactoriesCache
306
    {
307
        $factoriesCache = new FactoriesCache();
×
308
        $factoriesCache->load('config');
×
309

310
        return $factoriesCache;
×
311
    }
312

313
    /**
314
     * The CodeIgniter class contains the core functionality to make
315
     * the application run, and does all the dirty work to get
316
     * the pieces all working together.
317
     */
318
    protected static function initializeCodeIgniter(): CodeIgniter
319
    {
320
        $app = service('codeigniter');
×
321
        $app->initialize();
×
322
        $context = is_cli() ? 'php-cli' : 'web';
×
323
        $app->setContext($context);
×
324

325
        return $app;
×
326
    }
327

328
    /**
329
     * Now that everything is set up, it's time to actually fire
330
     * up the engines and make this app do its thang.
331
     */
332
    protected static function runCodeIgniter(CodeIgniter $app): void
333
    {
334
        $app->run();
×
335
    }
336

337
    protected static function saveConfigCache(FactoriesCache $factoriesCache): void
338
    {
339
        $factoriesCache->save('config');
×
340
    }
341

342
    protected static function initializeConsole(): Console
343
    {
344
        $console = new Console();
×
345

346
        // Show basic information before we do anything else.
347
        // @phpstan-ignore-next-line
348
        if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
×
349
            unset($_SERVER['argv'][$suppress]); // @phpstan-ignore-line
×
350
            $suppress = true;
×
351
        }
352

353
        $console->showHeader($suppress);
×
354

355
        return $console;
×
356
    }
357

358
    protected static function runCommand(Console $console): int
359
    {
360
        $exit = $console->run();
×
361

362
        return is_int($exit) ? $exit : EXIT_SUCCESS;
×
363
    }
364
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc