16
16
use CodeIgniter \CLI \BaseCommand ;
17
17
use CodeIgniter \CLI \CLI ;
18
18
use CodeIgniter \Publisher \Publisher ;
19
+ use RuntimeException ;
19
20
20
21
/**
21
22
* Optimize for production.
@@ -56,34 +57,53 @@ final class Optimize extends BaseCommand
56
57
*/
57
58
public function run (array $ params )
58
59
{
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
+ }
62
69
63
70
return EXIT_SUCCESS ;
64
71
}
65
72
66
- private function clearCache ()
73
+ private function clearCache (): void
67
74
{
68
75
$ cache = WRITEPATH . 'cache/FileLocatorCache ' ;
69
- if (is_file ($ cache )) {
70
- unlink ($ cache );
71
- CLI ::write ('Removed " ' . $ cache . '". ' , 'green ' );
72
- }
76
+ $ this ->removeFile ($ cache );
73
77
74
78
$ cache = WRITEPATH . 'cache/FactoriesCache_config ' ;
79
+ $ this ->removeFile ($ cache );
80
+ }
81
+
82
+ private function removeFile (string $ cache ): void
83
+ {
75
84
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__ );
78
96
}
79
97
}
80
98
81
99
private function enableCaching (): void
82
100
{
83
101
$ publisher = new Publisher (APPPATH , APPPATH );
84
102
103
+ $ config = APPPATH . 'Config/Optimize.php ' ;
104
+
85
105
$ result = $ publisher ->replace (
86
- APPPATH . ' Config/Optimize.php ' ,
106
+ $ config ,
87
107
[
88
108
'public bool $configCacheEnabled = false; ' => 'public bool $configCacheEnabled = true; ' ,
89
109
'public bool $locatorCacheEnabled = false; ' => 'public bool $locatorCacheEnabled = true; ' ,
@@ -95,7 +115,13 @@ private function enableCaching(): void
95
115
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php". ' ,
96
116
'green '
97
117
);
118
+
119
+ return ;
98
120
}
121
+
122
+ CLI ::error ('Error in updating file: ' . clean_path ($ config ));
123
+
124
+ throw new RuntimeException (__METHOD__ );
99
125
}
100
126
101
127
private function removeDevPackages (): void
@@ -109,6 +135,12 @@ private function removeDevPackages(): void
109
135
110
136
if ($ status === 0 ) {
111
137
CLI ::write ('Removed Composer dev packages. ' , 'green ' );
138
+
139
+ return ;
112
140
}
141
+
142
+ CLI ::error ('Error in removing Composer dev packages. ' );
143
+
144
+ throw new RuntimeException (__METHOD__ );
113
145
}
114
146
}
0 commit comments