Skip to content

Commit 229f3db

Browse files
committed
squash commit
1 parent e469021 commit 229f3db

File tree

1 file changed

+76
-19
lines changed

1 file changed

+76
-19
lines changed

system/Commands/Utilities/Optimize.php

+76-19
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use CodeIgniter\Autoloader\FileLocatorCached;
1818
use CodeIgniter\CLI\BaseCommand;
1919
use CodeIgniter\CLI\CLI;
20-
use CodeIgniter\Exceptions\RuntimeException;
2120
use CodeIgniter\Publisher\Publisher;
21+
use CodeIgniter\Exceptions\RuntimeException;
2222

2323
/**
2424
* Optimize for production.
@@ -52,15 +52,31 @@ final class Optimize extends BaseCommand
5252
*
5353
* @var string
5454
*/
55-
protected $usage = 'optimize';
55+
protected $usage = 'optimize [-c] [-l] [-d]';
56+
57+
/**
58+
* The Command's options
59+
*
60+
* @var array<string, string>
61+
*/
62+
protected $options = [
63+
'c' => 'Enable config caching.',
64+
'l' => 'Enable locator caching.',
65+
'd' => 'Disable config and locator caching.',
66+
];
5667

5768
/**
5869
* {@inheritDoc}
5970
*/
6071
public function run(array $params)
6172
{
73+
// Parse options
74+
$enableConfigCache = CLI::getOption('c');
75+
$enableLocatorCache = CLI::getOption('l');
76+
$disable = CLI::getOption('d');
77+
6278
try {
63-
$this->enableCaching();
79+
$this->enableCaching($enableConfigCache, $enableLocatorCache, $disable);
6480
$this->clearCache();
6581
$this->removeDevPackages();
6682
} catch (RuntimeException) {
@@ -99,32 +115,73 @@ private function removeFile(string $cache): void
99115
}
100116
}
101117

102-
private function enableCaching(): void
118+
private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void
103119
{
104120
$publisher = new Publisher(APPPATH, APPPATH);
105121

106122
$config = APPPATH . 'Config/Optimize.php';
107123

108-
$result = $publisher->replace(
109-
$config,
110-
[
111-
'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;',
112-
'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;',
113-
]
114-
);
124+
// Prepare search and replace mappings
125+
$searchReplace = [];
126+
127+
if ($disable === true) {
128+
$searchReplace = [
129+
'public bool $configCacheEnabled = true;' => 'public bool $configCacheEnabled = false;',
130+
'public bool $locatorCacheEnabled = true;' => 'public bool $locatorCacheEnabled = false;',
131+
];
132+
} else {
133+
if ($enableConfigCache === true) {
134+
$searchReplace['public bool $configCacheEnabled = false;'] = 'public bool $configCacheEnabled = true;';
135+
}
115136

116-
if ($result) {
117-
CLI::write(
118-
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
119-
'green'
120-
);
137+
if ($enableLocatorCache === true) {
138+
$searchReplace['public bool $locatorCacheEnabled = false;'] = 'public bool $locatorCacheEnabled = true;';
139+
}
121140

122-
return;
141+
// If no options provided, update both
142+
if ($enableConfigCache === null && $enableLocatorCache === null) {
143+
$searchReplace = [
144+
'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;',
145+
'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;',
146+
];
147+
}
123148
}
124149

125-
CLI::error('Error in updating file: ' . clean_path($config));
150+
// Apply replacements if necessary
151+
if ($searchReplace !== []) {
152+
$result = $publisher->replace($config, $searchReplace);
126153

127-
throw new RuntimeException(__METHOD__);
154+
if ($result === true) {
155+
$messages = [];
156+
157+
if (in_array('public bool $configCacheEnabled = true;', $searchReplace, true)) {
158+
$messages[] = 'Config Caching is enabled in "app/Config/Optimize.php".';
159+
}
160+
161+
if (in_array('public bool $locatorCacheEnabled = true;', $searchReplace, true)) {
162+
$messages[] = 'FileLocator Caching is enabled in "app/Config/Optimize.php".';
163+
}
164+
165+
if (in_array('public bool $configCacheEnabled = false;', $searchReplace, true)) {
166+
$messages[] = 'Config Caching is disabled in "app/Config/Optimize.php".';
167+
}
168+
169+
if (in_array('public bool $locatorCacheEnabled = false;', $searchReplace, true)) {
170+
$messages[] = 'FileLocator Caching is disabled in "app/Config/Optimize.php".';
171+
}
172+
173+
CLI::write(implode("\n\n", $messages), 'green');
174+
CLI::write();
175+
176+
return;
177+
}
178+
179+
CLI::error('Error in updating file: ' . clean_path($config));
180+
181+
throw new RuntimeException(__METHOD__);
182+
}
183+
184+
CLI::write('No changes to caching settings.', 'yellow');
128185
}
129186

130187
private function removeDevPackages(): void

0 commit comments

Comments
 (0)