diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 5991083f7..2b18fc796 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -442,12 +442,19 @@ FOOTER; if (!isset($autoload[$type]) || !is_array($autoload[$type])) { continue; } - if (null !== $package->getTargetDir() && $package !== $mainPackage) { + if ($type !== 'files' && null !== $package->getTargetDir() && $package !== $mainPackage) { $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); } foreach ($autoload[$type] as $namespace => $paths) { foreach ((array) $paths as $path) { + + // remove target-dir from file paths + if ($type === 'files' && !is_readable($installPath.$path)) { + $targetDir = str_replace('\\', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '', $package->getTargetDir()))); + $path = ltrim(preg_replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/'); + } + // remove target-dir from classmap entries of the root package if ($type === 'classmap' && $package === $mainPackage && $package->getTargetDir()) { $targetDir = str_replace('\\', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '', $package->getTargetDir()))); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 9693d8826..d216419c2 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -56,7 +56,8 @@ class AutoloadGeneratorTest extends TestCase $this->im->expects($this->any()) ->method('getInstallPath') ->will($this->returnCallback(function ($package) use ($that) { - return $that->vendorDir.'/'.$package->getName(); + $targetDir = $package->getTargetDir(); + return $that->vendorDir.'/'.$package->getName() . ($targetDir ? '/'.$targetDir : ''); })); $this->repository = $this->getMock('Composer\Repository\RepositoryInterface'); @@ -310,8 +311,11 @@ class AutoloadGeneratorTest extends TestCase $packages = array(); $packages[] = $a = new Package('a/a', '1.0', '1.0'); $packages[] = $b = new Package('b/b', '1.0', '1.0'); + $packages[] = $c = new Package('c/c', '1.0', '1.0'); $a->setAutoload(array('files' => array('test.php'))); $b->setAutoload(array('files' => array('test2.php'))); + $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php'))); + $c->setTargetDir('foo/bar'); $this->repository->expects($this->once()) ->method('getPackages') @@ -319,8 +323,11 @@ class AutoloadGeneratorTest extends TestCase $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a'); $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar'); file_put_contents($this->vendorDir.'/a/a/test.php', 'vendorDir.'/b/b/test2.php', 'vendorDir.'/c/c/foo/bar/test3.php', 'vendorDir.'/c/c/foo/bar/test4.php', 'workingDir.'/root.php', 'generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload'); @@ -330,6 +337,8 @@ class AutoloadGeneratorTest extends TestCase include $this->vendorDir . '/autoload.php'; $this->assertTrue(function_exists('testFilesAutoloadGeneration1')); $this->assertTrue(function_exists('testFilesAutoloadGeneration2')); + $this->assertTrue(function_exists('testFilesAutoloadGeneration3')); + $this->assertTrue(function_exists('testFilesAutoloadGeneration4')); $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot')); } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php index 2c75be906..6080089ec 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php @@ -40,7 +40,9 @@ class ComposerAutoloaderInitFilesAutoload require $vendorDir . '/a/a/test.php'; require $vendorDir . '/b/b/test2.php'; + require $vendorDir . '/c/c/foo/bar/test3.php'; require $baseDir . '/root.php'; + require $vendorDir . '/c/c/foo/bar/test4.php'; return $loader; }