1
0
Fork 0

added type check to autoloader fixes #1504

pull/1550/head
Cliff Odijk 2013-01-31 23:39:57 +01:00
parent 3b2accfb58
commit 5127fe8359
3 changed files with 20 additions and 2 deletions

View File

@ -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('\\<dirsep\\>', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '<dirsep>', $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('\\<dirsep\\>', '[\\\\/]', preg_quote(str_replace(array('/', '\\'), '<dirsep>', $package->getTargetDir())));

View File

@ -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', '<?php function testFilesAutoloadGeneration1() {}');
file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
file_put_contents($this->vendorDir.'/c/c/foo/bar/test3.php', '<?php function testFilesAutoloadGeneration3() {}');
file_put_contents($this->vendorDir.'/c/c/foo/bar/test4.php', '<?php function testFilesAutoloadGeneration4() {}');
file_put_contents($this->workingDir.'/root.php', '<?php function testFilesAutoloadGenerationRoot() {}');
$this->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'));
}

View File

@ -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;
}