From 229f3dbea8c0712025d644623d4eea11f68c025a Mon Sep 17 00:00:00 2001 From: Aselsan Date: Sat, 20 Jul 2024 17:14:37 +0700 Subject: [PATCH 1/9] 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 From 084d21554174aba6dc2996356cfe13744b05d0f3 Mon Sep 17 00:00:00 2001 From: warcooft Date: Mon, 29 Jul 2024 17:20:03 +0700 Subject: [PATCH 2/9] refactor, add test cases --- system/Commands/Utilities/Optimize.php | 113 ++++++++++++------ .../Commands/Utilities/OptimizeTest.php | 99 +++++++++++++++ 2 files changed, 173 insertions(+), 39 deletions(-) create mode 100644 tests/system/Commands/Utilities/OptimizeTest.php diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index b9a40ad98389..8caa8d6f9f1a 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\Publisher\Publisher; use CodeIgniter\Exceptions\RuntimeException; +use CodeIgniter\Publisher\Publisher; /** * Optimize for production. @@ -60,11 +60,19 @@ final class Optimize extends BaseCommand * @var array */ protected $options = [ - 'c' => 'Enable config caching.', - 'l' => 'Enable locator caching.', + 'c' => 'Enable only config caching.', + 'l' => 'Enable only locator caching.', 'd' => 'Disable config and locator caching.', ]; + private const CONFIG_CACHE = '$configCacheEnabled'; + + private const LOCATOR_CACHE = '$locatorCacheEnabled'; + + private const CONFIG_PATH = APPPATH . 'Config/Optimize.php'; + + private const CACHE_PATH = WRITEPATH . 'cache/FactoriesCache_config'; + /** * {@inheritDoc} */ @@ -76,7 +84,7 @@ public function run(array $params) $disable = CLI::getOption('d'); try { - $this->enableCaching($enableConfigCache, $enableLocatorCache, $disable); + $this->runCaching($enableConfigCache, $enableLocatorCache, $disable); $this->clearCache(); $this->removeDevPackages(); } catch (RuntimeException) { @@ -94,8 +102,7 @@ private function clearCache(): void $locator->deleteCache(); CLI::write('Removed FileLocatorCache.', 'green'); - $cache = WRITEPATH . 'cache/FactoriesCache_config'; - $this->removeFile($cache); + $this->removeFile(self::CACHE_PATH); } private function removeFile(string $cache): void @@ -115,68 +122,53 @@ private function removeFile(string $cache): void } } - private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void { - $publisher = new Publisher(APPPATH, APPPATH); - - $config = APPPATH . 'Config/Optimize.php'; - // 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;', - ]; + $searchReplace = $this->disableCaching(); } else { - if ($enableConfigCache === true) { - $searchReplace['public bool $configCacheEnabled = false;'] = 'public bool $configCacheEnabled = true;'; - } - - if ($enableLocatorCache === true) { - $searchReplace['public bool $locatorCacheEnabled = false;'] = 'public bool $locatorCacheEnabled = true;'; - } - - // 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;', - ]; - } + $searchReplace = $this->enableCaching(['config' => $enableConfigCache, 'locator' => $enableLocatorCache]); } // Apply replacements if necessary if ($searchReplace !== []) { - $result = $publisher->replace($config, $searchReplace); + $publisher = new Publisher(APPPATH, APPPATH); + + $result = $publisher->replace(self::CONFIG_PATH, $searchReplace); if ($result === true) { $messages = []; - if (in_array('public bool $configCacheEnabled = true;', $searchReplace, true)) { + if (in_array('public bool ' . self::CONFIG_CACHE . ' = true;', $searchReplace, true)) { $messages[] = 'Config Caching is enabled in "app/Config/Optimize.php".'; } - if (in_array('public bool $locatorCacheEnabled = true;', $searchReplace, true)) { + if (in_array('public bool ' . self::LOCATOR_CACHE .' = true;', $searchReplace, true)) { $messages[] = 'FileLocator Caching is enabled in "app/Config/Optimize.php".'; } - if (in_array('public bool $configCacheEnabled = false;', $searchReplace, true)) { + if (in_array('public bool ' . self::CONFIG_CACHE . ' = false;', $searchReplace, true)) { $messages[] = 'Config Caching is disabled in "app/Config/Optimize.php".'; } - if (in_array('public bool $locatorCacheEnabled = false;', $searchReplace, true)) { + if (in_array('public bool ' . self::LOCATOR_CACHE .' = false;', $searchReplace, true)) { $messages[] = 'FileLocator Caching is disabled in "app/Config/Optimize.php".'; } + + foreach ($messages as $message) { + CLI::write($message, 'green'); + CLI::newLine(); + } - CLI::write(implode("\n\n", $messages), 'green'); - CLI::write(); + CLI::newLine(); return; } - CLI::error('Error in updating file: ' . clean_path($config)); + CLI::error('Error in updating file: ' . clean_path(self::CONFIG_PATH)); throw new RuntimeException(__METHOD__); } @@ -184,6 +176,49 @@ private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCac CLI::write('No changes to caching settings.', 'yellow'); } + /** + * Disable Caching + * + * @return array + */ + private function disableCaching(): array + { + return [ + 'public bool ' . self::CONFIG_CACHE . ' = true;' => 'public bool ' . self::CONFIG_CACHE . ' = false;', + 'public bool ' . self::LOCATOR_CACHE .' = true;' => 'public bool ' . self::LOCATOR_CACHE .' = false;', + ]; + } + + /** + * Enable Caching + * + * @param array $options + * + * @return array + */ + private function enableCaching(array $options): array + { + $searchReplace = []; + + if ($options['config'] === true) { + $searchReplace['public bool ' . self::CONFIG_CACHE . ' = false;'] = 'public bool ' . self::CONFIG_CACHE . ' = true;'; + } + + if ($options['locator'] === true) { + $searchReplace['public bool ' . self::LOCATOR_CACHE .' = false;'] = 'public bool ' . self::LOCATOR_CACHE .' = true;'; + } + + // If no options provided, update both + if ($options['config'] === null && $options['locator'] === null) { + $searchReplace = [ + 'public bool ' . self::CONFIG_CACHE . ' = false;' => 'public bool ' . self::CONFIG_CACHE . ' = true;', + 'public bool ' . self::LOCATOR_CACHE .' = false;' => 'public bool ' . self::LOCATOR_CACHE .' = true;', + ]; + } + + return $searchReplace; + } + private function removeDevPackages(): void { if (! defined('VENDORPATH')) { @@ -203,4 +238,4 @@ private function removeDevPackages(): void throw new RuntimeException(__METHOD__); } -} +} \ No newline at end of file diff --git a/tests/system/Commands/Utilities/OptimizeTest.php b/tests/system/Commands/Utilities/OptimizeTest.php new file mode 100644 index 000000000000..ee8298526541 --- /dev/null +++ b/tests/system/Commands/Utilities/OptimizeTest.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class OptimizeTest extends CIUnitTestCase +{ + use StreamFilterTrait; + + protected function setUp(): void + { + $this->resetServices(); + + parent::setUp(); + } + + protected function getBuffer(): string + { + return $this->getStreamFilterBuffer(); + } + + public function testEnableConfigCaching(): void + { + $command = new Optimize(service('logger'), service('commands')); + + $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + + // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $enableCaching(true, null, null); + + // Check if config caching is enabled + $this->assertFileContains('public bool $configCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); + } + + public function testEnableLocatorCaching(): void + { + $command = new Optimize(service('logger'), service('commands')); + + $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + + // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $enableCaching(null, true, null); + + // Check if locator caching is enabled + $this->assertFileContains('public bool $locatorCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); + } + + public function testDisableCaching(): void + { + $command = new Optimize(service('logger'), service('commands')); + + $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + + // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $enableCaching(null, null, true); + + // Check if both caches are disabled + $this->assertFileContains('public bool $configCacheEnabled = false;', APPPATH . 'Config/Optimize.php'); + $this->assertFileContains('public bool $locatorCacheEnabled = false;', APPPATH . 'Config/Optimize.php'); + } + + public function testWithoutOptions(): void + { + $command = new Optimize(service('logger'), service('commands')); + + $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + + // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $enableCaching(null, null, null); + + // Check if both caches are disabled + $this->assertFileContains('public bool $configCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); + $this->assertFileContains('public bool $locatorCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); + } + + protected function assertFileContains(string $needle, string $filePath): void + { + $this->assertFileExists($filePath); + $this->assertStringContainsString($needle, file_get_contents($filePath)); + } +} \ No newline at end of file From e948d0f5b4706a3edd726640a024fa7fe4bf75aa Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 10:27:53 +0700 Subject: [PATCH 3/9] fix coding style --- system/Commands/Utilities/Optimize.php | 30 +++++++++++--------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 8caa8d6f9f1a..f190ff17fad4 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -65,13 +65,10 @@ final class Optimize extends BaseCommand 'd' => 'Disable config and locator caching.', ]; - private const CONFIG_CACHE = '$configCacheEnabled'; - + private const CONFIG_CACHE = '$configCacheEnabled'; private const LOCATOR_CACHE = '$locatorCacheEnabled'; - - private const CONFIG_PATH = APPPATH . 'Config/Optimize.php'; - - private const CACHE_PATH = WRITEPATH . 'cache/FactoriesCache_config'; + private const CONFIG_PATH = APPPATH . 'Config/Optimize.php'; + private const CACHE_PATH = WRITEPATH . 'cache/FactoriesCache_config'; /** * {@inheritDoc} @@ -128,7 +125,7 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, $searchReplace = []; if ($disable === true) { - $searchReplace = $this->disableCaching(); + $searchReplace = $this->disableCaching(); } else { $searchReplace = $this->enableCaching(['config' => $enableConfigCache, 'locator' => $enableLocatorCache]); } @@ -146,7 +143,7 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, $messages[] = 'Config Caching is enabled in "app/Config/Optimize.php".'; } - if (in_array('public bool ' . self::LOCATOR_CACHE .' = true;', $searchReplace, true)) { + if (in_array('public bool ' . self::LOCATOR_CACHE . ' = true;', $searchReplace, true)) { $messages[] = 'FileLocator Caching is enabled in "app/Config/Optimize.php".'; } @@ -154,7 +151,7 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, $messages[] = 'Config Caching is disabled in "app/Config/Optimize.php".'; } - if (in_array('public bool ' . self::LOCATOR_CACHE .' = false;', $searchReplace, true)) { + if (in_array('public bool ' . self::LOCATOR_CACHE . ' = false;', $searchReplace, true)) { $messages[] = 'FileLocator Caching is disabled in "app/Config/Optimize.php".'; } @@ -178,14 +175,12 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, /** * Disable Caching - * - * @return array */ - private function disableCaching(): array + private function disableCaching(): array { return [ 'public bool ' . self::CONFIG_CACHE . ' = true;' => 'public bool ' . self::CONFIG_CACHE . ' = false;', - 'public bool ' . self::LOCATOR_CACHE .' = true;' => 'public bool ' . self::LOCATOR_CACHE .' = false;', + 'public bool ' . self::LOCATOR_CACHE . ' = true;' => 'public bool ' . self::LOCATOR_CACHE . ' = false;', ]; } @@ -193,10 +188,8 @@ private function disableCaching(): array * Enable Caching * * @param array $options - * - * @return array */ - private function enableCaching(array $options): array + private function enableCaching(array $options): array { $searchReplace = []; @@ -205,14 +198,14 @@ private function enableCaching(array $options): array } if ($options['locator'] === true) { - $searchReplace['public bool ' . self::LOCATOR_CACHE .' = false;'] = 'public bool ' . self::LOCATOR_CACHE .' = true;'; + $searchReplace['public bool ' . self::LOCATOR_CACHE . ' = false;'] = 'public bool ' . self::LOCATOR_CACHE . ' = true;'; } // If no options provided, update both if ($options['config'] === null && $options['locator'] === null) { $searchReplace = [ 'public bool ' . self::CONFIG_CACHE . ' = false;' => 'public bool ' . self::CONFIG_CACHE . ' = true;', - 'public bool ' . self::LOCATOR_CACHE .' = false;' => 'public bool ' . self::LOCATOR_CACHE .' = true;', + 'public bool ' . self::LOCATOR_CACHE . ' = false;' => 'public bool ' . self::LOCATOR_CACHE . ' = true;', ]; } @@ -238,4 +231,5 @@ private function removeDevPackages(): void throw new RuntimeException(__METHOD__); } + } \ No newline at end of file From e4d734bb25d289ca164eb57a1b052a323809bcd8 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 10:34:59 +0700 Subject: [PATCH 4/9] fix coding standards --- system/Commands/Utilities/Optimize.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index f190ff17fad4..27c668251b4a 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -25,6 +25,11 @@ */ final class Optimize extends BaseCommand { + private const CONFIG_CACHE = '$configCacheEnabled'; + private const LOCATOR_CACHE = '$locatorCacheEnabled'; + private const CONFIG_PATH = APPPATH . 'Config/Optimize.php'; + private const CACHE_PATH = WRITEPATH . 'cache/FactoriesCache_config'; + /** * The group the command is lumped under * when listing commands. @@ -65,11 +70,6 @@ final class Optimize extends BaseCommand 'd' => 'Disable config and locator caching.', ]; - private const CONFIG_CACHE = '$configCacheEnabled'; - private const LOCATOR_CACHE = '$locatorCacheEnabled'; - private const CONFIG_PATH = APPPATH . 'Config/Optimize.php'; - private const CACHE_PATH = WRITEPATH . 'cache/FactoriesCache_config'; - /** * {@inheritDoc} */ @@ -154,7 +154,7 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, if (in_array('public bool ' . self::LOCATOR_CACHE . ' = false;', $searchReplace, true)) { $messages[] = 'FileLocator Caching is disabled in "app/Config/Optimize.php".'; } - + foreach ($messages as $message) { CLI::write($message, 'green'); CLI::newLine(); @@ -169,7 +169,6 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, throw new RuntimeException(__METHOD__); } - CLI::write('No changes to caching settings.', 'yellow'); } @@ -231,5 +230,4 @@ private function removeDevPackages(): void throw new RuntimeException(__METHOD__); } - -} \ No newline at end of file +} From 5f1045c2be158b2e1454e609a2866307abea5d94 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 10:37:19 +0700 Subject: [PATCH 5/9] add newline at end of file --- tests/system/Commands/Utilities/OptimizeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Commands/Utilities/OptimizeTest.php b/tests/system/Commands/Utilities/OptimizeTest.php index ee8298526541..9a901b010717 100644 --- a/tests/system/Commands/Utilities/OptimizeTest.php +++ b/tests/system/Commands/Utilities/OptimizeTest.php @@ -96,4 +96,4 @@ protected function assertFileContains(string $needle, string $filePath): void $this->assertFileExists($filePath); $this->assertStringContainsString($needle, file_get_contents($filePath)); } -} \ No newline at end of file +} From 32a2013befadb46e6b0077cf5fcafd7cb84f5e25 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 10:59:31 +0700 Subject: [PATCH 6/9] add method reinstallDevPackages() --- system/Commands/Utilities/Optimize.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 27c668251b4a..6004ef37156c 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -83,7 +83,11 @@ public function run(array $params) try { $this->runCaching($enableConfigCache, $enableLocatorCache, $disable); $this->clearCache(); - $this->removeDevPackages(); + if($disable === true) { + $this->reinstallDevPackages(); + } else { + $this->removeDevPackages(); + } } catch (RuntimeException) { CLI::error('The "spark optimize" failed.'); @@ -230,4 +234,24 @@ private function removeDevPackages(): void throw new RuntimeException(__METHOD__); } + + private function reinstallDevPackages(): void + { + if (! defined('VENDORPATH')) { + return; + } + + chdir(ROOTPATH); + passthru('composer install', $status); + + if ($status === 0) { + CLI::write('Installed Composer dev packages.', 'green'); + + return; + } + + CLI::error('Error in installing Composer dev packages.'); + + throw new RuntimeException(__METHOD__); + } } From 4115b8cc663505b6722775cc846d392c54d51774 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 11:02:17 +0700 Subject: [PATCH 7/9] add whitespace --- system/Commands/Utilities/Optimize.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 6004ef37156c..958acf5a6072 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -83,7 +83,7 @@ public function run(array $params) try { $this->runCaching($enableConfigCache, $enableLocatorCache, $disable); $this->clearCache(); - if($disable === true) { + if ($disable === true) { $this->reinstallDevPackages(); } else { $this->removeDevPackages(); From d73de63540553f08239c803371b49ff23394ae19 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 11:47:30 +0700 Subject: [PATCH 8/9] fix error PHPStan, update test case main method --- system/Commands/Utilities/Optimize.php | 4 ++++ .../Commands/Utilities/OptimizeTest.php | 24 +++++++++---------- .../source/installation/deployment.rst | 15 ++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 958acf5a6072..613029297e0c 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -178,6 +178,8 @@ private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, /** * Disable Caching + * + * @return array */ private function disableCaching(): array { @@ -191,6 +193,8 @@ private function disableCaching(): array * Enable Caching * * @param array $options + * + * @return array */ private function enableCaching(array $options): array { diff --git a/tests/system/Commands/Utilities/OptimizeTest.php b/tests/system/Commands/Utilities/OptimizeTest.php index 9a901b010717..48fe4fbe3b27 100644 --- a/tests/system/Commands/Utilities/OptimizeTest.php +++ b/tests/system/Commands/Utilities/OptimizeTest.php @@ -41,10 +41,10 @@ public function testEnableConfigCaching(): void { $command = new Optimize(service('logger'), service('commands')); - $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + $runCaching = $this->getPrivateMethodInvoker($command, 'runCaching'); - // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void - $enableCaching(true, null, null); + // private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $runCaching(true, null, null); // Check if config caching is enabled $this->assertFileContains('public bool $configCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); @@ -54,10 +54,10 @@ public function testEnableLocatorCaching(): void { $command = new Optimize(service('logger'), service('commands')); - $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + $runCaching = $this->getPrivateMethodInvoker($command, 'runCaching'); - // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void - $enableCaching(null, true, null); + // private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $runCaching(null, true, null); // Check if locator caching is enabled $this->assertFileContains('public bool $locatorCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); @@ -67,10 +67,10 @@ public function testDisableCaching(): void { $command = new Optimize(service('logger'), service('commands')); - $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + $runCaching = $this->getPrivateMethodInvoker($command, 'runCaching'); - // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void - $enableCaching(null, null, true); + // private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $runCaching(null, null, true); // Check if both caches are disabled $this->assertFileContains('public bool $configCacheEnabled = false;', APPPATH . 'Config/Optimize.php'); @@ -81,10 +81,10 @@ public function testWithoutOptions(): void { $command = new Optimize(service('logger'), service('commands')); - $enableCaching = $this->getPrivateMethodInvoker($command, 'enableCaching'); + $runCaching = $this->getPrivateMethodInvoker($command, 'runCaching'); - // private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void - $enableCaching(null, null, null); + // private function runCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void + $runCaching(null, null, null); // Check if both caches are disabled $this->assertFileContains('public bool $configCacheEnabled = true;', APPPATH . 'Config/Optimize.php'); diff --git a/user_guide_src/source/installation/deployment.rst b/user_guide_src/source/installation/deployment.rst index 0eb206ee9d04..74f56e7020bd 100644 --- a/user_guide_src/source/installation/deployment.rst +++ b/user_guide_src/source/installation/deployment.rst @@ -28,6 +28,21 @@ The ``spark optimize`` command performs the following optimizations: - Enabling `Config Caching`_ - Enabling `FileLocator Caching`_ +If you want disable or restore the actions above, run ``spark optimize -d`` +it will do a restore to default settings, as follow: + +.. versionadded:: 4.6.0 + +- `Reinstall Dev Packages`_ +- Disabling `Config Caching`_ +- Disabling `FileLocator Caching`_ + +Available options: + +- `-c` Enable only config caching. +- `-l` Enable only locator caching. +- `-d` Disable config and locator caching. + Composer Optimization ===================== From 880eeaf11369ac5f65edc79a7f33e077605d07f6 Mon Sep 17 00:00:00 2001 From: warcooft Date: Tue, 30 Jul 2024 14:46:58 +0700 Subject: [PATCH 9/9] fix: [Doc] Unknown target name --- user_guide_src/source/installation/deployment.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/installation/deployment.rst b/user_guide_src/source/installation/deployment.rst index 74f56e7020bd..cf5ac4a396eb 100644 --- a/user_guide_src/source/installation/deployment.rst +++ b/user_guide_src/source/installation/deployment.rst @@ -33,7 +33,7 @@ it will do a restore to default settings, as follow: .. versionadded:: 4.6.0 -- `Reinstall Dev Packages`_ +- Reinstall Dev Packages - Disabling `Config Caching`_ - Disabling `FileLocator Caching`_