1
0
Fork 0

[Autoload] Make all paths relative for file portability

pull/1777/head
Martin Hasoň 2013-04-05 14:22:16 +02:00
parent 0a561d7bf1
commit 07d2f17afe
3 changed files with 87 additions and 46 deletions

View File

@ -47,7 +47,6 @@ class AutoloadGenerator
$targetDir = $vendorPath.'/'.$targetDir;
$filesystem->ensureDirectoryExists($targetDir);
$relVendorPath = $filesystem->findShortestPath($basePath, $vendorPath, true);
$vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$vendorPathCode52 = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode);
$vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
@ -73,7 +72,7 @@ EOF;
foreach ($autoloads['psr-0'] as $namespace => $paths) {
$exportedPaths = array();
foreach ($paths as $path) {
$exportedPaths[] = $this->getPathCode($filesystem, $basePath, $relVendorPath, $vendorPath, $path);
$exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
}
$exportedPrefix = var_export($namespace, true);
$namespacesFile .= " $exportedPrefix => ";
@ -135,19 +134,19 @@ EOF;
if ($scanPsr0Packages) {
foreach ($autoloads['psr-0'] as $namespace => $paths) {
foreach ($paths as $dir) {
$dir = $this->getPath($filesystem, $basePath, $relVendorPath, $vendorPath, $dir);
$whitelist = sprintf(
'{%s/%s.+(?<!(?<!/)Test\.php)$}',
preg_quote(rtrim($dir, '/')),
strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
);
$dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir);
if (!is_dir($dir)) {
continue;
}
$whitelist = sprintf(
'{%s/%s.+(?<!(?<!/)Test\.php)$}',
preg_quote($dir),
strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
);
foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
if ('' === $namespace || 0 === strpos($class, $namespace)) {
if (!isset($classMap[$class])) {
$path = $this->getPathCode($filesystem, $basePath, $relVendorPath, $vendorPath, $path);
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
$classMap[$class] = $path.",\n";
}
}
@ -159,7 +158,7 @@ EOF;
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
foreach ($autoloads['classmap'] as $dir) {
foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
$path = $this->getPathCode($filesystem, $basePath, $relVendorPath, $vendorPath, $path);
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
$classMap[$class] = $path.",\n";
}
}
@ -173,7 +172,7 @@ EOF;
$filesCode = "";
$autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
foreach ($autoloads['files'] as $functionFile) {
$filesCode .= ' require '.$this->getPathCode($filesystem, $basePath, $relVendorPath, $vendorPath, $functionFile).";\n";
$filesCode .= ' require '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).";\n";
}
if (!$suffix) {
@ -182,7 +181,7 @@ EOF;
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $relVendorPath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
}
file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
@ -253,7 +252,7 @@ EOF;
return $loader;
}
protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $basePath, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)
protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)
{
$includePaths = array();
@ -287,31 +286,29 @@ return array(
EOF;
foreach ($includePaths as $path) {
$includePathsFile .= " " . $this->getPathCode($filesystem, $basePath, $relVendorPath, $vendorPath, $path) . ",\n";
$includePathsFile .= " " . $this->getPathCode($filesystem, $basePath, $vendorPath, $path) . ",\n";
}
return $includePathsFile . ");\n";
}
protected function getPathCode(Filesystem $filesystem, $basePath, $relVendorPath, $vendorPath, $path)
protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path)
{
$path = $filesystem->normalizePath($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 . ';
$path = $basePath . '/' . $path;
}
} elseif (strpos($path, $vendorPath) === 0) {
$path = $filesystem->normalizePath($path);
$baseDir = '';
if (strpos($path, $vendorPath) === 0) {
$path = substr($path, strlen($vendorPath));
$baseDir = '$vendorDir . ';
} elseif (strpos($path, $basePath) === 0) {
$path = substr($path, strlen($basePath));
} else {
$path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true));
if (!$filesystem->isAbsolutePath($path)) {
$baseDir = '$baseDir . ';
$path = '/' . $path;
}
}
if (preg_match('/\.phar$/', $path)){
@ -321,21 +318,6 @@ EOF;
return $baseDir.var_export($path, true);
}
protected function getPath(Filesystem $filesystem, $basePath, $relVendorPath, $vendorPath, $path)
{
$path = $filesystem->normalizePath($path);
if (!$filesystem->isAbsolutePath($path)) {
if (strpos($path, $relVendorPath) === 0) {
// path starts with vendor dir
return $vendorPath . substr($path, strlen($relVendorPath));
}
return $basePath.'/'.$path;
}
return $path;
}
protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
{
return <<<AUTOLOAD

View File

@ -750,6 +750,65 @@ EOF;
$this->assertContains("require \$baseDir . '/test.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
}
public function testUpLevelRelativePaths()
{
$workingDir = $this->workingDir.'/working-dir';
mkdir($workingDir, 0777, true);
chdir($workingDir);
$package = new Package('a', '1.0', '1.0');
$package->setAutoload(array(
'psr-0' => array('Foo' => '../path/../src'),
'classmap' => array('../classmap'),
'files' => array('../test.php'),
));
$this->repository->expects($this->once())
->method('getPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
$this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');
$expectedNamespace = <<<'EOF'
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Foo' => $baseDir . '/../src',
);
EOF;
$expectedClassmap = <<<'EOF'
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir).'/working-dir';
return array(
'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php',
'Foo\\Foo' => $baseDir . '/../classmap/classes.php',
);
EOF;
$this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
$this->assertContains("require \$baseDir . '/../test.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
}
private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
{
$a = __DIR__.'/Fixtures/autoload_'.$name.'.php';

View File

@ -6,6 +6,6 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = $vendorDir;
return array(
'Main' => $baseDir . '/src',
'Lala' => $baseDir . '/src',
'Main' => $vendorDir . '/src',
'Lala' => $vendorDir . '/src',
);