Skip to content

fix: move Kint loading to Autoloader #8603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
28a8009
refactor: move CodeIgniter::initializeKint() to Autoloader
kenjis Feb 18, 2024
470dace
docs: remove unneeded @var
kenjis Feb 18, 2024
4e602a6
refactor: move CodeIgniter::bootstrapEnvironment() to bootstrap.php
kenjis Feb 18, 2024
85adb5b
docs: remove meaningless @codeCoverageIgnore
kenjis Feb 18, 2024
c157590
docs: add comment headings
kenjis Feb 18, 2024
e670df1
refactor: move DEFINE ENVIRONMENT to bootstrap.php
kenjis Feb 18, 2024
66c81bc
refactor: move Services::exceptions()->initialize() to bootstrap.php
kenjis Feb 18, 2024
83c15b9
refactor: move CodeIgniter::resolvePlatformExtensions() to bootstrap.php
kenjis Feb 18, 2024
fb00173
refactor: remove variables
kenjis Feb 18, 2024
a87af65
refactor: match test/bootstrap to system/bootstrap as much as possible
kenjis Feb 18, 2024
3c8b1c3
refactor: remove `$_SERVER['app.baseURL'] = 'http://example.com/'`
kenjis Feb 18, 2024
a1c1be2
refactor: remove unneeded `require_once COMPOSER_PATH`
kenjis Feb 18, 2024
ff6a2d5
fix: move LOAD ENVIRONMENT BOOTSTRAP to index.php
kenjis Feb 18, 2024
3f55c98
fix: move BOOTSTRAP THE APPLICATION down
kenjis Feb 18, 2024
a3bccfb
fix: add missing LOAD ENVIRONMENT BOOTSTRAP
kenjis Feb 19, 2024
10a60e0
fix: index.php and spark do not work
kenjis Feb 19, 2024
793b57a
fix: Undefined variable $paths
kenjis Feb 19, 2024
48307b5
chore: add exlucde for PHPCPD
kenjis Feb 19, 2024
46230e7
docs: add user guide
kenjis Mar 3, 2024
3dba2bc
docs: fix by proofreading
kenjis Mar 15, 2024
e75e128
docs: fix by proofreading
kenjis Mar 15, 2024
458333c
docs: add Bootstrap change to changelog
kenjis Mar 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-phpcpd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
42 changes: 33 additions & 9 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

// Check PHP version.
/*
*---------------------------------------------------------------
* CHECK PHP VERSION
*---------------------------------------------------------------
*/

$minPhpVersion = '8.1'; // If you update this, don't forget to update `spark`.
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
$message = sprintf(
Expand All @@ -12,6 +17,12 @@
exit($message);
}

/*
*---------------------------------------------------------------
* SET THE CURRENT DIRECTORY
*---------------------------------------------------------------
*/

// Path to the front controller (this file)
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);

Expand All @@ -29,25 +40,38 @@
* 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();

// Location of the framework bootstrap file.
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

// LOAD DOTENV FILE
// 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
// 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);
}

// 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 . 'bootstrap.php';

// Load Config Cache
// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();
// $factoriesCache->load('config');
Expand Down
65 changes: 53 additions & 12 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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);

Expand All @@ -57,32 +68,56 @@ 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();

// Location of the framework bootstrap file.
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

// LOAD DOTENV FILE
// 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
// 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);
}

// 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
}

// Grab our CodeIgniter
// LOAD THE FRAMEWORK BOOTSTRAP FILE
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';

/*
* ---------------------------------------------------------------
* 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
Expand All @@ -91,6 +126,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();

Expand Down
77 changes: 77 additions & 0 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -481,4 +486,76 @@ 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;
}

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;
}
}
25 changes: 12 additions & 13 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,11 @@ public function __construct(App $config)
*/
public function initialize()
{
// Define environment variables
$this->bootstrapEnvironment();

// Setup Exception Handling
Services::exceptions()->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');

// Set default timezone on the server
date_default_timezone_set($this->config->appTimezone ?? 'UTC');

$this->initializeKint();
}

/**
Expand All @@ -214,6 +201,8 @@ public function initialize()
* @throws FrameworkException
*
* @codeCoverageIgnore
*
* @deprecated 4.5.0 Moved to system/bootstrap.php.
*/
protected function resolvePlatformExtensions()
{
Expand All @@ -240,6 +229,8 @@ protected function resolvePlatformExtensions()
* Initializes Kint
*
* @return void
*
* @deprecated 4.5.0 Moved to Autoloader.
*/
protected function initializeKint()
{
Expand All @@ -255,6 +246,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
Expand All @@ -277,6 +271,9 @@ private function autoloadKint(): void
}
}

/**
* @deprecated 4.5.0 Moved to Autoloader.
*/
private function configureKint(): void
{
$config = new KintConfig();
Expand Down Expand Up @@ -578,6 +575,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()
{
Expand Down
Loading