Skip to content

Commit

Permalink
symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed May 27, 2024
1 parent 39dc5ab commit 77d9e7a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
12 changes: 1 addition & 11 deletions src/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,7 @@ public function cleanup(array $sourceFiles): void

$absolutePath = $this->workingDir . $relativeDirectoryPath;

// Fix for Windows paths.
$absolutePath = str_replace(['\\','/'], DIRECTORY_SEPARATOR, $absolutePath);

$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$isSymlink = function ($path) use ($filesystem): bool {
return ! is_null($filesystem->readlink($path));
};

if ($isSymlink($absolutePath)) {
if ($absolutePath !== realpath($absolutePath)) {
if (false !== strpos('WIN', PHP_OS)) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
Expand All @@ -83,9 +75,7 @@ public function cleanup(array $sourceFiles): void
} else {
unlink($absolutePath);
}
}

if ($absolutePath !== realpath($absolutePath)) {
continue;
}

Expand Down
35 changes: 5 additions & 30 deletions tests/Integration/Util/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use BrianHenryIE\Strauss\Console\Commands\Compose;
use BrianHenryIE\Strauss\TestCase;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -85,35 +87,8 @@ protected function deleteDir($dir)
return;
}

$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);

$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$isSymlink = function ($path) use ($filesystem): bool {
return ! is_null($filesystem->readlink($path));
};

/** @var \SplFileInfo $file */
foreach ($files as $file) {
if ($isSymlink($file->getPath())) {
if (false !== strpos('WIN', PHP_OS)) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
* "On windows, take care that `is_link()` returns false for Junctions."
*
* @see https://www.php.net/manual/en/function.is-link.php#113263
* @see https://stackoverflow.com/a/18262809/336146
*/
rmdir($file->getPath());
} else {
unlink($file->getPath());
}
} elseif ($file->isDir()) {
rmdir($file->getPath());
} elseif (is_readable($file->getPath())) {
unlink($file->getPath());
}
}
rmdir($dir);
$filesystem = new Filesystem(new LocalFilesystemAdapter('/'));

$filesystem->deleteDirectory($dir);
}
}

0 comments on commit 77d9e7a

Please sign in to comment.