From 6585f68b34e3e164585dec3798c60a4bd5c1364b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sun, 26 May 2024 17:20:03 -0700 Subject: [PATCH] Windows symlinkg fix attempt --- src/Cleanup.php | 36 +++++++++---------- .../Integration/Util/IntegrationTestCase.php | 34 ++++++++++-------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Cleanup.php b/src/Cleanup.php index 3cf8d8a1..378fefe0 100644 --- a/src/Cleanup.php +++ b/src/Cleanup.php @@ -65,24 +65,24 @@ public function cleanup(array $sourceFiles): void // Fix for Windows paths. $absolutePath = str_replace(['\\','/'], DIRECTORY_SEPARATOR, $absolutePath); - $file = new \SplFileInfo($absolutePath); - - if ($file->isLink()) { - unlink($absolutePath); - } - - if (false !== strpos('WIN', PHP_OS) - && (stat($absolutePath)['nlink'] !== lstat($absolutePath)['nlink'] - || $file->isLink()) - ) { - /** - * `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($absolutePath); + $filesystem = new \Symfony\Component\Filesystem\Filesystem(); + $isSymlink = function ($path) use ($filesystem): bool { + return ! is_null($filesystem->readlink($path)); + }; + + if ($isSymlink($absolutePath)) { + 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($absolutePath); + } else { + unlink($absolutePath); + } } if ($absolutePath !== realpath($absolutePath)) { diff --git a/tests/Integration/Util/IntegrationTestCase.php b/tests/Integration/Util/IntegrationTestCase.php index ce06f43e..137a1803 100644 --- a/tests/Integration/Util/IntegrationTestCase.php +++ b/tests/Integration/Util/IntegrationTestCase.php @@ -87,28 +87,32 @@ protected function deleteDir($dir) $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 ($file->isLink()) { - unlink($file); + if ($isSymlink($file->getRealPath())) { + 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); + } else { + unlink($file); + } } elseif ($file->isDir()) { rmdir($file->getRealPath()); } elseif (is_readable($file->getRealPath())) { unlink($file->getRealPath()); } - - if (false !== strpos('WIN', PHP_OS) - && stat($file)['nlink'] !== lstat($file)['nlink'] - ) { - /** - * `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); - } } rmdir($dir); }