From c85ae48e5dc028102a25c1854a53d777c59e804a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Mon, 12 Mar 2012 12:12:44 +0100 Subject: [PATCH] Vendor dir can be outside the current working directory --- src/Composer/Installer/InstallationManager.php | 15 +++++++++------ .../Test/Installer/InstallationManagerTest.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Composer/Installer/InstallationManager.php b/src/Composer/Installer/InstallationManager.php index 68355f72c..cc217a41f 100644 --- a/src/Composer/Installer/InstallationManager.php +++ b/src/Composer/Installer/InstallationManager.php @@ -18,6 +18,7 @@ use Composer\DependencyResolver\Operation\OperationInterface; use Composer\DependencyResolver\Operation\InstallOperation; use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\DependencyResolver\Operation\UninstallOperation; +use Composer\Util\Filesystem; /** * Package operation manager. @@ -39,13 +40,15 @@ class InstallationManager */ public function __construct($vendorDir = 'vendor') { - if (substr($vendorDir, 0, 1) === '/' || substr($vendorDir, 1, 1) === ':') { + $fs = new Filesystem(); + + if ($fs->isAbsolutePath($vendorDir)) { $basePath = getcwd(); - if (0 !== strpos($vendorDir, $basePath)) { - throw new \InvalidArgumentException("Vendor dir ($vendorDir) must be within the current working directory ($basePath)."); + $relativePath = $fs->findShortestPath($basePath.'/file', $vendorDir); + if ($fs->isAbsolutePath($relativePath)) { + throw new \InvalidArgumentException("Vendor dir ($vendorDir) must be accessible from the directory ($basePath)."); } - // convert to relative path - $this->vendorPath = rtrim(substr($vendorDir, strlen($basePath)+1), '/'); + $this->vendorPath = $relativePath; } else { $this->vendorPath = rtrim($vendorDir, '/'); } @@ -203,4 +206,4 @@ class InstallationManager return getcwd().DIRECTORY_SEPARATOR.$this->vendorPath; } -} \ No newline at end of file +} diff --git a/tests/Composer/Test/Installer/InstallationManagerTest.php b/tests/Composer/Test/Installer/InstallationManagerTest.php index 9182ad308..941157a29 100644 --- a/tests/Composer/Test/Installer/InstallationManagerTest.php +++ b/tests/Composer/Test/Installer/InstallationManagerTest.php @@ -19,6 +19,21 @@ use Composer\DependencyResolver\Operation\UninstallOperation; class InstallationManagerTest extends \PHPUnit_Framework_TestCase { + public function testVendorDirOutsideTheWorkingDir() + { + $manager = new InstallationManager(realpath(getcwd().'/../')); + $this->assertSame('../', $manager->getVendorPath()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testVendorDirNotAccessible() + { + $manager = new InstallationManager('/oops'); + $this->assertSame('../', $manager->getVendorPath()); + } + public function testAddGetInstaller() { $installer = $this->createInstallerMock();