Browse Source

Added Symlink utility class

Kocsis Máté 10 years ago
parent
commit
38c26ae26b
2 changed files with 58 additions and 8 deletions
  1. 4 8
      src/Composer/Installer/LibraryInstaller.php
  2. 54 0
      src/Composer/Util/Symlink.php

+ 4 - 8
src/Composer/Installer/LibraryInstaller.php

@@ -18,6 +18,7 @@ use Composer\Repository\InstalledRepositoryInterface;
 use Composer\Package\PackageInterface;
 use Composer\Package\PackageInterface;
 use Composer\Util\Filesystem;
 use Composer\Util\Filesystem;
 use Composer\Util\ProcessExecutor;
 use Composer\Util\ProcessExecutor;
+use Composer\Util\Symlink;
 
 
 /**
 /**
  * Package installation manager.
  * Package installation manager.
@@ -256,15 +257,10 @@ class LibraryInstaller implements InstallerInterface
     {
     {
         $cwd = getcwd();
         $cwd = getcwd();
         try {
         try {
-            // under linux symlinks are not always supported for example
-            // when using it in smbfs mounted folder
-            $relativeBin = $this->filesystem->findShortestPath($link, $binPath);
-            chdir(dirname($link));
-            if (false === symlink($relativeBin, $link)) {
-                throw new \ErrorException();
-            }
+            $symlink = new Symlink($this->filesystem);
+            $symlink->symlinkBin($binPath, $link);
         } catch (\ErrorException $e) {
         } catch (\ErrorException $e) {
-           $this->installUnixyProxyBinaries($binPath, $link);
+            $this->installUnixyProxyBinaries($binPath, $link);
         }
         }
         chdir($cwd);
         chdir($cwd);
     }
     }

+ 54 - 0
src/Composer/Util/Symlink.php

@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Util;
+
+use Composer\Config;
+
+/**
+ * @author Kocsis Máté <kocsismate@woohoolabs.com>
+ */
+class Symlink
+{
+    protected $filesystem;
+
+    /**
+     * Initializes the symlinking utility.
+     *
+     * @param Filesystem  $filesystem
+     */
+    public function __construct(Filesystem $filesystem = null)
+    {
+        $this->filesystem = $filesystem ?: new Filesystem();
+    }
+
+    /**
+     * Creates a symlink for a binary file at a given path.
+     *
+     * @param string $binPath The path of the binary file to be symlinked
+     * @param string $link The path where the symlink should be created
+     * @throws \ErrorException
+     */
+    public function symlinkBin($binPath, $link)
+    {
+        $cwd = getcwd();
+
+        $relativeBin = $this->filesystem->findShortestPath($link, $binPath);
+        chdir(dirname($link));
+        $result = symlink($relativeBin, $link);
+
+        chdir($cwd);
+        if ($result === false) {
+            throw new \ErrorException();
+        }
+    }
+}