Skip to content

Commit bc81205

Browse files
authored
Merge pull request #1441 from ga-devfront/feat/let-backup-on-uninstall
[FEAT] remove all folder on uninstall except backups
2 parents f3f2469 + 85e19f4 commit bc81205

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

autoupgrade.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public function uninstall()
131131
$ajaxTab->delete();
132132
}
133133

134-
// Remove the 1-click upgrade working directory
135-
self::_removeDirectory(_PS_ADMIN_DIR_ . DIRECTORY_SEPARATOR . 'autoupgrade');
134+
// Remove the 'autoupgrade' admin directory except backups
135+
$this->getUpgradeContainer()->getFilesystemAdapter()->clearDirectory(_PS_ADMIN_DIR_ . DIRECTORY_SEPARATOR . 'autoupgrade', ['backup']);
136136

137137
return parent::uninstall();
138138
}
@@ -159,29 +159,6 @@ protected function _abortInstall($error)
159159
return false;
160160
}
161161

162-
/**
163-
* @param string $dir
164-
*
165-
* @return void
166-
*/
167-
private static function _removeDirectory($dir)
168-
{
169-
if ($handle = @opendir($dir)) {
170-
while (false !== ($entry = @readdir($handle))) {
171-
if ($entry != '.' && $entry != '..') {
172-
if (is_dir($dir . DIRECTORY_SEPARATOR . $entry) === true) {
173-
self::_removeDirectory($dir . DIRECTORY_SEPARATOR . $entry);
174-
} else {
175-
@unlink($dir . DIRECTORY_SEPARATOR . $entry);
176-
}
177-
}
178-
}
179-
180-
@closedir($handle);
181-
@rmdir($dir);
182-
}
183-
}
184-
185162
/**
186163
* Adapter for trans calls, existing only on PS 1.7.
187164
* Making them available for PS 1.6 as well.

classes/UpgradeTools/FilesystemAdapter.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,36 +232,43 @@ public function isReleaseValid(string $path): bool
232232
}
233233

234234
/**
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.
238237
*
239238
* @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
240240
*
241241
* @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
242244
*/
243-
public function clearDirectory(string $folderToClear): bool
245+
public function clearDirectory(string $folderToClear, array $excluded = []): bool
244246
{
245247
$hasDeletedItems = false;
246248

247249
if (!$this->filesystem->exists($folderToClear)) {
248250
return $hasDeletedItems;
249251
}
250252

253+
$excluded[] = 'index.php';
254+
251255
$directory = new FilesystemIterator(
252256
$folderToClear, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO
253257
);
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);
256261
});
262+
257263
$iterator = new IteratorIterator($filter);
258264

259265
foreach ($iterator as $file) {
260266
$this->filesystem->remove($file);
267+
$hasDeletedItems = true;
261268
}
262269

263270
clearstatcache();
264271

265-
return (bool) iterator_count($iterator);
272+
return $hasDeletedItems;
266273
}
267274
}

0 commit comments

Comments
 (0)