Skip to content

Commit

Permalink
Merge pull request #66 from metaversedataman/metaversedataman-patch-1
Browse files Browse the repository at this point in the history
Added try catch
  • Loading branch information
freekmurze authored Apr 28, 2023
2 parents 993193b + 20c8194 commit 104c08c
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/TemporaryDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FilesystemIterator;
use Spatie\TemporaryDirectory\Exceptions\InvalidDirectoryName;
use Spatie\TemporaryDirectory\Exceptions\PathAlreadyExists;
use Throwable;

class TemporaryDirectory
{
Expand Down Expand Up @@ -151,30 +152,34 @@ protected function isFilePath(string $path): bool

protected function deleteDirectory(string $path): bool
{
if (is_link($path)) {
return unlink($path);
}
try {
if (is_link($path)) {
return unlink($path);
}

if (! file_exists($path)) {
return true;
}
if (! file_exists($path)) {
return true;
}

if (! is_dir($path)) {
return unlink($path);
}
if (! is_dir($path)) {
return unlink($path);
}

foreach (new FilesystemIterator($path) as $item) {
if (! $this->deleteDirectory($item)) {
return false;
foreach (new FilesystemIterator($path) as $item) {
if (! $this->deleteDirectory($item)) {
return false;
}
}
}

/*
* By forcing a php garbage collection cycle using gc_collect_cycles() we can ensure
* that the rmdir does not fail due to files still being reserved in memory.
*/
gc_collect_cycles();
/*
* By forcing a php garbage collection cycle using gc_collect_cycles() we can ensure
* that the rmdir does not fail due to files still being reserved in memory.
*/
gc_collect_cycles();

return rmdir($path);
return rmdir($path);
} catch (Throwable $throwable) {
return false;
}
}
}

0 comments on commit 104c08c

Please sign in to comment.