From 229f3dbea8c0712025d644623d4eea11f68c025a Mon Sep 17 00:00:00 2001 From: Aselsan Date: Sat, 20 Jul 2024 17:14:37 +0700 Subject: [PATCH] squash commit --- system/Commands/Utilities/Optimize.php | 95 ++++++++++++++++++++------ 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 46b24efb2da9..b9a40ad98389 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -17,8 +17,8 @@ use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\CLI; -use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\Publisher\Publisher; +use CodeIgniter\Exceptions\RuntimeException; /** * Optimize for production. @@ -52,15 +52,31 @@ final class Optimize extends BaseCommand * * @var string */ - protected $usage = 'optimize'; + protected $usage = 'optimize [-c] [-l] [-d]'; + + /** + * The Command's options + * + * @var array + */ + protected $options = [ + 'c' => 'Enable config caching.', + 'l' => 'Enable locator caching.', + 'd' => 'Disable config and locator caching.', + ]; /** * {@inheritDoc} */ public function run(array $params) { + // Parse options + $enableConfigCache = CLI::getOption('c'); + $enableLocatorCache = CLI::getOption('l'); + $disable = CLI::getOption('d'); + try { - $this->enableCaching(); + $this->enableCaching($enableConfigCache, $enableLocatorCache, $disable); $this->clearCache(); $this->removeDevPackages(); } catch (RuntimeException) { @@ -99,32 +115,73 @@ private function removeFile(string $cache): void } } - private function enableCaching(): void + private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void { $publisher = new Publisher(APPPATH, APPPATH); $config = APPPATH . 'Config/Optimize.php'; - $result = $publisher->replace( - $config, - [ - 'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;', - 'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;', - ] - ); + // Prepare search and replace mappings + $searchReplace = []; + + if ($disable === true) { + $searchReplace = [ + 'public bool $configCacheEnabled = true;' => 'public bool $configCacheEnabled = false;', + 'public bool $locatorCacheEnabled = true;' => 'public bool $locatorCacheEnabled = false;', + ]; + } else { + if ($enableConfigCache === true) { + $searchReplace['public bool $configCacheEnabled = false;'] = 'public bool $configCacheEnabled = true;'; + } - if ($result) { - CLI::write( - 'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".', - 'green' - ); + if ($enableLocatorCache === true) { + $searchReplace['public bool $locatorCacheEnabled = false;'] = 'public bool $locatorCacheEnabled = true;'; + } - return; + // If no options provided, update both + if ($enableConfigCache === null && $enableLocatorCache === null) { + $searchReplace = [ + 'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;', + 'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;', + ]; + } } - CLI::error('Error in updating file: ' . clean_path($config)); + // Apply replacements if necessary + if ($searchReplace !== []) { + $result = $publisher->replace($config, $searchReplace); - throw new RuntimeException(__METHOD__); + if ($result === true) { + $messages = []; + + if (in_array('public bool $configCacheEnabled = true;', $searchReplace, true)) { + $messages[] = 'Config Caching is enabled in "app/Config/Optimize.php".'; + } + + if (in_array('public bool $locatorCacheEnabled = true;', $searchReplace, true)) { + $messages[] = 'FileLocator Caching is enabled in "app/Config/Optimize.php".'; + } + + if (in_array('public bool $configCacheEnabled = false;', $searchReplace, true)) { + $messages[] = 'Config Caching is disabled in "app/Config/Optimize.php".'; + } + + if (in_array('public bool $locatorCacheEnabled = false;', $searchReplace, true)) { + $messages[] = 'FileLocator Caching is disabled in "app/Config/Optimize.php".'; + } + + CLI::write(implode("\n\n", $messages), 'green'); + CLI::write(); + + return; + } + + CLI::error('Error in updating file: ' . clean_path($config)); + + throw new RuntimeException(__METHOD__); + } + + CLI::write('No changes to caching settings.', 'yellow'); } private function removeDevPackages(): void