From 9e90ef9f3a2ed279ed76e72f5bfea54460fdb66d Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 18:54:13 +0900 Subject: [PATCH 01/36] refactor: move CodeIgniter::initializeKint() to Autoloader --- system/Autoloader/Autoloader.php | 78 ++++++++++++++++++++++++++++ system/CodeIgniter.php | 10 +++- system/Test/bootstrap.php | 1 + system/bootstrap.php | 1 + tests/system/CommonFunctionsTest.php | 7 +-- 5 files changed, 90 insertions(+), 7 deletions(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index 48516e522584..07510eb57625 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -17,8 +17,13 @@ use Composer\Autoload\ClassLoader; use Composer\InstalledVersions; use Config\Autoload; +use Config\Kint as KintConfig; use Config\Modules; +use Config\Services; use InvalidArgumentException; +use Kint; +use Kint\Renderer\CliRenderer; +use Kint\Renderer\RichRenderer; use RuntimeException; /** @@ -481,4 +486,77 @@ public function loadHelpers(): void { helper($this->helpers); } + + /** + * Initializes Kint + */ + public function initializeKint(bool $debug = false): void + { + if ($debug) { + $this->autoloadKint(); + $this->configureKint(); + } elseif (class_exists(Kint::class)) { + // In case that Kint is already loaded via Composer. + Kint::$enabled_mode = false; + // @codeCoverageIgnore + } + + helper('kint'); + } + + private function autoloadKint(): void + { + // If we have KINT_DIR it means it's already loaded via composer + if (! defined('KINT_DIR')) { + spl_autoload_register(function ($class) { + $class = explode('\\', $class); + + if (array_shift($class) !== 'Kint') { + return; + } + + $file = SYSTEMPATH . 'ThirdParty/Kint/' . implode('/', $class) . '.php'; + + if (is_file($file)) { + require_once $file; + } + }); + + require_once SYSTEMPATH . 'ThirdParty/Kint/init.php'; + } + } + + private function configureKint(): void + { + $config = new KintConfig(); + + Kint::$depth_limit = $config->maxDepth; + Kint::$display_called_from = $config->displayCalledFrom; + Kint::$expanded = $config->expanded; + + if (isset($config->plugins) && is_array($config->plugins)) { + Kint::$plugins = $config->plugins; + } + + $csp = Services::csp(); + if ($csp->enabled()) { + RichRenderer::$js_nonce = $csp->getScriptNonce(); + RichRenderer::$css_nonce = $csp->getStyleNonce(); + } + + RichRenderer::$theme = $config->richTheme; + RichRenderer::$folder = $config->richFolder; + RichRenderer::$sort = $config->richSort; + if (isset($config->richObjectPlugins) && is_array($config->richObjectPlugins)) { + RichRenderer::$value_plugins = $config->richObjectPlugins; + } + if (isset($config->richTabPlugins) && is_array($config->richTabPlugins)) { + RichRenderer::$tab_plugins = $config->richTabPlugins; + } + + CliRenderer::$cli_colors = $config->cliColors; + CliRenderer::$force_utf8 = $config->cliForceUTF8; + CliRenderer::$detect_width = $config->cliDetectWidth; + CliRenderer::$min_terminal_width = $config->cliMinWidth; + } } diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8322d4ee34eb..8031bdc14a9c 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -202,8 +202,6 @@ public function initialize() // Set default timezone on the server date_default_timezone_set($this->config->appTimezone ?? 'UTC'); - - $this->initializeKint(); } /** @@ -240,6 +238,8 @@ protected function resolvePlatformExtensions() * Initializes Kint * * @return void + * + * @deprecated 4.5.0 Moved to Autoloader. */ protected function initializeKint() { @@ -255,6 +255,9 @@ protected function initializeKint() helper('kint'); } + /** + * @deprecated 4.5.0 Moved to Autoloader. + */ private function autoloadKint(): void { // If we have KINT_DIR it means it's already loaded via composer @@ -277,6 +280,9 @@ private function autoloadKint(): void } } + /** + * @deprecated 4.5.0 Moved to Autoloader. + */ private function configureKint(): void { $config = new KintConfig(); diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 8b992be9f884..a8ef6b01b0b9 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -78,6 +78,7 @@ // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); +Services::autoloader()->initializeKint(CI_DEBUG); // Now load Composer's if it's available if (is_file(COMPOSER_PATH)) { diff --git a/system/bootstrap.php b/system/bootstrap.php index e03fd570ca4c..b89689644389 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -106,3 +106,4 @@ // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); +Services::autoloader()->initializeKint(CI_DEBUG); diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index e583b8a67194..a31e73c749cf 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -26,7 +26,6 @@ use CodeIgniter\Session\Handlers\FileHandler; use CodeIgniter\Session\Session; use CodeIgniter\Test\CIUnitTestCase; -use CodeIgniter\Test\Mock\MockCodeIgniter; use CodeIgniter\Test\Mock\MockIncomingRequest; use CodeIgniter\Test\Mock\MockSecurity; use CodeIgniter\Test\Mock\MockSession; @@ -710,8 +709,7 @@ public function testDWithCSP(): void $config->CSPEnabled = true; // Initialize Kint - $app = new MockCodeIgniter($config); - $app->initialize(); + Services::autoloader()->initializeKint(CI_DEBUG); $cliDetection = Kint::$cli_detection; Kint::$cli_detection = false; @@ -736,8 +734,7 @@ public function testTraceWithCSP(): void $config->CSPEnabled = true; // Initialize Kint - $app = new MockCodeIgniter($config); - $app->initialize(); + Services::autoloader()->initializeKint(CI_DEBUG); Kint::$cli_detection = false; From 870edb9bd8a80c2d58ac31cba29fb5c533358007 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 18:55:59 +0900 Subject: [PATCH 02/36] docs: remove unneeded @var --- system/bootstrap.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/system/bootstrap.php b/system/bootstrap.php index b89689644389..6b7e94a2b0f6 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -26,11 +26,10 @@ * so they are available in the config files that are loaded. */ +/** @var Paths $paths */ + // The path to the application directory. if (! defined('APPPATH')) { - /** - * @var Paths $paths - */ define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } @@ -41,25 +40,16 @@ // The path to the system directory. if (! defined('SYSTEMPATH')) { - /** - * @var Paths $paths - */ define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } // The path to the writable directory. if (! defined('WRITEPATH')) { - /** - * @var Paths $paths - */ define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } // The path to the tests directory if (! defined('TESTPATH')) { - /** - * @var Paths $paths - */ define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } From 55dd55462f49ec2a0bb3b81bdc1bbc5849f65a6e Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 19:34:27 +0900 Subject: [PATCH 03/36] refactor: move CodeIgniter::bootstrapEnvironment() to bootstrap.php --- public/index.php | 14 ++++++++------ spark | 14 ++++++++------ system/CodeIgniter.php | 5 ++--- system/Test/bootstrap.php | 9 +++++++++ system/bootstrap.php | 21 +++++++++++++++++++++ 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/public/index.php b/public/index.php index 8cf5ce347c82..6d78241411ba 100644 --- a/public/index.php +++ b/public/index.php @@ -36,18 +36,20 @@ $paths = new Config\Paths(); -// Location of the framework bootstrap file. -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - // Load environment settings from .env files into $_SERVER and $_ENV -require_once SYSTEMPATH . 'Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); // Define ENVIRONMENT if (! defined('ENVIRONMENT')) { - define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production')); + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); } +// Location of the framework bootstrap file. +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; + // Load Config Cache // $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); // $factoriesCache->load('config'); diff --git a/spark b/spark index adbd43fafd63..1d4293f59a1a 100755 --- a/spark +++ b/spark @@ -64,18 +64,20 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); -// Location of the framework bootstrap file. -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - // Load environment settings from .env files into $_SERVER and $_ENV -require_once SYSTEMPATH . 'Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); // Define ENVIRONMENT if (! defined('ENVIRONMENT')) { - define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production')); + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); } +// Location of the framework bootstrap file. +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; + // Grab our CodeIgniter $app = Config\Services::codeigniter(); $app->initialize(); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 8031bdc14a9c..cb62124a8a35 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -186,9 +186,6 @@ public function __construct(App $config) */ public function initialize() { - // Define environment variables - $this->bootstrapEnvironment(); - // Setup Exception Handling Services::exceptions()->initialize(); @@ -584,6 +581,8 @@ protected function detectEnvironment() * is wrong. At the very least, they should have error reporting setup. * * @return void + * + * @deprecated 4.5.0 Moved to system/bootstrap.php. */ protected function bootstrapEnvironment() { diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index a8ef6b01b0b9..7d67acd070a6 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -39,6 +39,10 @@ require CONFIGPATH . 'Paths.php'; $paths = new Paths(); +// Load environment settings from .env files into $_SERVER and $_ENV +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new DotEnv($paths->appDirectory . '/../'))->load(); + // Define necessary framework path constants defined('APPPATH') || define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); defined('WRITEPATH') || define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); @@ -51,6 +55,11 @@ defined('COMPOSER_PATH') || define('COMPOSER_PATH', (string) realpath(HOMEPATH . 'vendor/autoload.php')); defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); +// Load environment bootstrap +if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { + require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +} + // Load Common.php from App then System if (is_file(APPPATH . 'Common.php')) { require_once APPPATH . 'Common.php'; diff --git a/system/bootstrap.php b/system/bootstrap.php index 6b7e94a2b0f6..fef24bd9807f 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -53,6 +53,27 @@ define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } +/* + * --------------------------------------------------------------- + * LOAD ENVIRONMENT BOOTSTRAP + * --------------------------------------------------------------- + * + * Load any custom boot files based upon the current environment. + * If no boot file exists, we shouldn't continue because something + * is wrong. At the very least, they should have error reporting setup. + */ + +if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { + require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +} else { + // @codeCoverageIgnoreStart + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR + // @codeCoverageIgnoreEnd +} + /* * --------------------------------------------------------------- * GRAB OUR CONSTANTS & COMMON From 14a5968749310b48fd53e29a7d7959b2a365b668 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 21:21:55 +0900 Subject: [PATCH 04/36] docs: remove meaningless @codeCoverageIgnore --- system/Autoloader/Autoloader.php | 1 - 1 file changed, 1 deletion(-) diff --git a/system/Autoloader/Autoloader.php b/system/Autoloader/Autoloader.php index 07510eb57625..c8c1767a9d83 100644 --- a/system/Autoloader/Autoloader.php +++ b/system/Autoloader/Autoloader.php @@ -498,7 +498,6 @@ public function initializeKint(bool $debug = false): void } elseif (class_exists(Kint::class)) { // In case that Kint is already loaded via Composer. Kint::$enabled_mode = false; - // @codeCoverageIgnore } helper('kint'); From 5b3005ac75e745a42e10cdf7cbe5cf2cecafb517 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 21:38:41 +0900 Subject: [PATCH 05/36] docs: add comment headings --- public/index.php | 20 ++++++++++++++++---- spark | 43 ++++++++++++++++++++++++++++++++++++------- system/bootstrap.php | 8 +++++++- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/public/index.php b/public/index.php index 6d78241411ba..9132d097e769 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,11 @@ systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); -// Define ENVIRONMENT +// DEFINE ENVIRONMENT if (! defined('ENVIRONMENT')) { $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); define('ENVIRONMENT', ($env !== false) ? $env : 'production'); unset($env); } -// Location of the framework bootstrap file. +// LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; // Load Config Cache diff --git a/spark b/spark index 1d4293f59a1a..5d93cd5eec06 100755 --- a/spark +++ b/spark @@ -12,7 +12,7 @@ /* * -------------------------------------------------------------------- - * CodeIgniter command-line tools + * CODEIGNITER COMMAND-LINE TOOLS * -------------------------------------------------------------------- * The main entry point into the CLI system and allows you to run * commands and perform maintenance on your application. @@ -26,7 +26,12 @@ if (strpos(PHP_SAPI, 'cgi') === 0) { exit("The cli tool is not supported when running php-cgi. It needs php-cli to function!\n\n"); } -// Check PHP version. +/* + *--------------------------------------------------------------- + * CHECK PHP VERSION + *--------------------------------------------------------------- + */ + $minPhpVersion = '8.1'; // If you update this, don't forget to update `public/index.php`. if (version_compare(PHP_VERSION, $minPhpVersion, '<')) { $message = sprintf( @@ -42,6 +47,12 @@ if (version_compare(PHP_VERSION, $minPhpVersion, '<')) { error_reporting(E_ALL); ini_set('display_errors', '1'); +/* + *--------------------------------------------------------------- + * SET THE CURRENT DIRECTORY + *--------------------------------------------------------------- + */ + // Path to the front controller define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); @@ -57,34 +68,46 @@ chdir(FCPATH); * and fires up an environment-specific bootstrapping. */ -// Load our paths config file +// LOAD OUR PATHS CONFIG FILE // This is the line that might need to be changed, depending on your folder structure. require FCPATH . '../app/Config/Paths.php'; // ^^^ Change this line if you move your application folder $paths = new Config\Paths(); +// LOAD DOTENV FILE // Load environment settings from .env files into $_SERVER and $_ENV require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); -// Define ENVIRONMENT +// DEFINE ENVIRONMENT if (! defined('ENVIRONMENT')) { $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); define('ENVIRONMENT', ($env !== false) ? $env : 'production'); unset($env); } -// Location of the framework bootstrap file. +// LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; -// Grab our CodeIgniter +/* + * --------------------------------------------------------------- + * GRAB OUR CODEIGNITER INSTANCE + * --------------------------------------------------------------- + */ + $app = Config\Services::codeigniter(); $app->initialize(); -// Grab our Console +/* + * --------------------------------------------------------------- + * GRAB OUR CONSOLE + * --------------------------------------------------------------- + */ + $console = new CodeIgniter\CLI\Console(); +// SHOW HEADER // Show basic information before we do anything else. if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore @@ -93,6 +116,12 @@ if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { $console->showHeader($suppress); +/* + *--------------------------------------------------------------- + * EXECUTE THE COMMAND + *--------------------------------------------------------------- + */ + // fire off the command in the main framework. $exit = $console->run(); diff --git a/system/bootstrap.php b/system/bootstrap.php index fef24bd9807f..50cc24043318 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -76,7 +76,7 @@ /* * --------------------------------------------------------------- - * GRAB OUR CONSTANTS & COMMON + * GRAB OUR CONSTANTS * --------------------------------------------------------------- */ @@ -84,6 +84,12 @@ require_once APPPATH . 'Config/Constants.php'; } +/* + * --------------------------------------------------------------- + * LOAD COMMON FUNCTIONS + * --------------------------------------------------------------- + */ + // Require app/Common.php file if exists. if (is_file(APPPATH . 'Common.php')) { require_once APPPATH . 'Common.php'; From e4638d335ba661ed5ab3022b24e4253eab5dbf2e Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 22:11:26 +0900 Subject: [PATCH 06/36] refactor: move DEFINE ENVIRONMENT to bootstrap.php --- public/index.php | 7 ------- spark | 7 ------- system/bootstrap.php | 12 ++++++++++++ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/public/index.php b/public/index.php index 9132d097e769..9df0b8e86a67 100644 --- a/public/index.php +++ b/public/index.php @@ -52,13 +52,6 @@ require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; diff --git a/spark b/spark index 5d93cd5eec06..16af4a543c52 100755 --- a/spark +++ b/spark @@ -80,13 +80,6 @@ $paths = new Config\Paths(); require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; diff --git a/system/bootstrap.php b/system/bootstrap.php index 50cc24043318..dcb074242251 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -16,6 +16,18 @@ use Config\Paths; use Config\Services; +/* + * --------------------------------------------------------------- + * DEFINE ENVIRONMENT + * --------------------------------------------------------------- + */ + +if (! defined('ENVIRONMENT')) { + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); +} + /* * --------------------------------------------------------------- * SETUP OUR PATH CONSTANTS From 7433b89576bf7583b6ae65a3a6bbff0bb3a6c552 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 22:17:45 +0900 Subject: [PATCH 07/36] refactor: move Services::exceptions()->initialize() to bootstrap.php --- system/CodeIgniter.php | 3 --- system/Test/bootstrap.php | 5 +++++ system/bootstrap.php | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index cb62124a8a35..7b45e815f684 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -186,9 +186,6 @@ public function __construct(App $config) */ public function initialize() { - // Setup Exception Handling - Services::exceptions()->initialize(); - // Run this check for manual installations if (! is_file(COMPOSER_PATH)) { $this->resolvePlatformExtensions(); // @codeCoverageIgnore diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 7d67acd070a6..15b1b8cc4b2d 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -87,6 +87,11 @@ // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); + +// Setup Exception Handling +Services::exceptions()->initialize(); + +// Initialize Kint Services::autoloader()->initializeKint(CI_DEBUG); // Now load Composer's if it's available diff --git a/system/bootstrap.php b/system/bootstrap.php index dcb074242251..58f8d645a748 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -135,4 +135,14 @@ // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); + +/* + * --------------------------------------------------------------- + * SET EXCEPTION AND ERROR HANDLERS + * --------------------------------------------------------------- + */ + +Services::exceptions()->initialize(); + +// Initialize Kint Services::autoloader()->initializeKint(CI_DEBUG); From f240f25a77bf11c9f6ebb3ba3991fd147a32e457 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 22:23:48 +0900 Subject: [PATCH 08/36] refactor: move CodeIgniter::resolvePlatformExtensions() to bootstrap.php --- system/CodeIgniter.php | 7 ++----- system/bootstrap.php | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 7b45e815f684..01ec43408484 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -186,11 +186,6 @@ public function __construct(App $config) */ public function initialize() { - // Run this check for manual installations - if (! is_file(COMPOSER_PATH)) { - $this->resolvePlatformExtensions(); // @codeCoverageIgnore - } - // Set default locale on the server Locale::setDefault($this->config->defaultLocale ?? 'en'); @@ -206,6 +201,8 @@ public function initialize() * @throws FrameworkException * * @codeCoverageIgnore + * + * @deprecated 4.5.0 Moved to system/bootstrap.php. */ protected function resolvePlatformExtensions() { diff --git a/system/bootstrap.php b/system/bootstrap.php index 58f8d645a748..617492428b55 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -11,6 +11,7 @@ * the LICENSE file that was distributed with this source code. */ +use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; use Config\Paths; @@ -144,5 +145,37 @@ Services::exceptions()->initialize(); -// Initialize Kint +/* + * --------------------------------------------------------------- + * CHECK SYSTEM FOR MISSING REQUIRED PHP EXTENSIONS + * --------------------------------------------------------------- + */ + +// Run this check for manual installations +if (! is_file(COMPOSER_PATH)) { + $requiredExtensions = [ + 'intl', + 'json', + 'mbstring', + ]; + + $missingExtensions = []; + + foreach ($requiredExtensions as $extension) { + if (! extension_loaded($extension)) { + $missingExtensions[] = $extension; + } + } + + if ($missingExtensions !== []) { + throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); + } +} + +/* + * --------------------------------------------------------------- + * INITIALIZE KINT + * --------------------------------------------------------------- + */ + Services::autoloader()->initializeKint(CI_DEBUG); From d40fcf1584709a56ac35bd7c91669d5777cf3e00 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 22:44:34 +0900 Subject: [PATCH 09/36] refactor: remove variables --- system/bootstrap.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/system/bootstrap.php b/system/bootstrap.php index 617492428b55..058160dd75b4 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -153,15 +153,13 @@ // Run this check for manual installations if (! is_file(COMPOSER_PATH)) { - $requiredExtensions = [ + $missingExtensions = []; + + foreach ([ 'intl', 'json', 'mbstring', - ]; - - $missingExtensions = []; - - foreach ($requiredExtensions as $extension) { + ] as $extension) { if (! extension_loaded($extension)) { $missingExtensions[] = $extension; } @@ -170,6 +168,8 @@ if ($missingExtensions !== []) { throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); } + + unset($missingExtensions); } /* From cf4f00935bd5818ca36b47930c1eba2cf81c99e7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 07:13:38 +0900 Subject: [PATCH 10/36] refactor: match test/bootstrap to system/bootstrap as much as possible --- system/Test/bootstrap.php | 127 +++++++++++++++++++++++++++++--------- 1 file changed, 99 insertions(+), 28 deletions(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 15b1b8cc4b2d..4c9380b82b67 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -21,11 +21,52 @@ ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); +/* + * --------------------------------------------------------------- + * DEFINE ENVIRONMENT + * --------------------------------------------------------------- + */ + // Make sure it recognizes that we're testing. $_SERVER['CI_ENVIRONMENT'] = 'testing'; define('ENVIRONMENT', 'testing'); + defined('CI_DEBUG') || define('CI_DEBUG', true); +/* + *--------------------------------------------------------------- + * BOOTSTRAP THE APPLICATION + *--------------------------------------------------------------- + * This process sets up the path constants, loads and registers + * our autoloader, along with Composer's, loads our constants + * and fires up an environment-specific bootstrapping. + */ + +// LOAD OUR PATHS CONFIG FILE +// Load framework paths from their config file +require CONFIGPATH . 'Paths.php'; +$paths = new Paths(); + +// LOAD DOTENV FILE +// Load environment settings from .env files into $_SERVER and $_ENV +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new DotEnv($paths->appDirectory . '/../'))->load(); + +// Set environment values that would otherwise stop the framework from functioning during tests. +if (! isset($_SERVER['app.baseURL'])) { + $_SERVER['app.baseURL'] = 'http://example.com/'; +} + +/* + * --------------------------------------------------------------- + * SETUP OUR PATH CONSTANTS + * --------------------------------------------------------------- + * + * The path constants provide convenient access to the folders + * throughout the application. We have to setup them up here + * so they are available in the config files that are loaded. + */ + // Often these constants are pre-defined, but query the current directory structure as a fallback defined('HOMEPATH') || define('HOMEPATH', realpath(rtrim(getcwd(), '\\/ ')) . DIRECTORY_SEPARATOR); $source = is_dir(HOMEPATH . 'app') @@ -35,31 +76,48 @@ defined('PUBLICPATH') || define('PUBLICPATH', realpath($source . 'public') . DIRECTORY_SEPARATOR); unset($source); -// Load framework paths from their config file -require CONFIGPATH . 'Paths.php'; -$paths = new Paths(); +// Define necessary framework path constants +defined('APPPATH') || define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); +defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); +defined('SYSTEMPATH') || define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/')) . DIRECTORY_SEPARATOR); +defined('WRITEPATH') || define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); +defined('TESTPATH') || define('TESTPATH', realpath(HOMEPATH . 'tests/') . DIRECTORY_SEPARATOR); -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new DotEnv($paths->appDirectory . '/../'))->load(); +defined('CIPATH') || define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR); +defined('FCPATH') || define('FCPATH', realpath(PUBLICPATH) . DIRECTORY_SEPARATOR); -// Define necessary framework path constants -defined('APPPATH') || define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); -defined('WRITEPATH') || define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); -defined('SYSTEMPATH') || define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/')) . DIRECTORY_SEPARATOR); -defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); -defined('CIPATH') || define('CIPATH', realpath(SYSTEMPATH . '../') . DIRECTORY_SEPARATOR); -defined('FCPATH') || define('FCPATH', realpath(PUBLICPATH) . DIRECTORY_SEPARATOR); -defined('TESTPATH') || define('TESTPATH', realpath(HOMEPATH . 'tests/') . DIRECTORY_SEPARATOR); defined('SUPPORTPATH') || define('SUPPORTPATH', realpath(TESTPATH . '_support/') . DIRECTORY_SEPARATOR); defined('COMPOSER_PATH') || define('COMPOSER_PATH', (string) realpath(HOMEPATH . 'vendor/autoload.php')); defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); -// Load environment bootstrap +/* + * --------------------------------------------------------------- + * LOAD ENVIRONMENT BOOTSTRAP + * --------------------------------------------------------------- + * + * Load any custom boot files based upon the current environment. + * If no boot file exists, we shouldn't continue because something + * is wrong. At the very least, they should have error reporting setup. + */ + if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; } +/* + * --------------------------------------------------------------- + * GRAB OUR CONSTANTS + * --------------------------------------------------------------- + */ + +require_once APPPATH . 'Config/Constants.php'; + +/* + * --------------------------------------------------------------- + * LOAD COMMON FUNCTIONS + * --------------------------------------------------------------- + */ + // Load Common.php from App then System if (is_file(APPPATH . 'Common.php')) { require_once APPPATH . 'Common.php'; @@ -67,15 +125,18 @@ require_once SYSTEMPATH . 'Common.php'; -// Set environment values that would otherwise stop the framework from functioning during tests. -if (! isset($_SERVER['app.baseURL'])) { - $_SERVER['app.baseURL'] = 'http://example.com/'; -} +/* + * --------------------------------------------------------------- + * LOAD OUR AUTOLOADER + * --------------------------------------------------------------- + * + * The autoloader allows all of 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. + */ -// Load necessary components require_once SYSTEMPATH . 'Config/AutoloadConfig.php'; require_once APPPATH . 'Config/Autoload.php'; -require_once APPPATH . 'Config/Constants.php'; require_once SYSTEMPATH . 'Modules/Modules.php'; require_once APPPATH . 'Config/Modules.php'; @@ -88,10 +149,20 @@ Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); -// Setup Exception Handling +/* + * --------------------------------------------------------------- + * SET EXCEPTION AND ERROR HANDLERS + * --------------------------------------------------------------- + */ + Services::exceptions()->initialize(); -// Initialize Kint +/* + * --------------------------------------------------------------- + * INITIALIZE KINT + * --------------------------------------------------------------- + */ + Services::autoloader()->initializeKint(CI_DEBUG); // Now load Composer's if it's available @@ -99,10 +170,10 @@ require_once COMPOSER_PATH; } -// Load environment settings from .env files into $_SERVER and $_ENV -require_once SYSTEMPATH . 'Config/DotEnv.php'; - -$env = new DotEnv(ROOTPATH); -$env->load(); +/* + * --------------------------------------------------------------- + * LOAD ROUTES + * --------------------------------------------------------------- + */ Services::routes()->loadRoutes(); From 40e72a5b4041abdf23fed431f7087adb8ec1d9f4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 07:31:52 +0900 Subject: [PATCH 11/36] refactor: remove `$_SERVER['app.baseURL'] = 'http://example.com/'` The value is set by phpnit.xml.dist, and there is no need to change baseURL. --- system/Test/bootstrap.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 4c9380b82b67..c5662a2a572c 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -52,11 +52,6 @@ require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new DotEnv($paths->appDirectory . '/../'))->load(); -// Set environment values that would otherwise stop the framework from functioning during tests. -if (! isset($_SERVER['app.baseURL'])) { - $_SERVER['app.baseURL'] = 'http://example.com/'; -} - /* * --------------------------------------------------------------- * SETUP OUR PATH CONSTANTS From 9c75dd8a91b0bd39151a3325494c92cd0ded9f7c Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 07:41:55 +0900 Subject: [PATCH 12/36] refactor: remove unneeded `require_once COMPOSER_PATH` It is done in `Services::exceptions()->initialize()`. --- system/Test/bootstrap.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index c5662a2a572c..73d894379736 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -160,11 +160,6 @@ Services::autoloader()->initializeKint(CI_DEBUG); -// Now load Composer's if it's available -if (is_file(COMPOSER_PATH)) { - require_once COMPOSER_PATH; -} - /* * --------------------------------------------------------------- * LOAD ROUTES From 3d57ea0020600a88e1c00af94e01be2cc726927f Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 07:51:07 +0900 Subject: [PATCH 13/36] fix: move LOAD ENVIRONMENT BOOTSTRAP to index.php This was in CodeIgniter::initialize(), so it was called only in web. --- public/index.php | 10 ++++++++++ system/bootstrap.php | 21 --------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/public/index.php b/public/index.php index 9df0b8e86a67..c266197e44be 100644 --- a/public/index.php +++ b/public/index.php @@ -52,6 +52,16 @@ require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); +// LOAD ENVIRONMENT BOOTSTRAP +if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { + require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +} else { + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR +} + // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; diff --git a/system/bootstrap.php b/system/bootstrap.php index 058160dd75b4..ce9765995c61 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -66,27 +66,6 @@ define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } -/* - * --------------------------------------------------------------- - * LOAD ENVIRONMENT BOOTSTRAP - * --------------------------------------------------------------- - * - * Load any custom boot files based upon the current environment. - * If no boot file exists, we shouldn't continue because something - * is wrong. At the very least, they should have error reporting setup. - */ - -if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { - require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; -} else { - // @codeCoverageIgnoreStart - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo 'The application environment is not set correctly.'; - - exit(EXIT_ERROR); // EXIT_ERROR - // @codeCoverageIgnoreEnd -} - /* * --------------------------------------------------------------- * GRAB OUR CONSTANTS From 30197abdae61c7d9356ec52212cfaa013d65a300 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 08:40:49 +0900 Subject: [PATCH 14/36] fix: move BOOTSTRAP THE APPLICATION down CONFIGPATH was not defined yet. --- system/Test/bootstrap.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 73d894379736..470c3be5909f 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -33,25 +33,6 @@ defined('CI_DEBUG') || define('CI_DEBUG', true); -/* - *--------------------------------------------------------------- - * BOOTSTRAP THE APPLICATION - *--------------------------------------------------------------- - * This process sets up the path constants, loads and registers - * our autoloader, along with Composer's, loads our constants - * and fires up an environment-specific bootstrapping. - */ - -// LOAD OUR PATHS CONFIG FILE -// Load framework paths from their config file -require CONFIGPATH . 'Paths.php'; -$paths = new Paths(); - -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new DotEnv($paths->appDirectory . '/../'))->load(); - /* * --------------------------------------------------------------- * SETUP OUR PATH CONSTANTS @@ -85,6 +66,25 @@ defined('COMPOSER_PATH') || define('COMPOSER_PATH', (string) realpath(HOMEPATH . 'vendor/autoload.php')); defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); +/* + *--------------------------------------------------------------- + * BOOTSTRAP THE APPLICATION + *--------------------------------------------------------------- + * This process sets up the path constants, loads and registers + * our autoloader, along with Composer's, loads our constants + * and fires up an environment-specific bootstrapping. + */ + +// LOAD OUR PATHS CONFIG FILE +// Load framework paths from their config file +require CONFIGPATH . 'Paths.php'; +$paths = new Paths(); + +// LOAD DOTENV FILE +// Load environment settings from .env files into $_SERVER and $_ENV +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new DotEnv($paths->appDirectory . '/../'))->load(); + /* * --------------------------------------------------------------- * LOAD ENVIRONMENT BOOTSTRAP From edd0accd45c9bb9ccc09a3bedb6d383b0ff3f669 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:19:15 +0900 Subject: [PATCH 15/36] fix: add missing LOAD ENVIRONMENT BOOTSTRAP --- spark | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spark b/spark index 16af4a543c52..a166f32726c2 100755 --- a/spark +++ b/spark @@ -80,6 +80,16 @@ $paths = new Config\Paths(); require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); +// LOAD ENVIRONMENT BOOTSTRAP +if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { + require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +} else { + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR +} + // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; From e8fe4918724cda53ace98ff785aa7bfc8b4333a3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:32:06 +0900 Subject: [PATCH 16/36] fix: index.php and spark do not work --- public/index.php | 11 +++++++++-- spark | 11 +++++++++-- system/bootstrap.php | 12 ------------ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/public/index.php b/public/index.php index c266197e44be..0bfbce2053fd 100644 --- a/public/index.php +++ b/public/index.php @@ -52,9 +52,16 @@ require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); +// DEFINE ENVIRONMENT +if (! defined('ENVIRONMENT')) { + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); +} + // LOAD ENVIRONMENT BOOTSTRAP -if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { - require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { + require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; } else { header('HTTP/1.1 503 Service Unavailable.', true, 503); echo 'The application environment is not set correctly.'; diff --git a/spark b/spark index a166f32726c2..6b4034131925 100755 --- a/spark +++ b/spark @@ -80,9 +80,16 @@ $paths = new Config\Paths(); require_once $paths->systemDirectory . '/Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); +// DEFINE ENVIRONMENT +if (! defined('ENVIRONMENT')) { + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); +} + // LOAD ENVIRONMENT BOOTSTRAP -if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { - require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { + require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; } else { header('HTTP/1.1 503 Service Unavailable.', true, 503); echo 'The application environment is not set correctly.'; diff --git a/system/bootstrap.php b/system/bootstrap.php index ce9765995c61..84d52cef5cbc 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -17,18 +17,6 @@ use Config\Paths; use Config\Services; -/* - * --------------------------------------------------------------- - * DEFINE ENVIRONMENT - * --------------------------------------------------------------- - */ - -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - /* * --------------------------------------------------------------- * SETUP OUR PATH CONSTANTS From 0331a224d6f1a05273da514de62c1ebd4af7dd09 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:40:13 +0900 Subject: [PATCH 17/36] refactor: extract Boot class --- public/index.php | 3 +- spark | 3 +- system/Boot.php | 164 +++++++++++++++++++++++++++++++++++++++++++ system/bootstrap.php | 7 ++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 system/Boot.php diff --git a/public/index.php b/public/index.php index 0bfbce2053fd..b8072f8e7b46 100644 --- a/public/index.php +++ b/public/index.php @@ -70,7 +70,8 @@ } // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +CodeIgniter\Boot::BootWeb($paths); // Load Config Cache // $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); diff --git a/spark b/spark index 6b4034131925..1be5328728c0 100755 --- a/spark +++ b/spark @@ -98,7 +98,8 @@ if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { } // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +CodeIgniter\Boot::BootSpark($paths); /* * --------------------------------------------------------------- diff --git a/system/Boot.php b/system/Boot.php new file mode 100644 index 000000000000..7ae52f0aabb8 --- /dev/null +++ b/system/Boot.php @@ -0,0 +1,164 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter; + +use CodeIgniter\Exceptions\FrameworkException; +use Config\Autoload; +use Config\Modules; +use Config\Paths; +use Config\Services; + +class Boot +{ + /** + * @used-by public/index.php + * + * Context + * web: Invoked by HTTP request + * php-cli: Invoked by CLI via `php public/index.php` + */ + public static function bootWeb(Paths $paths): void + { + static::definePathConstant($paths); + static::loadConstants(); + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * @used-by spark + */ + public static function bootSpark(Paths $paths): void + { + static::definePathConstant($paths); + static::loadConstants(); + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * 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 definePathConstant(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 + { + if (! defined('APP_NAMESPACE')) { + 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(); + 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 !== []) { + throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); + } + + unset($missingExtensions); + } + } + + protected static function initializeKint(): void + { + Services::autoloader()->initializeKint(CI_DEBUG); + } +} diff --git a/system/bootstrap.php b/system/bootstrap.php index 84d52cef5cbc..f091aa306527 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -11,6 +11,13 @@ * the LICENSE file that was distributed with this source code. */ +/** + * --------------------------------------------------------------- + * + * @deprecated 4.5.0 This file is no longer used. Moved to Boot.php. + * --------------------------------------------------------------- + */ + use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; From f55bc38ad1542d48ead30562e8c465fe85b52ef8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:45:40 +0900 Subject: [PATCH 18/36] refactor: move code to Boot class --- public/index.php | 22 ---------------------- spark | 22 ---------------------- system/Boot.php | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/public/index.php b/public/index.php index b8072f8e7b46..a93c7d30d80e 100644 --- a/public/index.php +++ b/public/index.php @@ -47,28 +47,6 @@ $paths = new Config\Paths(); -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); - -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - -// LOAD ENVIRONMENT BOOTSTRAP -if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { - require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; -} else { - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo 'The application environment is not set correctly.'; - - exit(EXIT_ERROR); // EXIT_ERROR -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; CodeIgniter\Boot::BootWeb($paths); diff --git a/spark b/spark index 1be5328728c0..172bacc8c500 100755 --- a/spark +++ b/spark @@ -75,28 +75,6 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); - -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - -// LOAD ENVIRONMENT BOOTSTRAP -if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { - require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; -} else { - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo 'The application environment is not set correctly.'; - - exit(EXIT_ERROR); // EXIT_ERROR -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; CodeIgniter\Boot::BootSpark($paths); diff --git a/system/Boot.php b/system/Boot.php index 7ae52f0aabb8..00ae0f61de25 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -13,6 +13,7 @@ namespace CodeIgniter; +use CodeIgniter\Config\DotEnv; use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; @@ -30,6 +31,9 @@ class Boot */ public static function bootWeb(Paths $paths): void { + static::loadDotEnv($paths); + static::defineEnvironment(); + static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); static::loadConstants(); static::loadCommonFunctions(); @@ -44,6 +48,9 @@ public static function bootWeb(Paths $paths): void */ public static function bootSpark(Paths $paths): void { + static::loadDotEnv($paths); + static::defineEnvironment(); + static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); static::loadConstants(); static::loadCommonFunctions(); @@ -53,6 +60,36 @@ public static function bootSpark(Paths $paths): void static::initializeKint(); } + /** + * 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')) { + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); + } + } + + protected static function loadEnvironmentBootstrap(Paths $paths): void + { + if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { + require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; + } else { + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // 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 From 0b60b7d2452b6bb7b9eaf83747e8777ab510e82b Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 10:07:25 +0900 Subject: [PATCH 19/36] fix: Undefined variable $paths --- system/Test/bootstrap.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 470c3be5909f..08c6b2e52519 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -52,6 +52,11 @@ defined('PUBLICPATH') || define('PUBLICPATH', realpath($source . 'public') . DIRECTORY_SEPARATOR); unset($source); +// LOAD OUR PATHS CONFIG FILE +// Load framework paths from their config file +require CONFIGPATH . 'Paths.php'; +$paths = new Paths(); + // Define necessary framework path constants defined('APPPATH') || define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); defined('ROOTPATH') || define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); @@ -75,11 +80,6 @@ * and fires up an environment-specific bootstrapping. */ -// LOAD OUR PATHS CONFIG FILE -// Load framework paths from their config file -require CONFIGPATH . 'Paths.php'; -$paths = new Paths(); - // LOAD DOTENV FILE // Load environment settings from .env files into $_SERVER and $_ENV require_once $paths->systemDirectory . '/Config/DotEnv.php'; From 96b2b4dc2618c4910fde77449ceb22757a5eac93 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:19:00 +0900 Subject: [PATCH 20/36] refactor: use Boot class in Test/bootstrap.php --- system/Boot.php | 40 ++++++++++++++---- system/Test/bootstrap.php | 86 ++------------------------------------- 2 files changed, 37 insertions(+), 89 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 00ae0f61de25..6902684ca612 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -20,6 +20,9 @@ use Config\Paths; use Config\Services; +/** + * Bootstrap for the application + */ class Boot { /** @@ -35,7 +38,9 @@ public static function bootWeb(Paths $paths): void static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); - static::loadConstants(); + if (! defined('APP_NAMESPACE')) { + static::loadConstants(); + } static::loadCommonFunctions(); static::loadAutoloader(); static::setExceptionHandler(); @@ -52,6 +57,23 @@ public static function bootSpark(Paths $paths): void static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); + if (! defined('APP_NAMESPACE')) { + static::loadConstants(); + } + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * @used-by system/Test/bootstrap.php + */ + public static function bootTest(Paths $paths): void + { + static::loadDotEnv($paths); + static::loadEnvironmentBootstrap($paths, false); static::loadConstants(); static::loadCommonFunctions(); static::loadAutoloader(); @@ -72,21 +94,27 @@ protected static function loadDotEnv(Paths $paths): void protected static function defineEnvironment(): void { if (! defined('ENVIRONMENT')) { + // @phpstan-ignore-next-line $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); unset($env); } } - protected static function loadEnvironmentBootstrap(Paths $paths): void + 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'; - } else { + + return; + } + + if ($exit) { header('HTTP/1.1 503 Service Unavailable.', true, 503); echo 'The application environment is not set correctly.'; - exit(EXIT_ERROR); // EXIT_ERROR + exit(EXIT_ERROR); } } @@ -125,9 +153,7 @@ protected static function definePathConstant(Paths $paths): void protected static function loadConstants(): void { - if (! defined('APP_NAMESPACE')) { - require_once APPPATH . 'Config/Constants.php'; - } + require_once APPPATH . 'Config/Constants.php'; } protected static function loadCommonFunctions(): void diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 08c6b2e52519..cb70a435831b 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -11,9 +11,7 @@ * the LICENSE file that was distributed with this source code. */ -use CodeIgniter\Config\DotEnv; -use Config\Autoload; -use Config\Modules; +use CodeIgniter\Boot; use Config\Paths; use Config\Services; @@ -80,85 +78,9 @@ * and fires up an environment-specific bootstrapping. */ -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new DotEnv($paths->appDirectory . '/../'))->load(); - -/* - * --------------------------------------------------------------- - * LOAD ENVIRONMENT BOOTSTRAP - * --------------------------------------------------------------- - * - * Load any custom boot files based upon the current environment. - * If no boot file exists, we shouldn't continue because something - * is wrong. At the very least, they should have error reporting setup. - */ - -if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { - require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; -} - -/* - * --------------------------------------------------------------- - * GRAB OUR CONSTANTS - * --------------------------------------------------------------- - */ - -require_once APPPATH . 'Config/Constants.php'; - -/* - * --------------------------------------------------------------- - * LOAD COMMON FUNCTIONS - * --------------------------------------------------------------- - */ - -// Load Common.php from App then System -if (is_file(APPPATH . 'Common.php')) { - require_once APPPATH . 'Common.php'; -} - -require_once SYSTEMPATH . 'Common.php'; - -/* - * --------------------------------------------------------------- - * LOAD OUR AUTOLOADER - * --------------------------------------------------------------- - * - * The autoloader allows all of 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. - */ - -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(); - -/* - * --------------------------------------------------------------- - * SET EXCEPTION AND ERROR HANDLERS - * --------------------------------------------------------------- - */ - -Services::exceptions()->initialize(); - -/* - * --------------------------------------------------------------- - * INITIALIZE KINT - * --------------------------------------------------------------- - */ - -Services::autoloader()->initializeKint(CI_DEBUG); +// LOAD THE FRAMEWORK BOOTSTRAP FILE +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +Boot::bootTest($paths); /* * --------------------------------------------------------------- From ce6630669778c3210a01a5cfe87724b7089177b6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:50:55 +0900 Subject: [PATCH 21/36] refactor: fix method name --- system/Boot.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 6902684ca612..a1a9ca4a1168 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -37,7 +37,7 @@ public static function bootWeb(Paths $paths): void static::loadDotEnv($paths); static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); - static::definePathConstant($paths); + static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { static::loadConstants(); } @@ -56,7 +56,7 @@ public static function bootSpark(Paths $paths): void static::loadDotEnv($paths); static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); - static::definePathConstant($paths); + static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { static::loadConstants(); } @@ -123,7 +123,7 @@ protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = tr * the application. We have to set them up here, so they are available in * the config files that are loaded. */ - protected static function definePathConstant(Paths $paths): void + protected static function definePathConstants(Paths $paths): void { // The path to the application directory. if (! defined('APPPATH')) { From 5455e5cf88fcb854328afae2ca46cf2ef296f53a Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:55:41 +0900 Subject: [PATCH 22/36] refactor: remove rtrim() --- public/index.php | 2 +- spark | 2 +- system/Test/bootstrap.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/index.php b/public/index.php index a93c7d30d80e..057ec8a111c3 100644 --- a/public/index.php +++ b/public/index.php @@ -48,7 +48,7 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; CodeIgniter\Boot::BootWeb($paths); // Load Config Cache diff --git a/spark b/spark index 172bacc8c500..8588352a7e50 100755 --- a/spark +++ b/spark @@ -76,7 +76,7 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; CodeIgniter\Boot::BootSpark($paths); /* diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index cb70a435831b..6dfcc7654e83 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -79,7 +79,7 @@ */ // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; Boot::bootTest($paths); /* From 7233cfde5ddd0342e4f9e913da4d883c9692fd74 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 12:45:20 +0900 Subject: [PATCH 23/36] chore: add exlucde for PHPCPD --- .github/workflows/test-phpcpd.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test-phpcpd.yml b/.github/workflows/test-phpcpd.yml index 5c89eaf4337e..c0bf72f3bf10 100644 --- a/.github/workflows/test-phpcpd.yml +++ b/.github/workflows/test-phpcpd.yml @@ -57,4 +57,5 @@ jobs: --exclude system/Debug/Exceptions.php --exclude system/HTTP/SiteURI.php --exclude system/Validation/Rules.php + --exclude system/Autoloader/Autoloader.php -- app/ public/ system/ From e1deebed46d1044ab9a04fb8c73236a8d23ea361 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 12:52:25 +0900 Subject: [PATCH 24/36] docs: update comments --- spark | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spark b/spark index 8588352a7e50..a1b42e35b0b7 100755 --- a/spark +++ b/spark @@ -16,9 +16,12 @@ * -------------------------------------------------------------------- * The main entry point into the CLI system and allows you to run * commands and perform maintenance on your application. - * - * Because CodeIgniter can handle CLI requests as just another web request - * this class mainly acts as a passthru to the framework itself. + */ + +/* + *--------------------------------------------------------------- + * CHECK SERVER API + *--------------------------------------------------------------- */ // Refuse to run when called from php-cgi From 54d782eb45010d9b02b104c1973097368ac27ee7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 13:11:00 +0900 Subject: [PATCH 25/36] docs: fix typo --- system/Test/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 6dfcc7654e83..0810ac5d180c 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -37,7 +37,7 @@ * --------------------------------------------------------------- * * The path constants provide convenient access to the folders - * throughout the application. We have to setup them up here + * throughout the application. We have to set them up here * so they are available in the config files that are loaded. */ From 0e81e44d464dfca29ac2aa143c06b065685bd003 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 13:35:17 +0900 Subject: [PATCH 26/36] docs: add user guide --- user_guide_src/source/changelogs/v4.5.0.rst | 20 +++++++++++++++++-- .../source/installation/upgrade_450.rst | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 12c1b77e82fc..98666185389e 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -185,6 +185,8 @@ Others - **Web Page Caching:** ``ResponseCache`` has been improved to include the request HTTP method in the cache key. This means that the same URI will be cached separately if the HTTP method is different. +- **Bootstrap:** The ``CodeIgniter\Boot`` class has been introduced, replacing + **system/bootstrap.php**. ******** BREAKING @@ -445,8 +447,22 @@ Changes Deprecations ************ -- **CodeIgniter:** The ``determinePath()`` method has been deprecated. No longer - used. +- **CodeIgniter:** + - The ``determinePath()`` method has been deprecated. No longer used. + - The ``resolvePlatformExtensions()`` method has been deprecated. No longer + used. It has been moved to the ``CodeIgniter\Boot::checkMissingExtensions()`` + method. + - The ``bootstrapEnvironment()`` method has been deprecated. No longer used. + It has been moved to the ``CodeIgniter\Boot::loadEnvironmentBootstrap()`` + method. + - The ``initializeKint()`` method has been deprecated. No longer used. It has + been moved to the ``Autoloader``. + - The ``autoloadKint()`` method has been deprecated. No longer used. It has + been moved to the ``Autoloader``. + - The ``configureKint()`` method has been deprecated. No longer used. It has + been moved to the ``Autoloader``. +- **system/bootstrap.php:** This file has been deprecated. No longer used. + The code has been moved to the new ``CodeIgniter\Boot`` class. - **Response:** The constructor parameter ``$config`` has been deprecated. No longer used. - **Filters:** diff --git a/user_guide_src/source/installation/upgrade_450.rst b/user_guide_src/source/installation/upgrade_450.rst index 53498a20b301..27fe4bc67593 100644 --- a/user_guide_src/source/installation/upgrade_450.rst +++ b/user_guide_src/source/installation/upgrade_450.rst @@ -15,6 +15,26 @@ Please refer to the upgrade instructions corresponding to your installation meth Mandatory File Changes ********************** +index.php and spark +=================== + +The following files received significant changes and +**you must merge the updated versions** with your application: + +- ``public/index.php`` +- ``spark`` + +.. important:: If you don't update the above files, CodeIgniter will not work + properly after running ``composer update``. + + The upgrade procedure, for example, is as follows: + + .. code-block:: console + + composer update + cp vendor/codeigniter4/framework/public/index.php public/index.php + cp vendor/codeigniter4/framework/spark spark + Breaking Changes **************** From fb154fc992e3dde05f4ac1765ec768ae8094f9a1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 13:52:24 +0900 Subject: [PATCH 27/36] docs: add @codeCoverageIgnore --- system/Boot.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/Boot.php b/system/Boot.php index a1a9ca4a1168..0001db6ba051 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -22,6 +22,8 @@ /** * Bootstrap for the application + * + * @codeCoverageIgnore */ class Boot { From 574bb5e7caf97b267e0b50f561ce4618b60bd35c Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 07:20:09 +0900 Subject: [PATCH 28/36] feat: add config Cache::$configCacheEnabled and move code from index.php to Boot class. --- app/Config/Cache.php | 9 +++++++ public/index.php | 39 +--------------------------- system/Boot.php | 61 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 40 deletions(-) diff --git a/app/Config/Cache.php b/app/Config/Cache.php index b29c13a9ea71..fb63945a2a90 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -168,4 +168,13 @@ class Cache extends BaseConfig 'redis' => RedisHandler::class, 'wincache' => WincacheHandler::class, ]; + + /** + * -------------------------------------------------------------------------- + * Config Caching + * -------------------------------------------------------------------------- + * + * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching + */ + public bool $configCacheEnabled = false; } diff --git a/public/index.php b/public/index.php index 057ec8a111c3..ab53b3d009d9 100644 --- a/public/index.php +++ b/public/index.php @@ -49,42 +49,5 @@ // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -CodeIgniter\Boot::BootWeb($paths); -// Load Config Cache -// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); -// $factoriesCache->load('config'); -// ^^^ Uncomment these lines if you want to use Config Caching. - -/* - * --------------------------------------------------------------- - * GRAB OUR CODEIGNITER INSTANCE - * --------------------------------------------------------------- - * - * 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. - */ - -$app = Config\Services::codeigniter(); -$app->initialize(); -$context = is_cli() ? 'php-cli' : 'web'; -$app->setContext($context); - -/* - *--------------------------------------------------------------- - * LAUNCH THE APPLICATION - *--------------------------------------------------------------- - * Now that everything is set up, it's time to actually fire - * up the engines and make this app do its thang. - */ - -$app->run(); - -// Save Config Cache -// $factoriesCache->save('config'); -// ^^^ Uncomment this line if you want to use Config Caching. - -// Exits the application, setting the exit code for CLI-based applications -// that might be watching. -exit(EXIT_SUCCESS); +exit(CodeIgniter\Boot::BootWeb($paths)); diff --git a/system/Boot.php b/system/Boot.php index 0001db6ba051..57e04e2d4e9d 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -13,9 +13,11 @@ namespace CodeIgniter; +use CodeIgniter\Cache\FactoriesCache; use CodeIgniter\Config\DotEnv; use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; +use Config\Cache; use Config\Modules; use Config\Paths; use Config\Services; @@ -30,11 +32,13 @@ class Boot /** * @used-by public/index.php * - * Context + * 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): void + public static function bootWeb(Paths $paths): int { static::loadDotEnv($paths); static::defineEnvironment(); @@ -48,6 +52,22 @@ public static function bootWeb(Paths $paths): void static::setExceptionHandler(); static::checkMissingExtensions(); static::initializeKint(); + + $configCacheEnabled = (new Cache())->configCacheEnabled ?? false; + if ($configCacheEnabled) { + $factoriesCache = static::loadConfigCache(); + } + + $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; } /** @@ -226,4 +246,41 @@ protected static function initializeKint(): void { Services::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 = Config\Services::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'); + } } From 2246d8f00744d174f1533ae235a97e9aeddf3a35 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 07:55:26 +0900 Subject: [PATCH 29/36] refactor: move code from spark to Boot class --- spark | 38 +------------------------------------- system/Boot.php | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/spark b/spark index a1b42e35b0b7..623b6df1093b 100755 --- a/spark +++ b/spark @@ -80,41 +80,5 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -CodeIgniter\Boot::BootSpark($paths); -/* - * --------------------------------------------------------------- - * GRAB OUR CODEIGNITER INSTANCE - * --------------------------------------------------------------- - */ - -$app = Config\Services::codeigniter(); -$app->initialize(); - -/* - * --------------------------------------------------------------- - * GRAB OUR CONSOLE - * --------------------------------------------------------------- - */ - -$console = new CodeIgniter\CLI\Console(); - -// SHOW HEADER -// Show basic information before we do anything else. -if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { - unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore - $suppress = true; -} - -$console->showHeader($suppress); - -/* - *--------------------------------------------------------------- - * EXECUTE THE COMMAND - *--------------------------------------------------------------- - */ - -// fire off the command in the main framework. -$exit = $console->run(); - -exit(is_int($exit) ? $exit : EXIT_SUCCESS); +exit(CodeIgniter\Boot::BootSpark($paths)); diff --git a/system/Boot.php b/system/Boot.php index 57e04e2d4e9d..849828ef1e26 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -14,6 +14,7 @@ namespace CodeIgniter; use CodeIgniter\Cache\FactoriesCache; +use CodeIgniter\CLI\Console; use CodeIgniter\Config\DotEnv; use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; @@ -72,8 +73,10 @@ public static function bootWeb(Paths $paths): int /** * @used-by spark + * + * @return int Exit code. */ - public static function bootSpark(Paths $paths): void + public static function bootSpark(Paths $paths): int { static::loadDotEnv($paths); static::defineEnvironment(); @@ -87,6 +90,11 @@ public static function bootSpark(Paths $paths): void static::setExceptionHandler(); static::checkMissingExtensions(); static::initializeKint(); + + static::initializeCodeIgniter(); + $console = static::initializeConsole(); + + return static::runCommand($console); } /** @@ -283,4 +291,27 @@ 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; + } } From 2d59d4263c738a1c9a9a8683326719c17b6f765f Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 08:08:21 +0900 Subject: [PATCH 30/36] docs: update "How to Enable Config Caching" --- user_guide_src/source/concepts/factories.rst | 55 +++++++++++--------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index f4872327f082..7d7133afcf1f 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -316,27 +316,34 @@ Or simply delete the **writable/cache/FactoriesCache_config** file. How to Enable Config Caching ============================ -Uncomment the following code in **public/index.php**:: - - --- a/public/index.php - +++ b/public/index.php - @@ -49,8 +49,8 @@ if (! defined('ENVIRONMENT')) { - } - - // Load Config Cache - -// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); - -// $factoriesCache->load('config'); - +$factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); - +$factoriesCache->load('config'); - // ^^^ Uncomment these lines if you want to use Config Caching. - - /* - @@ -79,7 +79,7 @@ $app->setContext($context); - $app->run(); - - // Save Config Cache - -// $factoriesCache->save('config'); - +$factoriesCache->save('config'); - // ^^^ Uncomment this line if you want to use Config Caching. - - // Exits the application, setting the exit code for CLI-based applications +.. versionadded:: 4.5.0 + +Set the following property to ``true`` in **app/Config/Cache.php**:: + + public bool $configCacheEnabled = true; + +.. note:: + Prior to v4.5.0, uncomment the following code in **public/index.php**:: + + --- a/public/index.php + +++ b/public/index.php + @@ -49,8 +49,8 @@ if (! defined('ENVIRONMENT')) { + } + + // Load Config Cache + -// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); + -// $factoriesCache->load('config'); + +$factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); + +$factoriesCache->load('config'); + // ^^^ Uncomment these lines if you want to use Config Caching. + + /* + @@ -79,7 +79,7 @@ $app->setContext($context); + $app->run(); + + // Save Config Cache + -// $factoriesCache->save('config'); + +$factoriesCache->save('config'); + // ^^^ Uncomment this line if you want to use Config Caching. + + // Exits the application, setting the exit code for CLI-based applications From a50f172e166548636d4a040125b25a4375ed58af Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 08:17:16 +0900 Subject: [PATCH 31/36] config: move $cacheQueryString down This setting is for Web Page Caching, not for Cache service. --- app/Config/Cache.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/app/Config/Cache.php b/app/Config/Cache.php index fb63945a2a90..6c61b0de1b56 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -46,25 +46,6 @@ class Cache extends BaseConfig */ public string $storePath = WRITEPATH . 'cache/'; - /** - * -------------------------------------------------------------------------- - * Cache Include Query String - * -------------------------------------------------------------------------- - * - * Whether to take the URL query string into consideration when generating - * output cache files. Valid options are: - * - * false = Disabled - * true = Enabled, take all query parameters into account. - * Please be aware that this may result in numerous cache - * files generated for the same page over and over again. - * ['q'] = Enabled, but only take into account the specified list - * of query parameters. - * - * @var bool|list - */ - public $cacheQueryString = false; - /** * -------------------------------------------------------------------------- * Key Prefix @@ -169,6 +150,25 @@ class Cache extends BaseConfig 'wincache' => WincacheHandler::class, ]; + /** + * -------------------------------------------------------------------------- + * Web Page Caching: Cache Include Query String + * -------------------------------------------------------------------------- + * + * Whether to take the URL query string into consideration when generating + * output cache files. Valid options are: + * + * false = Disabled + * true = Enabled, take all query parameters into account. + * Please be aware that this may result in numerous cache + * files generated for the same page over and over again. + * ['q'] = Enabled, but only take into account the specified list + * of query parameters. + * + * @var bool|list + */ + public $cacheQueryString = false; + /** * -------------------------------------------------------------------------- * Config Caching From 78c11b6d56c680a2e7f287df76802670e0f1837d Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 09:12:23 +0900 Subject: [PATCH 32/36] fix: extract autoloadHelpers() --- system/Boot.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/system/Boot.php b/system/Boot.php index 849828ef1e26..67721910fbaa 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -59,6 +59,8 @@ public static function bootWeb(Paths $paths): int $factoriesCache = static::loadConfigCache(); } + static::autoloadHelpers(); + $app = static::initializeCodeIgniter(); static::runCodeIgniter($app); @@ -90,6 +92,7 @@ public static function bootSpark(Paths $paths): int static::setExceptionHandler(); static::checkMissingExtensions(); static::initializeKint(); + static::autoloadHelpers(); static::initializeCodeIgniter(); $console = static::initializeConsole(); @@ -110,6 +113,7 @@ public static function bootTest(Paths $paths): void static::setExceptionHandler(); static::checkMissingExtensions(); static::initializeKint(); + static::autoloadHelpers(); } /** @@ -218,6 +222,10 @@ protected static function loadAutoloader(): void // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); + } + + protected static function autoloadHelpers(): void + { Services::autoloader()->loadHelpers(); } From 555ab4a0d64d33e1b4e11b9d84ec0c139fdede24 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 11:06:08 +0900 Subject: [PATCH 33/36] fix: method name case --- public/index.php | 2 +- spark | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index ab53b3d009d9..f1f678a0a211 100644 --- a/public/index.php +++ b/public/index.php @@ -50,4 +50,4 @@ // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -exit(CodeIgniter\Boot::BootWeb($paths)); +exit(CodeIgniter\Boot::bootWeb($paths)); diff --git a/spark b/spark index 623b6df1093b..a56fbc1bd7b6 100755 --- a/spark +++ b/spark @@ -81,4 +81,4 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -exit(CodeIgniter\Boot::BootSpark($paths)); +exit(CodeIgniter\Boot::bootSpark($paths)); From 4e7157645cda2ac3bd9f738db4063ba40b5cdd43 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 29 Feb 2024 09:04:23 +0900 Subject: [PATCH 34/36] perf: add config Optimize and move $configCacheEnabled The Cache Config class that extends BaseConfig is very slow to instantiate.. See https://github.com/codeigniter4/CodeIgniter4/pull/8558#discussion_r1506813270 --- app/Config/Cache.php | 9 -------- app/Config/Optimize.php | 23 ++++++++++++++++++++ system/Boot.php | 5 +++-- user_guide_src/source/concepts/factories.rst | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 app/Config/Optimize.php diff --git a/app/Config/Cache.php b/app/Config/Cache.php index 6c61b0de1b56..3fbade6840cc 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -168,13 +168,4 @@ class Cache extends BaseConfig * @var bool|list */ public $cacheQueryString = false; - - /** - * -------------------------------------------------------------------------- - * Config Caching - * -------------------------------------------------------------------------- - * - * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching - */ - public bool $configCacheEnabled = false; } diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php new file mode 100644 index 000000000000..7895f16b3093 --- /dev/null +++ b/app/Config/Optimize.php @@ -0,0 +1,23 @@ +configCacheEnabled ?? false; + $configCacheEnabled = class_exists(Optimize::class) + && (new Optimize())->configCacheEnabled; if ($configCacheEnabled) { $factoriesCache = static::loadConfigCache(); } diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index 7d7133afcf1f..094e2a9d0b41 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -318,7 +318,7 @@ How to Enable Config Caching .. versionadded:: 4.5.0 -Set the following property to ``true`` in **app/Config/Cache.php**:: +Set the following property to ``true`` in **app/Config/Optimize.php**:: public bool $configCacheEnabled = true; From d538a0812823483c2f6bd84167bdd7c4bf93e234 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 29 Feb 2024 09:29:05 +0900 Subject: [PATCH 35/36] feat: add Config\Optimize::$locatorCacheEnabled --- app/Config/Optimize.php | 9 +++++++ system/Config/BaseService.php | 11 +++++++- user_guide_src/source/concepts/autoloader.rst | 4 +-- .../source/concepts/autoloader/004.php | 25 ------------------- 4 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 user_guide_src/source/concepts/autoloader/004.php diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php index 7895f16b3093..6fb441fd2288 100644 --- a/app/Config/Optimize.php +++ b/app/Config/Optimize.php @@ -20,4 +20,13 @@ class Optimize * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching */ public bool $configCacheEnabled = false; + + /** + * -------------------------------------------------------------------------- + * Config Caching + * -------------------------------------------------------------------------- + * + * @see https://codeigniter.com/user_guide/concepts/autoloader.html#file-locator-caching + */ + public bool $locatorCacheEnabled = false; } diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index 588f66534949..dec0ac2020d8 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -15,6 +15,7 @@ use CodeIgniter\Autoloader\Autoloader; use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\Autoloader\FileLocatorInterface; use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Cache\ResponseCache; @@ -71,6 +72,7 @@ use Config\Images; use Config\Migrations; use Config\Modules; +use Config\Optimize; use Config\Pager as ConfigPager; use Config\Services as AppServices; use Config\Toolbar as ConfigToolbar; @@ -235,7 +237,14 @@ public static function locator(bool $getShared = true) { if ($getShared) { if (empty(static::$instances['locator'])) { - static::$instances['locator'] = new FileLocator(static::autoloader()); + $cacheEnabled = class_exists(Optimize::class) + && (new Optimize())->locatorCacheEnabled; + + if ($cacheEnabled) { + static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader())); + } else { + static::$instances['locator'] = new FileLocator(static::autoloader()); + } } return static::$mocks['locator'] ?? static::$instances['locator']; diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 7328dd00c0aa..3bbd5f8d1fcd 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -190,6 +190,6 @@ Or simply delete the **writable/cache/FileLocatorCache** file. How to Enable FileLocator Caching ================================= -Add the following code in **app/Config/Services.php**: +Set the following property to ``true`` in **app/Config/Optimize.php**:: -.. literalinclude:: autoloader/004.php + public bool $locatorCacheEnabled = true; diff --git a/user_guide_src/source/concepts/autoloader/004.php b/user_guide_src/source/concepts/autoloader/004.php deleted file mode 100644 index 090149e6486c..000000000000 --- a/user_guide_src/source/concepts/autoloader/004.php +++ /dev/null @@ -1,25 +0,0 @@ - Date: Thu, 29 Feb 2024 09:36:51 +0900 Subject: [PATCH 36/36] docs: add notes for environment variables --- user_guide_src/source/concepts/autoloader.rst | 4 ++++ user_guide_src/source/concepts/factories.rst | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 3bbd5f8d1fcd..26ce27a673be 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -193,3 +193,7 @@ How to Enable FileLocator Caching Set the following property to ``true`` in **app/Config/Optimize.php**:: public bool $locatorCacheEnabled = true; + +.. note:: + This property cannot be overridden by + :ref:`environment variables `. diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index 094e2a9d0b41..b544baa393b2 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -322,6 +322,10 @@ Set the following property to ``true`` in **app/Config/Optimize.php**:: public bool $configCacheEnabled = true; +.. note:: + This property cannot be overridden by + :ref:`environment variables `. + .. note:: Prior to v4.5.0, uncomment the following code in **public/index.php**::