From 3c0a620ad5391821e794cc87617fc81200e89e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Thu, 26 Sep 2013 14:34:41 +0200 Subject: [PATCH] Fixed path analysis --- src/Composer/Autoload/AutoloadGenerator.php | 2 +- src/Composer/Util/Filesystem.php | 16 +++++----- .../Test/Autoload/AutoloadGeneratorTest.php | 31 +++++++++++++++++++ tests/Composer/Test/Util/FilesystemTest.php | 4 +++ 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 7c5821025..e5cceb43a 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -328,7 +328,7 @@ EOF; $path = $filesystem->normalizePath($path); $baseDir = ''; - if (strpos($path, $vendorPath) === 0) { + if (strpos($path.'/', $vendorPath.'/') === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir'; diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index 9b7f04901..7ed74dfaf 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -222,12 +222,12 @@ class Filesystem return './'.basename($to); } - $commonPath = $to.'/'; - while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { - $commonPath = strtr(dirname($commonPath), '\\', '/'); + $commonPath = $to; + while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath)) { + $commonPath = dirname($commonPath); } - if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { + if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { return $to; } @@ -260,12 +260,12 @@ class Filesystem return $directories ? '__DIR__' : '__FILE__'; } - $commonPath = $to.'/'; - while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { - $commonPath = strtr(dirname($commonPath), '\\', '/'); + $commonPath = $to; + while (strpos($from.'/', $commonPath.'/') !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath)) { + $commonPath = dirname($commonPath); } - if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { + if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { return var_export($to, true); } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index cecdfb4a3..7a1609926 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -869,6 +869,37 @@ EOF; $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php')); } + public function testVendorSubstringPath() + { + $package = new Package('a', '1.0', '1.0'); + $package->setAutoload(array( + 'psr-0' => array('Foo' => 'composer-test-autoload-src/src'), + )); + + $this->repository->expects($this->once()) + ->method('getCanonicalPackages') + ->will($this->returnValue(array())); + + $this->fs->ensureDirectoryExists($this->vendorDir.'/a'); + + $expectedNamespace = <<<'EOF' + array($baseDir . '/composer-test-autoload-src/src'), +); + +EOF; + + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'VendorSubstring'); + $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php')); + } + private function assertAutoloadFiles($name, $dir, $type = 'namespaces') { $a = __DIR__.'/Fixtures/autoload_'.$name.'.php'; diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 3b565ded4..29e7d8c62 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -60,6 +60,8 @@ class FilesystemTest extends TestCase array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'"), array('/foo/bar', '/foo/bar_vendor', true, "dirname(__DIR__).'/bar_vendor'"), array('/foo/bar_vendor', '/foo/bar', true, "dirname(__DIR__).'/bar'"), + array('/foo/bar_vendor', '/foo/bar/src', true, "dirname(__DIR__).'/bar/src'"), + array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "dirname(dirname(__DIR__)).'/bar/src/lib'"), ); } @@ -107,6 +109,8 @@ class FilesystemTest extends TestCase array('/tmp', '/tmp/../../test', '/test', true), array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true), array('/foo/bar_vendor', '/foo/bar', '../bar', true), + array('/foo/bar_vendor', '/foo/bar/src', '../bar/src', true), + array('/foo/bar_vendor/src2', '/foo/bar/src/lib', '../../bar/src/lib', true), ); }