public function purge($dir, callable $progress = NULL)
{
$dirs = $entries = [];
$iterator = dir($path = "ssh2.sftp://{$this->sftp}{$dir}");
while (FALSE !== ($entry = $iterator->read())) {
if ($entry !== '.' && $entry !== '..') {
$entries[] = $entry;
}
}
foreach ($entries as $entry) {
if (is_dir("{$path}/{$entry}")) {
$dirs[] = $tmp = '.delete' . uniqid() . count($dirs);
$this->protect('rename', ["{$path}/{$entry}", "{$path}/{$tmp}"]);
} else {
$this->protect('unlink', ["{$path}/{$entry}"]);
}
if ($progress) {
$progress($entry);
}
}
foreach ($dirs as $subdir) {
$this->purge("{$dir}/{$subdir}", $progress);
$this->protect('rmdir', ["{$path}/{$subdir}"]);
}
}