Skip to content

Commit 8d6d6f8

Browse files
committed
feat: add error handling
1 parent b1bd41b commit 8d6d6f8

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

system/Commands/Utilities/Optimize.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\CLI\BaseCommand;
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Publisher\Publisher;
19+
use RuntimeException;
1920

2021
/**
2122
* Optimize for production.
@@ -56,34 +57,53 @@ final class Optimize extends BaseCommand
5657
*/
5758
public function run(array $params)
5859
{
59-
$this->enableCaching();
60-
$this->clearCache();
61-
$this->removeDevPackages();
60+
try {
61+
$this->enableCaching();
62+
$this->clearCache();
63+
$this->removeDevPackages();
64+
} catch (RuntimeException) {
65+
CLI::error('The "spark optimize" failed.');
66+
67+
return EXIT_ERROR;
68+
}
6269

6370
return EXIT_SUCCESS;
6471
}
6572

66-
private function clearCache()
73+
private function clearCache(): void
6774
{
6875
$cache = WRITEPATH . 'cache/FileLocatorCache';
69-
if (is_file($cache)) {
70-
unlink($cache);
71-
CLI::write('Removed "' . $cache . '".', 'green');
72-
}
76+
$this->removeFile($cache);
7377

7478
$cache = WRITEPATH . 'cache/FactoriesCache_config';
79+
$this->removeFile($cache);
80+
}
81+
82+
private function removeFile(string $cache): void
83+
{
7584
if (is_file($cache)) {
76-
unlink($cache);
77-
CLI::write('Removed "' . $cache . '".', 'green');
85+
$result = unlink($cache);
86+
87+
if ($result) {
88+
CLI::write('Removed "' . clean_path($cache) . '".', 'green');
89+
90+
return;
91+
}
92+
93+
CLI::error('Error in removing file: ' . clean_path($cache));
94+
95+
throw new RuntimeException(__METHOD__);
7896
}
7997
}
8098

8199
private function enableCaching(): void
82100
{
83101
$publisher = new Publisher(APPPATH, APPPATH);
84102

103+
$config = APPPATH . 'Config/Optimize.php';
104+
85105
$result = $publisher->replace(
86-
APPPATH . 'Config/Optimize.php',
106+
$config,
87107
[
88108
'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;',
89109
'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;',
@@ -95,7 +115,13 @@ private function enableCaching(): void
95115
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
96116
'green'
97117
);
118+
119+
return;
98120
}
121+
122+
CLI::error('Error in updating file: ' . clean_path($config));
123+
124+
throw new RuntimeException(__METHOD__);
99125
}
100126

101127
private function removeDevPackages(): void
@@ -109,6 +135,12 @@ private function removeDevPackages(): void
109135

110136
if ($status === 0) {
111137
CLI::write('Removed Composer dev packages.', 'green');
138+
139+
return;
112140
}
141+
142+
CLI::error('Error in removing Composer dev packages.');
143+
144+
throw new RuntimeException(__METHOD__);
113145
}
114146
}

0 commit comments

Comments
 (0)