1
0
Fork 0

Improve include_paths generation, fixes #596

pull/547/merge
Jordi Boggiano 2012-04-19 10:51:57 +02:00
parent 5ceae7fb1f
commit b999d18365
2 changed files with 54 additions and 28 deletions

View File

@ -34,8 +34,8 @@ class AutoloadGenerator
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
$vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
$appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
$appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
$appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
$namespacesFile = <<<EOF
<?php
@ -43,7 +43,7 @@ class AutoloadGenerator
// autoload_namespace.php generated by Composer
\$vendorDir = $vendorDirCode;
\$baseDir = $appBaseDir;
\$baseDir = $appBaseDirCode;
return array(
@ -55,22 +55,7 @@ EOF;
foreach ($autoloads['psr-0'] as $namespace => $paths) {
$exportedPaths = array();
foreach ($paths as $path) {
$path = strtr($path, '\\', '/');
$baseDir = '';
if (!$filesystem->isAbsolutePath($path)) {
if (strpos($path, $relVendorPath) === 0) {
// path starts with vendor dir
$path = substr($path, strlen($relVendorPath));
$baseDir = '$vendorDir . ';
} else {
$path = '/'.$path;
$baseDir = '$baseDir . ';
}
} elseif (strpos($path, $vendorPath) === 0) {
$path = substr($path, strlen($vendorPath));
$baseDir = '$vendorDir . ';
}
$exportedPaths[] = $baseDir.var_export($path, true);
$exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
}
$exportedPrefix = var_export($namespace, true);
$namespacesFile .= " $exportedPrefix => ";
@ -88,7 +73,7 @@ EOF;
// autoload_classmap.php generated by Composer
\$vendorDir = $vendorDirCode;
\$baseDir = $appBaseDir;
\$baseDir = $appBaseDirCode;
return array(
@ -106,7 +91,7 @@ EOF;
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
if ($includePathFile = $this->getIncludePathsFile($packageMap)) {
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) {
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
}
file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
@ -186,7 +171,7 @@ EOF;
return $loader;
}
protected function getIncludePathsFile(array $packageMap)
protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)
{
$includePaths = array();
@ -198,6 +183,7 @@ EOF;
}
foreach ($package->getIncludePaths() as $includePath) {
$includePath = trim($includePath, '/');
$includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
}
}
@ -206,9 +192,43 @@ EOF;
return;
}
return sprintf(
"<?php\nreturn %s;\n", var_export($includePaths, true)
);
$includePathsFile = <<<EOF
<?php
// include_paths.php generated by Composer
\$vendorDir = $vendorDirCode;
\$baseDir = $appBaseDirCode;
return array(
EOF;
foreach ($includePaths as $path) {
$includePathsFile .= " " . $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path) . ",\n";
}
return $includePathsFile . ");\n";
}
protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
{
$path = strtr($path, '\\', '/');
$baseDir = '';
if (!$filesystem->isAbsolutePath($path)) {
if (strpos($path, $relVendorPath) === 0) {
// path starts with vendor dir
$path = substr($path, strlen($relVendorPath));
$baseDir = '$vendorDir . ';
} else {
$path = '/'.$path;
$baseDir = '$baseDir . ';
}
} elseif (strpos($path, $vendorPath) === 0) {
$path = substr($path, strlen($vendorPath));
$baseDir = '$vendorDir . ';
}
return $baseDir.var_export($path, true);
}
protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)

View File

@ -264,8 +264,12 @@ class AutoloadGeneratorTest extends TestCase
$b = new MemoryPackage("b/b", "1.0", "1.0");
$b->setIncludePaths(array("library"));
$c = new MemoryPackage("c", "1.0", "1.0");
$c->setIncludePaths(array("library"));
$packages[] = $a;
$packages[] = $b;
$packages[] = $c;
$this->repository->expects($this->once())
->method("getPackages")
@ -275,10 +279,12 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
$this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/.composer/include_paths.php');
$this->assertEquals(
array(
$this->vendorDir."/a/a/lib/",
$this->vendorDir."/b/b/library"
$this->vendorDir."/a/a/lib",
$this->vendorDir."/b/b/library",
$this->vendorDir."/c/library",
),
require($this->vendorDir."/.composer/include_paths.php")
);
@ -307,7 +313,7 @@ class AutoloadGeneratorTest extends TestCase
require($this->vendorDir."/.composer/autoload.php");
$this->assertEquals(
$oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib/",
$oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib",
get_include_path()
);