CodeIgniter4/system/Boot.php

234 lines
6.7 KiB
PHP
Raw Normal View History

2024-02-19 09:40:13 +09:00
<?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;
2024-02-19 09:45:40 +09:00
use CodeIgniter\Config\DotEnv;
2024-02-19 09:40:13 +09:00
use Config\Autoload;
use Config\Modules;
use Config\Paths;
use Config\Services;
/**
* Bootstrap for the application
2024-02-19 13:52:24 +09:00
*
* @codeCoverageIgnore
*/
2024-02-19 09:40:13 +09:00
class Boot
{
/**
* Used by `public/index.php`
2024-02-19 09:40:13 +09:00
*
2024-03-16 10:11:22 +09:00
* Context
2024-02-19 09:40:13 +09:00
* web: Invoked by HTTP request
* php-cli: Invoked by CLI via `php public/index.php`
*/
public static function bootWeb(Paths $paths): void
{
2024-03-17 08:46:51 +09:00
static::boot($paths);
2024-02-19 09:40:13 +09:00
}
/**
* Used by `spark`
2024-02-19 09:40:13 +09:00
*/
public static function bootSpark(Paths $paths): void
2024-03-17 08:46:51 +09:00
{
static::boot($paths);
}
protected static function boot(Paths $paths): void
2024-02-19 09:40:13 +09:00
{
2024-02-19 11:50:55 +09:00
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();
}
/**
* Used by `system/Test/bootstrap.php`
*/
public static function bootTest(Paths $paths): void
{
static::loadConstants();
static::checkMissingExtensions();
static::loadDotEnv($paths);
static::loadEnvironmentBootstrap($paths, false);
2024-02-19 09:40:13 +09:00
static::loadCommonFunctions();
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
}
2024-02-19 09:45:40 +09:00
/**
* 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
2024-03-17 08:50:47 +09:00
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT']
?? getenv('CI_ENVIRONMENT')
?: 'production';
2024-03-17 08:50:47 +09:00
define('ENVIRONMENT', $env);
2024-02-19 09:45:40 +09:00
}
}
protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = true): void
2024-02-19 09:45:40 +09:00
{
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
return;
}
if ($exit) {
2024-02-19 09:45:40 +09:00
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'The application environment is not set correctly.';
exit(EXIT_ERROR);
2024-02-19 09:45:40 +09:00
}
}
2024-02-19 09:40:13 +09:00
/**
* 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.
*/
2024-02-19 11:50:55 +09:00
protected static function definePathConstants(Paths $paths): void
2024-02-19 09:40:13 +09:00
{
// 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';
2024-02-19 09:40:13 +09:00
}
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();
Services::autoloader()->loadHelpers();
}
protected static function setExceptionHandler(): void
{
Services::exceptions()->initialize();
}
protected static function checkMissingExtensions(): void
{
// Run this check for manual installations
if (! is_file(COMPOSER_PATH)) {
$missingExtensions = [];
foreach ([
'intl',
'json',
'mbstring',
] as $extension) {
if (! extension_loaded($extension)) {
$missingExtensions[] = $extension;
}
}
if ($missingExtensions !== []) {
$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);
2024-02-19 09:40:13 +09:00
}
}
}
protected static function initializeKint(): void
{
Services::autoloader()->initializeKint(CI_DEBUG);
}
}