Skip to content

feat: add Config\Optimize #8605

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 9 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 19 additions & 19 deletions app/Config/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
public $cacheQueryString = false;

/**
* --------------------------------------------------------------------------
* Key Prefix
Expand Down Expand Up @@ -168,4 +149,23 @@ class Cache extends BaseConfig
'redis' => RedisHandler::class,
'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<string>
*/
public $cacheQueryString = false;
}
32 changes: 32 additions & 0 deletions app/Config/Optimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Config;

/**
* Optimization Configuration.
*
* NOTE: This class does not extend BaseConfig for performance reasons.
* So you cannot replace the property values with Environment Variables.
*
* @immutable
*/
class Optimize
{
/**
* --------------------------------------------------------------------------
* Config Caching
* --------------------------------------------------------------------------
*
* @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;
}
39 changes: 1 addition & 38 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,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));
38 changes: 1 addition & 37 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -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));
121 changes: 113 additions & 8 deletions system/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace CodeIgniter;

use CodeIgniter\Cache\FactoriesCache;
use CodeIgniter\CLI\Console;
use CodeIgniter\Config\DotEnv;
use Config\Autoload;
use Config\Modules;
use Config\Optimize;
use Config\Paths;
use Config\Services;

Expand All @@ -32,21 +35,52 @@ class Boot
* 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::boot($paths);
static::definePathConstants($paths);
if (! defined('APP_NAMESPACE')) {
static::loadConstants();
}
static::checkMissingExtensions();

static::loadDotEnv($paths);
static::defineEnvironment();
static::loadEnvironmentBootstrap($paths);

static::loadCommonFunctions();
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();

$configCacheEnabled = class_exists(Optimize::class)
&& (new Optimize())->configCacheEnabled;
if ($configCacheEnabled) {
$factoriesCache = static::loadConfigCache();
}

static::autoloadHelpers();

$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;
}

/**
* Used by `spark`
*
* @return int Exit code.
*/
public static function bootSpark(Paths $paths): void
{
static::boot($paths);
}

protected static function boot(Paths $paths): void
public static function bootSpark(Paths $paths): int
{
static::definePathConstants($paths);
if (! defined('APP_NAMESPACE')) {
Expand All @@ -62,6 +96,12 @@ protected static function boot(Paths $paths): void
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
static::autoloadHelpers();

static::initializeCodeIgniter();
$console = static::initializeConsole();

return static::runCommand($console);
}

/**
Expand All @@ -79,6 +119,7 @@ public static function bootTest(Paths $paths): void
static::loadAutoloader();
static::setExceptionHandler();
static::initializeKint();
static::autoloadHelpers();
}

/**
Expand Down Expand Up @@ -188,6 +229,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();
}

Expand Down Expand Up @@ -234,4 +279,64 @@ 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');
}

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;
}
}
11 changes: 10 additions & 1 deletion system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -281,7 +283,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'];
Expand Down
8 changes: 6 additions & 2 deletions user_guide_src/source/concepts/autoloader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ 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;

.. note::
This property cannot be overridden by
:ref:`environment variables <configuration-classes-and-environment-variables>`.
Loading
Loading