|
17 | 17 | use CodeIgniter\Autoloader\FileLocatorCached;
|
18 | 18 | use CodeIgniter\CLI\BaseCommand;
|
19 | 19 | use CodeIgniter\CLI\CLI;
|
20 |
| -use CodeIgniter\Exceptions\RuntimeException; |
21 | 20 | use CodeIgniter\Publisher\Publisher;
|
| 21 | +use CodeIgniter\Exceptions\RuntimeException; |
22 | 22 |
|
23 | 23 | /**
|
24 | 24 | * Optimize for production.
|
@@ -52,15 +52,31 @@ final class Optimize extends BaseCommand
|
52 | 52 | *
|
53 | 53 | * @var string
|
54 | 54 | */
|
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 | + ]; |
56 | 67 |
|
57 | 68 | /**
|
58 | 69 | * {@inheritDoc}
|
59 | 70 | */
|
60 | 71 | public function run(array $params)
|
61 | 72 | {
|
| 73 | + // Parse options |
| 74 | + $enableConfigCache = CLI::getOption('c'); |
| 75 | + $enableLocatorCache = CLI::getOption('l'); |
| 76 | + $disable = CLI::getOption('d'); |
| 77 | + |
62 | 78 | try {
|
63 |
| - $this->enableCaching(); |
| 79 | + $this->enableCaching($enableConfigCache, $enableLocatorCache, $disable); |
64 | 80 | $this->clearCache();
|
65 | 81 | $this->removeDevPackages();
|
66 | 82 | } catch (RuntimeException) {
|
@@ -99,32 +115,73 @@ private function removeFile(string $cache): void
|
99 | 115 | }
|
100 | 116 | }
|
101 | 117 |
|
102 |
| - private function enableCaching(): void |
| 118 | + private function enableCaching(?bool $enableConfigCache, ?bool $enableLocatorCache, ?bool $disable): void |
103 | 119 | {
|
104 | 120 | $publisher = new Publisher(APPPATH, APPPATH);
|
105 | 121 |
|
106 | 122 | $config = APPPATH . 'Config/Optimize.php';
|
107 | 123 |
|
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 | + } |
115 | 136 |
|
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 | + } |
121 | 140 |
|
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 | + } |
123 | 148 | }
|
124 | 149 |
|
125 |
| - CLI::error('Error in updating file: ' . clean_path($config)); |
| 150 | + // Apply replacements if necessary |
| 151 | + if ($searchReplace !== []) { |
| 152 | + $result = $publisher->replace($config, $searchReplace); |
126 | 153 |
|
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'); |
128 | 185 | }
|
129 | 186 |
|
130 | 187 | private function removeDevPackages(): void
|
|
0 commit comments