@@ -232,36 +232,43 @@ public function isReleaseValid(string $path): bool
232
232
}
233
233
234
234
/**
235
- * Clears the contents of a given directory, optionally deleting the directory itself.
236
- *
237
- * @throws IOException if the removal of a file or directory fails
235
+ * Clears the contents of a given directory, excluding specified items,
236
+ * * optionally deleting the directory itself.
238
237
*
239
238
* @param string $folderToClear the absolute path of the directory to be cleared
239
+ * @param string[] $excluded list of file or directory names to exclude from deletion
240
240
*
241
241
* @return bool returns `true` if any files/folders were deleted, `false` otherwise
242
+ *
243
+ * @throws IOException if the removal of a file or directory fails
242
244
*/
243
- public function clearDirectory (string $ folderToClear ): bool
245
+ public function clearDirectory (string $ folderToClear, array $ excluded = [] ): bool
244
246
{
245
247
$ hasDeletedItems = false ;
246
248
247
249
if (!$ this ->filesystem ->exists ($ folderToClear )) {
248
250
return $ hasDeletedItems ;
249
251
}
250
252
253
+ $ excluded [] = 'index.php ' ;
254
+
251
255
$ directory = new FilesystemIterator (
252
256
$ folderToClear , FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO
253
257
);
254
- $ filter = new CallbackFilterIterator ($ directory , function ($ current ) {
255
- return $ current ->getFilename () !== 'index.php ' ;
258
+
259
+ $ filter = new CallbackFilterIterator ($ directory , function ($ current ) use ($ excluded ) {
260
+ return !in_array ($ current ->getFilename (), $ excluded , true );
256
261
});
262
+
257
263
$ iterator = new IteratorIterator ($ filter );
258
264
259
265
foreach ($ iterator as $ file ) {
260
266
$ this ->filesystem ->remove ($ file );
267
+ $ hasDeletedItems = true ;
261
268
}
262
269
263
270
clearstatcache ();
264
271
265
- return ( bool ) iterator_count ( $ iterator ) ;
272
+ return $ hasDeletedItems ;
266
273
}
267
274
}
0 commit comments