From f26943cfd8bcbad4211e5da201c66e3459498044 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 11:56:11 +0900 Subject: [PATCH 01/11] refactor: move CodeIgniter instantiation from system/bootstrap.php --- public/index.php | 19 +++++++++++++++++-- spark | 10 ++++++++-- system/bootstrap.php | 15 --------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/public/index.php b/public/index.php index f1c2f7e1e72f..65dbf4880be5 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,7 @@ systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; -/** @var CodeIgniter\CodeIgniter $app */ -$app = require realpath($bootstrap) ?: $bootstrap; + +require realpath($bootstrap) ?: $bootstrap; + +/* + * --------------------------------------------------------------- + * GRAB OUR CODEIGNITER INSTANCE + * --------------------------------------------------------------- + * + * The CodeIgniter class contains the core functionality to make + * the application run, and does all of the dirty work to get + * the pieces all working together. + */ +$app = Services::codeigniter(); +$app->initialize(); $context = is_cli() ? 'php-cli' : 'web'; $app->setContext($context); diff --git a/spark b/spark index 7d8b8eb64557..80c4cc166bd1 100755 --- a/spark +++ b/spark @@ -21,6 +21,8 @@ * this class mainly acts as a passthru to the framework itself. */ +use Config\Services; + /** * @var bool * @@ -48,6 +50,7 @@ define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR) // Load our paths config file $pathsConfig = 'app/Config/Paths.php'; // ^^^ Change this line if you move your application folder + require realpath($pathsConfig) ?: $pathsConfig; $paths = new Config\Paths(); @@ -56,8 +59,11 @@ $paths = new Config\Paths(); chdir(FCPATH); $bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; -/** @var CodeIgniter\CodeIgniter $app */ -$app = require realpath($bootstrap) ?: $bootstrap; + +require realpath($bootstrap) ?: $bootstrap; + +$app = Services::codeigniter(); +$app->initialize(); $app->setContext('spark'); // Grab our Console diff --git a/system/bootstrap.php b/system/bootstrap.php index 4549fbc2c939..15566118cd51 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -131,18 +131,3 @@ class_alias('Config\Services', 'CodeIgniter\Services'); // Always load the URL helper, it should be used in most of apps. helper('url'); - -/* - * --------------------------------------------------------------- - * GRAB OUR CODEIGNITER INSTANCE - * --------------------------------------------------------------- - * - * The CodeIgniter class contains the core functionality to make - * the application run, and does all of the dirty work to get - * the pieces all working together. - */ - -$app = Services::codeigniter(); -$app->initialize(); - -return $app; From 1adabfe043d2609be2f345f4f25b4136d308a4eb Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 12:51:32 +0900 Subject: [PATCH 02/11] refactor: remove realpath() No need to use it. --- public/index.php | 4 ++-- spark | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/index.php b/public/index.php index 65dbf4880be5..2a67ce232b58 100644 --- a/public/index.php +++ b/public/index.php @@ -22,14 +22,14 @@ $pathsConfig = FCPATH . '../app/Config/Paths.php'; // ^^^ Change this if you move your application folder -require realpath($pathsConfig) ?: $pathsConfig; +require $pathsConfig; $paths = new Config\Paths(); // Location of the framework bootstrap file. $bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; -require realpath($bootstrap) ?: $bootstrap; +require $bootstrap; /* * --------------------------------------------------------------- diff --git a/spark b/spark index 80c4cc166bd1..3852706cac8c 100755 --- a/spark +++ b/spark @@ -51,7 +51,7 @@ define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR) $pathsConfig = 'app/Config/Paths.php'; // ^^^ Change this line if you move your application folder -require realpath($pathsConfig) ?: $pathsConfig; +require $pathsConfig; $paths = new Config\Paths(); @@ -60,7 +60,7 @@ chdir(FCPATH); $bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; -require realpath($bootstrap) ?: $bootstrap; +require $bootstrap; $app = Services::codeigniter(); $app->initialize(); From b6a27dc5127801df9c512be16fa4c6bb8d57c3e6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 13:04:29 +0900 Subject: [PATCH 03/11] chore: tweak the files so that their contents are as same as possible. --- public/index.php | 10 ++++------ spark | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/public/index.php b/public/index.php index 2a67ce232b58..f385a8367293 100644 --- a/public/index.php +++ b/public/index.php @@ -1,7 +1,5 @@ initialize(); $context = is_cli() ? 'php-cli' : 'web'; $app->setContext($context); diff --git a/spark b/spark index 3852706cac8c..7624023fa121 100755 --- a/spark +++ b/spark @@ -21,8 +21,6 @@ * this class mainly acts as a passthru to the framework itself. */ -use Config\Services; - /** * @var bool * @@ -30,6 +28,9 @@ use Config\Services; */ define('SPARKED', true); +// Path to the front controller +define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); + /* *--------------------------------------------------------------- * BOOTSTRAP THE APPLICATION @@ -39,30 +40,29 @@ define('SPARKED', true); * and fires up an environment-specific bootstrapping. */ +// Ensure the current directory is pointing to the front controller's directory +chdir(FCPATH); + // Refuse to run when called from php-cgi 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"); } -// Path to the front controller -define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); - -// Load our paths config file -$pathsConfig = 'app/Config/Paths.php'; +// This is the line that might need to be changed, depending on your folder structure. +$pathsConfig = FCPATH . '../app/Config/Paths.php'; // ^^^ Change this line if you move your application folder +// Load our paths config file require $pathsConfig; $paths = new Config\Paths(); -// Ensure the current directory is pointing to the front controller's directory -chdir(FCPATH); - +// Location of the framework bootstrap file. $bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; require $bootstrap; -$app = Services::codeigniter(); +$app = Config\Services::codeigniter(); $app->initialize(); $app->setContext('spark'); From e2e94e8f0db1aad21196243e1bec253e1eae1572 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 13:18:24 +0900 Subject: [PATCH 04/11] refactor: move DotEnv::load() from system/bootstrap.php --- public/index.php | 6 ++++++ spark | 6 ++++++ system/bootstrap.php | 7 ------- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/public/index.php b/public/index.php index f385a8367293..973433c70882 100644 --- a/public/index.php +++ b/public/index.php @@ -29,6 +29,12 @@ require $bootstrap; +// Load environment settings from .env files into $_SERVER and $_ENV +require_once SYSTEMPATH . 'Config/DotEnv.php'; + +$env = new CodeIgniter\Config\DotEnv(ROOTPATH); +$env->load(); + /* * --------------------------------------------------------------- * GRAB OUR CODEIGNITER INSTANCE diff --git a/spark b/spark index 7624023fa121..7edc4c4f4992 100755 --- a/spark +++ b/spark @@ -62,6 +62,12 @@ $bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'boo require $bootstrap; +// Load environment settings from .env files into $_SERVER and $_ENV +require_once SYSTEMPATH . 'Config/DotEnv.php'; + +$env = new CodeIgniter\Config\DotEnv(ROOTPATH); +$env->load(); + $app = Config\Services::codeigniter(); $app->initialize(); $app->setContext('spark'); diff --git a/system/bootstrap.php b/system/bootstrap.php index 15566118cd51..7fae1a2a417e 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -9,7 +9,6 @@ * the LICENSE file that was distributed with this source code. */ -use CodeIgniter\Config\DotEnv; use Config\Autoload; use Config\Modules; use Config\Paths; @@ -123,11 +122,5 @@ class_alias('Config\Services', 'CodeIgniter\Services'); 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(); - // Always load the URL helper, it should be used in most of apps. helper('url'); From 1f6761b96c5b83dad9e4ad8b0269317bc2e578f1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 13:20:13 +0900 Subject: [PATCH 05/11] refactor: move SAPI check up --- spark | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spark b/spark index 7edc4c4f4992..63c3de77e04e 100755 --- a/spark +++ b/spark @@ -21,6 +21,11 @@ * this class mainly acts as a passthru to the framework itself. */ +// Refuse to run when called from php-cgi +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"); +} + /** * @var bool * @@ -43,11 +48,6 @@ define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR) // Ensure the current directory is pointing to the front controller's directory chdir(FCPATH); -// Refuse to run when called from php-cgi -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"); -} - // This is the line that might need to be changed, depending on your folder structure. $pathsConfig = FCPATH . '../app/Config/Paths.php'; // ^^^ Change this line if you move your application folder From 788072724e659660b6a71bd03f3d3e51c9496198 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 13:24:48 +0900 Subject: [PATCH 06/11] refactor: move error reporting config up --- spark | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spark b/spark index 63c3de77e04e..cb1ee1bd65c9 100755 --- a/spark +++ b/spark @@ -26,6 +26,10 @@ 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"); } +// We want errors to be shown when using it from the CLI. +error_reporting(-1); +ini_set('display_errors', '1'); + /** * @var bool * @@ -75,10 +79,6 @@ $app->setContext('spark'); // Grab our Console $console = new CodeIgniter\CLI\Console($app); -// We want errors to be shown when using it from the CLI. -error_reporting(-1); -ini_set('display_errors', '1'); - // Show basic information before we do anything else. if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore From a2a9b6c11886735d2fd1ca5798a0ad6a43582b59 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 7 May 2022 13:51:40 +0900 Subject: [PATCH 07/11] refactor: remove variables --- public/index.php | 14 ++++---------- spark | 14 ++++---------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/public/index.php b/public/index.php index 973433c70882..33601ff9b1b1 100644 --- a/public/index.php +++ b/public/index.php @@ -15,25 +15,19 @@ // Ensure the current directory is pointing to the front controller's directory chdir(FCPATH); +// Load our paths config file // This is the line that might need to be changed, depending on your folder structure. -$pathsConfig = FCPATH . '../app/Config/Paths.php'; +require FCPATH . '../app/Config/Paths.php'; // ^^^ Change this line if you move your application folder -// Load our paths config file -require $pathsConfig; - $paths = new Config\Paths(); // Location of the framework bootstrap file. -$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - -require $bootstrap; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; // Load environment settings from .env files into $_SERVER and $_ENV require_once SYSTEMPATH . 'Config/DotEnv.php'; - -$env = new CodeIgniter\Config\DotEnv(ROOTPATH); -$env->load(); +(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); /* * --------------------------------------------------------------- diff --git a/spark b/spark index cb1ee1bd65c9..fd453e84ce25 100755 --- a/spark +++ b/spark @@ -52,25 +52,19 @@ define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR) // Ensure the current directory is pointing to the front controller's directory chdir(FCPATH); +// Load our paths config file // This is the line that might need to be changed, depending on your folder structure. -$pathsConfig = FCPATH . '../app/Config/Paths.php'; +require FCPATH . '../app/Config/Paths.php'; // ^^^ Change this line if you move your application folder -// Load our paths config file -require $pathsConfig; - $paths = new Config\Paths(); // Location of the framework bootstrap file. -$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - -require $bootstrap; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; // Load environment settings from .env files into $_SERVER and $_ENV require_once SYSTEMPATH . 'Config/DotEnv.php'; - -$env = new CodeIgniter\Config\DotEnv(ROOTPATH); -$env->load(); +(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); $app = Config\Services::codeigniter(); $app->initialize(); From b2ed452f6575836328bdd7b4f04810805844aee5 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 May 2022 07:30:48 +0900 Subject: [PATCH 08/11] refactor: move chdir() up --- public/index.php | 8 +++++--- spark | 7 ++++--- system/bootstrap.php | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/public/index.php b/public/index.php index 33601ff9b1b1..51f4be81e731 100644 --- a/public/index.php +++ b/public/index.php @@ -3,6 +3,9 @@ // Path to the front controller (this file) define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR); +// Ensure the current directory is pointing to the front controller's directory +chdir(FCPATH); + /* *--------------------------------------------------------------- * BOOTSTRAP THE APPLICATION @@ -12,9 +15,6 @@ * and fires up an environment-specific bootstrapping. */ -// Ensure the current directory is pointing to the front controller's directory -chdir(FCPATH); - // 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'; @@ -38,6 +38,7 @@ * the application run, and does all of the dirty work to get * the pieces all working together. */ + $app = Config\Services::codeigniter(); $app->initialize(); $context = is_cli() ? 'php-cli' : 'web'; @@ -50,4 +51,5 @@ * Now that everything is setup, it's time to actually fire * up the engines and make this app do its thang. */ + $app->run(); diff --git a/spark b/spark index fd453e84ce25..225422aace74 100755 --- a/spark +++ b/spark @@ -40,6 +40,9 @@ define('SPARKED', true); // Path to the front controller define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR); +// Ensure the current directory is pointing to the front controller's directory +chdir(FCPATH); + /* *--------------------------------------------------------------- * BOOTSTRAP THE APPLICATION @@ -49,9 +52,6 @@ define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR) * and fires up an environment-specific bootstrapping. */ -// Ensure the current directory is pointing to the front controller's directory -chdir(FCPATH); - // 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'; @@ -66,6 +66,7 @@ require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstra require_once SYSTEMPATH . 'Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); +// Grab our CodeIgniter $app = Config\Services::codeigniter(); $app->initialize(); $app->setContext('spark'); diff --git a/system/bootstrap.php b/system/bootstrap.php index 7fae1a2a417e..fec0e5c14b02 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -66,6 +66,7 @@ * GRAB OUR CONSTANTS & COMMON * --------------------------------------------------------------- */ + if (! defined('APP_NAMESPACE')) { require_once APPPATH . 'Config/Constants.php'; } From 6545d0dd5f18e2d16b33ac8f7f6b0774de8a382a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 8 May 2022 08:42:39 +0900 Subject: [PATCH 09/11] docs: add user guide --- user_guide_src/source/changelogs/v4.2.0.rst | 1 + user_guide_src/source/installation/upgrade_420.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index a75a88281025..93c81004522c 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -16,6 +16,7 @@ BREAKING - The ``CodeIgniter\CodeIgniter`` class has a new property ``$context`` and it must have the correct context at runtime. So the following files have been changed: - ``public/index.php`` - ``spark`` +- The ``system/bootstrap.php`` no longer returns a ``CodeIgniter`` instance, and does not load ``.env`` file. - The method signature of ``CodeIgniter\CLI\CommandRunner::_remap()`` has been changed to fix a bug. - The ``CodeIgniter\Autoloader\Autoloader::initialize()`` has changed the behavior to fix a bug. It used to use Composer classmap only when ``$modules->discoverInComposer`` is true. Now it always uses the Composer classmap if Composer is available. - The color code output by :ref:`CLI::color() ` has been changed to fix a bug. diff --git a/user_guide_src/source/installation/upgrade_420.rst b/user_guide_src/source/installation/upgrade_420.rst index daf89b39290f..7b44f979e1c1 100644 --- a/user_guide_src/source/installation/upgrade_420.rst +++ b/user_guide_src/source/installation/upgrade_420.rst @@ -24,7 +24,7 @@ The following files received significant changes and Breaking Changes **************** - +- The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load ``.env`` file. The ``index.php`` and ``spark`` now do them. If you have code that expects these behaviors, it will no longer work and must be modified. Breaking Enhancements ********************* From caea4b6cba77d001933cf0bbcdb5d5aff1016a36 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 10 May 2022 21:53:22 +0900 Subject: [PATCH 10/11] docs: fix by proofreading Co-authored-by: MGatner --- user_guide_src/source/installation/upgrade_420.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/installation/upgrade_420.rst b/user_guide_src/source/installation/upgrade_420.rst index 7b44f979e1c1..dc77c28ed85e 100644 --- a/user_guide_src/source/installation/upgrade_420.rst +++ b/user_guide_src/source/installation/upgrade_420.rst @@ -24,7 +24,7 @@ The following files received significant changes and Breaking Changes **************** -- The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load ``.env`` file. The ``index.php`` and ``spark`` now do them. If you have code that expects these behaviors, it will no longer work and must be modified. +- The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load the ``.env`` file (now handled in ``index.php`` and ``spark``). If you have code that expects these behaviors it will no longer work and must be modified. Breaking Enhancements ********************* From d3baa55fc4c4fc546ed5610361d51d032fe036bb Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 17 May 2022 15:00:04 +0900 Subject: [PATCH 11/11] docs: add why or so --- user_guide_src/source/changelogs/v4.2.0.rst | 2 +- user_guide_src/source/installation/upgrade_420.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.2.0.rst b/user_guide_src/source/changelogs/v4.2.0.rst index 93c81004522c..c5f25f1b8396 100644 --- a/user_guide_src/source/changelogs/v4.2.0.rst +++ b/user_guide_src/source/changelogs/v4.2.0.rst @@ -16,7 +16,7 @@ BREAKING - The ``CodeIgniter\CodeIgniter`` class has a new property ``$context`` and it must have the correct context at runtime. So the following files have been changed: - ``public/index.php`` - ``spark`` -- The ``system/bootstrap.php`` no longer returns a ``CodeIgniter`` instance, and does not load ``.env`` file. +- The ``system/bootstrap.php`` file has been modified to easily implement `Preloading `_. Returning a ``CodeIgniter`` instance and loading ``.env`` file have been moved to ``index.php`` and ``spark``. - The method signature of ``CodeIgniter\CLI\CommandRunner::_remap()`` has been changed to fix a bug. - The ``CodeIgniter\Autoloader\Autoloader::initialize()`` has changed the behavior to fix a bug. It used to use Composer classmap only when ``$modules->discoverInComposer`` is true. Now it always uses the Composer classmap if Composer is available. - The color code output by :ref:`CLI::color() ` has been changed to fix a bug. diff --git a/user_guide_src/source/installation/upgrade_420.rst b/user_guide_src/source/installation/upgrade_420.rst index dc77c28ed85e..4e39cb3aee51 100644 --- a/user_guide_src/source/installation/upgrade_420.rst +++ b/user_guide_src/source/installation/upgrade_420.rst @@ -24,7 +24,7 @@ The following files received significant changes and Breaking Changes **************** -- The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load the ``.env`` file (now handled in ``index.php`` and ``spark``). If you have code that expects these behaviors it will no longer work and must be modified. +- The ``system/bootstrap.php`` file no longer returns a ``CodeIgniter`` instance, and does not load the ``.env`` file (now handled in ``index.php`` and ``spark``). If you have code that expects these behaviors it will no longer work and must be modified. This has been changed to make `Preloading `_ easier to implement. Breaking Enhancements *********************