From e36756b95136427a5e57e9869989b84740b0e746 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 18 Oct 2023 19:49:24 +0000 Subject: [PATCH] - Added autoclean of dead symlinks --- src/Action/Symlink.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Action/Symlink.php b/src/Action/Symlink.php index cc36a32..7917797 100644 --- a/src/Action/Symlink.php +++ b/src/Action/Symlink.php @@ -14,7 +14,7 @@ class Symlink implements InstallActionInterface protected Config $config, ) { } - public function run(bool $dryRun = false) : mixed + public function run(bool $dryRun = false, bool $clean = true) : mixed { if (empty($this->config->destination)) { throw new \InvalidArgumentException("A destination must be provided into Config object."); @@ -24,6 +24,16 @@ class Symlink implements InstallActionInterface $this->mapPackagesLinks($path, $file); } + if ( $clean ) { + foreach(glob($this->config->destination . DIRECTORY_SEPARATOR . "*") as $file) { + if ( is_link($file) ) { + if (! file_exists(readlink($file)) ) { + unlink($file); + } + } + } + } + if ( ! $dryRun ) { foreach($this->packageLinks as $link => $source) { $destination = $this->config->destination . DIRECTORY_SEPARATOR . $link;