24
24
use function file_exists ;
25
25
use function filter_var_array ;
26
26
use function fwrite ;
27
+ use function implode ;
27
28
use const FILTER_FLAG_STRIP_LOW ;
28
29
use const FILTER_SANITIZE_STRING ;
29
30
use const FILTER_VALIDATE_BOOLEAN ;
31
+ use const PHP_EOL ;
30
32
use const STDERR ;
33
+ use const STDOUT ;
31
34
32
35
/**
33
36
* A class to install and update local cache.
@@ -78,7 +81,7 @@ public static function createFromCacheDir(LoggerInterface $logger, string $cache
78
81
return new self (new Manager (new Cache ($ cacheDir ), new CurlHttpClient ()), $ logger );
79
82
}
80
83
81
- public function refresh (array $ context = []): int
84
+ public function refresh (array $ context = []): bool
82
85
{
83
86
$ context = filter_var_array (array_replace (self ::DEFAULT_CONTEXT , $ context ), [
84
87
self ::REFRESH_PSL_KEY => FILTER_VALIDATE_BOOLEAN ,
@@ -94,16 +97,14 @@ public function refresh(array $context = []): int
94
97
}
95
98
96
99
try {
97
- $ retVal = $ this ->execute ($ context );
100
+ return $ this ->execute ($ context );
98
101
} catch (PsrCacheException $ exception ) {
99
102
$ this ->logger ->error ('Local cache update failed with {exception} ' , ['exception ' => $ exception ->getMessage ()]);
100
- $ retVal = 1 ;
103
+ return false ;
101
104
} catch (Throwable $ exception ) {
102
105
$ this ->logger ->error ('Local cache update failed with {exception} ' , ['exception ' => $ exception ->getMessage ()]);
103
- $ retVal = 1 ;
106
+ return false ;
104
107
}
105
-
106
- return $ retVal ;
107
108
}
108
109
109
110
/**
@@ -113,7 +114,7 @@ public function refresh(array $context = []): int
113
114
*
114
115
* @throws PsrCacheException
115
116
*/
116
- private function execute (array $ arguments = []): int
117
+ private function execute (array $ arguments = []): bool
117
118
{
118
119
if ($ arguments [self ::REFRESH_PSL_KEY ]) {
119
120
if (!$ this ->manager ->refreshRules ($ arguments [self ::REFRESH_PSL_URL_KEY ], $ arguments [self ::TTL_KEY ])) {
@@ -122,7 +123,7 @@ private function execute(array $arguments = []): int
122
123
'ttl ' => $ arguments [self ::TTL_KEY ],
123
124
]);
124
125
125
- return 1 ;
126
+ return false ;
126
127
}
127
128
128
129
$ this ->logger ->info ('Public Suffix List Cache updated for {ttl} using {psl_url} ' , [
@@ -132,7 +133,7 @@ private function execute(array $arguments = []): int
132
133
}
133
134
134
135
if (!$ arguments [self ::REFRESH_RZD_KEY ]) {
135
- return 0 ;
136
+ return true ;
136
137
}
137
138
138
139
$ this ->logger ->info ('Updating your IANA Root Zone Database copy. ' );
@@ -142,15 +143,15 @@ private function execute(array $arguments = []): int
142
143
'ttl ' => $ arguments [self ::TTL_KEY ],
143
144
]);
144
145
145
- return 0 ;
146
+ return true ;
146
147
}
147
148
148
149
$ this ->logger ->error ('Unable to update the IANA Root Zone Database Cache using {rzd_url} with a TTL of {ttl} ' , [
149
150
'rzd_url ' => $ arguments [self ::REFRESH_RZD_URL_KEY ],
150
151
'ttl ' => $ arguments [self ::TTL_KEY ],
151
152
]);
152
153
153
- return 1 ;
154
+ return false ;
154
155
}
155
156
156
157
/**
@@ -160,18 +161,18 @@ private function execute(array $arguments = []): int
160
161
*/
161
162
public static function updateLocalCache (Event $ event = null )
162
163
{
164
+ $ io = self ::getIO ($ event );
163
165
if (!extension_loaded ('curl ' )) {
164
- fwrite (STDERR , 'The PHP cURL extension is missing. ' );
165
-
166
+ $ io ->writeError ('The PHP cURL extension is missing. ' );
166
167
die (1 );
167
168
}
168
169
169
170
$ vendor = self ::getVendorPath ($ event );
170
171
if (null === $ vendor ) {
171
- fwrite ( STDERR , implode ( PHP_EOL , [
172
+ $ io -> writeError ( [
172
173
'You must set up the project dependencies using composer ' ,
173
174
'see https://getcomposer.org ' ,
174
- ]). PHP_EOL ) ;
175
+ ]);
175
176
die (1 );
176
177
}
177
178
@@ -191,13 +192,41 @@ public static function updateLocalCache(Event $event = null)
191
192
$ logger = new Logger ();
192
193
$ installer = self ::createFromCacheDir ($ logger , $ arguments [Installer::CACHE_DIR_KEY ]);
193
194
$ logger ->info ('Updating your Pdp local cache. ' );
194
- $ retVal = $ installer ->refresh ($ arguments );
195
- if (0 === $ retVal ) {
195
+ if ($ installer ->refresh ($ arguments )) {
196
196
$ logger ->info ('Pdp local cache successfully updated. ' );
197
- } else {
198
- $ logger ->error ('The command failed to update Pdp local cache successfully. ' );
197
+ die (0 );
199
198
}
200
- die ($ retVal );
199
+
200
+ $ logger ->error ('The command failed to update Pdp local cache. ' );
201
+ die (1 );
202
+ }
203
+
204
+ /**
205
+ * Detect the I/O interface to use.
206
+ *
207
+ * @param Event|null $event
208
+ *
209
+ * @return mixed
210
+ */
211
+ private static function getIO (Event $ event = null )
212
+ {
213
+ return null !== $ event ? $ event ->getIO () : new class () {
214
+ public function write ($ messages , bool $ newline = true , int $ verbosity = 2 )
215
+ {
216
+ $ this ->doWrite ($ messages , $ newline , false , $ verbosity );
217
+ }
218
+ public function writeError ($ messages , bool $ newline = true , int $ verbosity = 2 )
219
+ {
220
+ $ this ->doWrite ($ messages , $ newline , true , $ verbosity );
221
+ }
222
+ private function doWrite ($ messages , bool $ newline , bool $ stderr , int $ verbosity )
223
+ {
224
+ fwrite (
225
+ $ stderr ? STDERR : STDOUT ,
226
+ implode ($ newline ? PHP_EOL : '' , (array ) $ messages ).PHP_EOL
227
+ );
228
+ }
229
+ };
201
230
}
202
231
203
232
/**
0 commit comments