Skip to content

Commit

Permalink
Merge pull request #5972 from kenjis/refactor-bootstrap
Browse files Browse the repository at this point in the history
refactor: `system/bootstrap.php` only loads files and registers autoloader
  • Loading branch information
kenjis authored May 17, 2022
2 parents 7c30113 + d3baa55 commit 8ddc566
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
33 changes: 24 additions & 9 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,21 +15,32 @@
* and fires up an environment-specific bootstrapping.
*/

// Ensure the current directory is pointing to the front controller's directory
chdir(__DIR__);

// 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';
// ^^^ Change this if you move your application folder
require realpath($pathsConfig) ?: $pathsConfig;
require FCPATH . '../app/Config/Paths.php';
// ^^^ Change this line if you move your application folder

$paths = new Config\Paths();

// Location of the framework bootstrap file.
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
/** @var CodeIgniter\CodeIgniter $app */
$app = require realpath($bootstrap) ?: $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';
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();

/*
* ---------------------------------------------------------------
* 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 = Config\Services::codeigniter();
$app->initialize();
$context = is_cli() ? 'php-cli' : 'web';
$app->setContext($context);

Expand All @@ -37,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();
45 changes: 26 additions & 19 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@
* 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");
}

// We want errors to be shown when using it from the CLI.
error_reporting(-1);
ini_set('display_errors', '1');

/**
* @var bool
*
* @deprecated No longer in use. `CodeIgniter` has `$context` property.
*/
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
Expand All @@ -37,36 +52,28 @@ define('SPARKED', true);
* and fires up an environment-specific bootstrapping.
*/

// 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.
require FCPATH . '../app/Config/Paths.php';
// ^^^ Change this line if you move your application folder
require realpath($pathsConfig) ?: $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.
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
/** @var CodeIgniter\CodeIgniter $app */
$app = require realpath($bootstrap) ?: $bootstrap;
// Load environment settings from .env files into $_SERVER and $_ENV
require_once SYSTEMPATH . 'Config/DotEnv.php';
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();

// Grab our CodeIgniter
$app = Config\Services::codeigniter();
$app->initialize();
$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
Expand Down
23 changes: 1 addition & 22 deletions system/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +66,7 @@
* GRAB OUR CONSTANTS & COMMON
* ---------------------------------------------------------------
*/

if (! defined('APP_NAMESPACE')) {
require_once APPPATH . 'Config/Constants.php';
}
Expand Down Expand Up @@ -123,26 +123,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');

/*
* ---------------------------------------------------------------
* 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;
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`` file has been modified to easily implement `Preloading <https://www.php.net/manual/en/opcache.preloading.php>`_. 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() <cli-library-color>` has been changed to fix a bug.
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/installation/upgrade_420.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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. This has been changed to make `Preloading <https://www.php.net/manual/en/opcache.preloading.php>`_ easier to implement.

Breaking Enhancements
*********************
Expand Down

0 comments on commit 8ddc566

Please sign in to comment.