Skip to content

Commit

Permalink
squash commit
Browse files Browse the repository at this point in the history
  • Loading branch information
warcooft committed Jul 29, 2024
1 parent e469021 commit 229f3db
Showing 1 changed file with 76 additions and 19 deletions.
95 changes: 76 additions & 19 deletions system/Commands/Utilities/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, string>
*/
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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 229f3db

Please sign in to comment.