Skip to content

Commit b50cfbd

Browse files
authored
Merge pull request #8603 from kenjis/fix-move-kint-autoload
fix: move Kint loading to Autoloader
2 parents 7d2d577 + 458333c commit b50cfbd

File tree

10 files changed

+364
-75
lines changed

10 files changed

+364
-75
lines changed

.github/workflows/test-phpcpd.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ jobs:
5757
--exclude system/Debug/Exceptions.php
5858
--exclude system/HTTP/SiteURI.php
5959
--exclude system/Validation/Rules.php
60+
--exclude system/Autoloader/Autoloader.php
6061
-- app/ public/ system/

public/index.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

3-
// Check PHP version.
3+
/*
4+
*---------------------------------------------------------------
5+
* CHECK PHP VERSION
6+
*---------------------------------------------------------------
7+
*/
8+
49
$minPhpVersion = '8.1'; // If you update this, don't forget to update `spark`.
510
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
611
$message = sprintf(
@@ -12,6 +17,12 @@
1217
exit($message);
1318
}
1419

20+
/*
21+
*---------------------------------------------------------------
22+
* SET THE CURRENT DIRECTORY
23+
*---------------------------------------------------------------
24+
*/
25+
1526
// Path to the front controller (this file)
1627
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);
1728

@@ -29,25 +40,38 @@
2940
* and fires up an environment-specific bootstrapping.
3041
*/
3142

32-
// Load our paths config file
43+
// LOAD OUR PATHS CONFIG FILE
3344
// This is the line that might need to be changed, depending on your folder structure.
3445
require FCPATH . '../app/Config/Paths.php';
3546
// ^^^ Change this line if you move your application folder
3647

3748
$paths = new Config\Paths();
3849

39-
// Location of the framework bootstrap file.
40-
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
41-
50+
// LOAD DOTENV FILE
4251
// Load environment settings from .env files into $_SERVER and $_ENV
43-
require_once SYSTEMPATH . 'Config/DotEnv.php';
44-
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();
52+
require_once $paths->systemDirectory . '/Config/DotEnv.php';
53+
(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load();
4554

46-
// Define ENVIRONMENT
55+
// DEFINE ENVIRONMENT
4756
if (! defined('ENVIRONMENT')) {
48-
define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production'));
57+
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT');
58+
define('ENVIRONMENT', ($env !== false) ? $env : 'production');
59+
unset($env);
60+
}
61+
62+
// LOAD ENVIRONMENT BOOTSTRAP
63+
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
64+
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
65+
} else {
66+
header('HTTP/1.1 503 Service Unavailable.', true, 503);
67+
echo 'The application environment is not set correctly.';
68+
69+
exit(EXIT_ERROR); // EXIT_ERROR
4970
}
5071

72+
// LOAD THE FRAMEWORK BOOTSTRAP FILE
73+
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
74+
5175
// Load Config Cache
5276
// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();
5377
// $factoriesCache->load('config');

spark

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/*
1414
* --------------------------------------------------------------------
15-
* CodeIgniter command-line tools
15+
* CODEIGNITER COMMAND-LINE TOOLS
1616
* --------------------------------------------------------------------
1717
* The main entry point into the CLI system and allows you to run
1818
* commands and perform maintenance on your application.
@@ -26,7 +26,12 @@ if (strpos(PHP_SAPI, 'cgi') === 0) {
2626
exit("The cli tool is not supported when running php-cgi. It needs php-cli to function!\n\n");
2727
}
2828

29-
// Check PHP version.
29+
/*
30+
*---------------------------------------------------------------
31+
* CHECK PHP VERSION
32+
*---------------------------------------------------------------
33+
*/
34+
3035
$minPhpVersion = '8.1'; // If you update this, don't forget to update `public/index.php`.
3136
if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
3237
$message = sprintf(
@@ -42,6 +47,12 @@ if (version_compare(PHP_VERSION, $minPhpVersion, '<')) {
4247
error_reporting(E_ALL);
4348
ini_set('display_errors', '1');
4449

50+
/*
51+
*---------------------------------------------------------------
52+
* SET THE CURRENT DIRECTORY
53+
*---------------------------------------------------------------
54+
*/
55+
4556
// Path to the front controller
4657
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
4758

@@ -57,32 +68,56 @@ chdir(FCPATH);
5768
* and fires up an environment-specific bootstrapping.
5869
*/
5970

60-
// Load our paths config file
71+
// LOAD OUR PATHS CONFIG FILE
6172
// This is the line that might need to be changed, depending on your folder structure.
6273
require FCPATH . '../app/Config/Paths.php';
6374
// ^^^ Change this line if you move your application folder
6475

6576
$paths = new Config\Paths();
6677

67-
// Location of the framework bootstrap file.
68-
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
69-
78+
// LOAD DOTENV FILE
7079
// Load environment settings from .env files into $_SERVER and $_ENV
71-
require_once SYSTEMPATH . 'Config/DotEnv.php';
72-
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();
80+
require_once $paths->systemDirectory . '/Config/DotEnv.php';
81+
(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load();
7382

74-
// Define ENVIRONMENT
83+
// DEFINE ENVIRONMENT
7584
if (! defined('ENVIRONMENT')) {
76-
define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production'));
85+
$env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT');
86+
define('ENVIRONMENT', ($env !== false) ? $env : 'production');
87+
unset($env);
88+
}
89+
90+
// LOAD ENVIRONMENT BOOTSTRAP
91+
if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) {
92+
require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php';
93+
} else {
94+
header('HTTP/1.1 503 Service Unavailable.', true, 503);
95+
echo 'The application environment is not set correctly.';
96+
97+
exit(EXIT_ERROR); // EXIT_ERROR
7798
}
7899

79-
// Grab our CodeIgniter
100+
// LOAD THE FRAMEWORK BOOTSTRAP FILE
101+
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
102+
103+
/*
104+
* ---------------------------------------------------------------
105+
* GRAB OUR CODEIGNITER INSTANCE
106+
* ---------------------------------------------------------------
107+
*/
108+
80109
$app = Config\Services::codeigniter();
81110
$app->initialize();
82111

83-
// Grab our Console
112+
/*
113+
* ---------------------------------------------------------------
114+
* GRAB OUR CONSOLE
115+
* ---------------------------------------------------------------
116+
*/
117+
84118
$console = new CodeIgniter\CLI\Console();
85119

120+
// SHOW HEADER
86121
// Show basic information before we do anything else.
87122
if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
88123
unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore
@@ -91,6 +126,12 @@ if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
91126

92127
$console->showHeader($suppress);
93128

129+
/*
130+
*---------------------------------------------------------------
131+
* EXECUTE THE COMMAND
132+
*---------------------------------------------------------------
133+
*/
134+
94135
// fire off the command in the main framework.
95136
$exit = $console->run();
96137

system/Autoloader/Autoloader.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
use Composer\Autoload\ClassLoader;
1818
use Composer\InstalledVersions;
1919
use Config\Autoload;
20+
use Config\Kint as KintConfig;
2021
use Config\Modules;
22+
use Config\Services;
2123
use InvalidArgumentException;
24+
use Kint;
25+
use Kint\Renderer\CliRenderer;
26+
use Kint\Renderer\RichRenderer;
2227
use RuntimeException;
2328

2429
/**
@@ -481,4 +486,76 @@ public function loadHelpers(): void
481486
{
482487
helper($this->helpers);
483488
}
489+
490+
/**
491+
* Initializes Kint
492+
*/
493+
public function initializeKint(bool $debug = false): void
494+
{
495+
if ($debug) {
496+
$this->autoloadKint();
497+
$this->configureKint();
498+
} elseif (class_exists(Kint::class)) {
499+
// In case that Kint is already loaded via Composer.
500+
Kint::$enabled_mode = false;
501+
}
502+
503+
helper('kint');
504+
}
505+
506+
private function autoloadKint(): void
507+
{
508+
// If we have KINT_DIR it means it's already loaded via composer
509+
if (! defined('KINT_DIR')) {
510+
spl_autoload_register(function ($class) {
511+
$class = explode('\\', $class);
512+
513+
if (array_shift($class) !== 'Kint') {
514+
return;
515+
}
516+
517+
$file = SYSTEMPATH . 'ThirdParty/Kint/' . implode('/', $class) . '.php';
518+
519+
if (is_file($file)) {
520+
require_once $file;
521+
}
522+
});
523+
524+
require_once SYSTEMPATH . 'ThirdParty/Kint/init.php';
525+
}
526+
}
527+
528+
private function configureKint(): void
529+
{
530+
$config = new KintConfig();
531+
532+
Kint::$depth_limit = $config->maxDepth;
533+
Kint::$display_called_from = $config->displayCalledFrom;
534+
Kint::$expanded = $config->expanded;
535+
536+
if (isset($config->plugins) && is_array($config->plugins)) {
537+
Kint::$plugins = $config->plugins;
538+
}
539+
540+
$csp = Services::csp();
541+
if ($csp->enabled()) {
542+
RichRenderer::$js_nonce = $csp->getScriptNonce();
543+
RichRenderer::$css_nonce = $csp->getStyleNonce();
544+
}
545+
546+
RichRenderer::$theme = $config->richTheme;
547+
RichRenderer::$folder = $config->richFolder;
548+
RichRenderer::$sort = $config->richSort;
549+
if (isset($config->richObjectPlugins) && is_array($config->richObjectPlugins)) {
550+
RichRenderer::$value_plugins = $config->richObjectPlugins;
551+
}
552+
if (isset($config->richTabPlugins) && is_array($config->richTabPlugins)) {
553+
RichRenderer::$tab_plugins = $config->richTabPlugins;
554+
}
555+
556+
CliRenderer::$cli_colors = $config->cliColors;
557+
CliRenderer::$force_utf8 = $config->cliForceUTF8;
558+
CliRenderer::$detect_width = $config->cliDetectWidth;
559+
CliRenderer::$min_terminal_width = $config->cliMinWidth;
560+
}
484561
}

system/CodeIgniter.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,11 @@ public function __construct(App $config)
186186
*/
187187
public function initialize()
188188
{
189-
// Define environment variables
190-
$this->bootstrapEnvironment();
191-
192-
// Setup Exception Handling
193-
Services::exceptions()->initialize();
194-
195-
// Run this check for manual installations
196-
if (! is_file(COMPOSER_PATH)) {
197-
$this->resolvePlatformExtensions(); // @codeCoverageIgnore
198-
}
199-
200189
// Set default locale on the server
201190
Locale::setDefault($this->config->defaultLocale ?? 'en');
202191

203192
// Set default timezone on the server
204193
date_default_timezone_set($this->config->appTimezone ?? 'UTC');
205-
206-
$this->initializeKint();
207194
}
208195

209196
/**
@@ -214,6 +201,8 @@ public function initialize()
214201
* @throws FrameworkException
215202
*
216203
* @codeCoverageIgnore
204+
*
205+
* @deprecated 4.5.0 Moved to system/bootstrap.php.
217206
*/
218207
protected function resolvePlatformExtensions()
219208
{
@@ -240,6 +229,8 @@ protected function resolvePlatformExtensions()
240229
* Initializes Kint
241230
*
242231
* @return void
232+
*
233+
* @deprecated 4.5.0 Moved to Autoloader.
243234
*/
244235
protected function initializeKint()
245236
{
@@ -255,6 +246,9 @@ protected function initializeKint()
255246
helper('kint');
256247
}
257248

249+
/**
250+
* @deprecated 4.5.0 Moved to Autoloader.
251+
*/
258252
private function autoloadKint(): void
259253
{
260254
// If we have KINT_DIR it means it's already loaded via composer
@@ -277,6 +271,9 @@ private function autoloadKint(): void
277271
}
278272
}
279273

274+
/**
275+
* @deprecated 4.5.0 Moved to Autoloader.
276+
*/
280277
private function configureKint(): void
281278
{
282279
$config = new KintConfig();
@@ -578,6 +575,8 @@ protected function detectEnvironment()
578575
* is wrong. At the very least, they should have error reporting setup.
579576
*
580577
* @return void
578+
*
579+
* @deprecated 4.5.0 Moved to system/bootstrap.php.
581580
*/
582581
protected function bootstrapEnvironment()
583582
{

0 commit comments

Comments
 (0)