Make classmaps relative instead of absolute for file portability
parent
27eb249aab
commit
f8a09eaa3f
|
@ -61,12 +61,16 @@ EOF;
|
|||
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath);
|
||||
$vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
|
||||
|
||||
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
|
||||
$appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
|
||||
|
||||
$namespacesFile = <<<EOF
|
||||
<?php
|
||||
|
||||
// autoload_namespace.php generated by Composer
|
||||
|
||||
\$vendorDir = $vendorDirCode;
|
||||
\$baseDir = $appBaseDir;
|
||||
|
||||
return array(
|
||||
|
||||
|
@ -75,56 +79,65 @@ EOF;
|
|||
$packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
|
||||
$autoloads = $this->parseAutoloads($packageMap);
|
||||
|
||||
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
|
||||
$appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
|
||||
|
||||
if (isset($autoloads['psr-0'])) {
|
||||
foreach ($autoloads['psr-0'] as $namespace => $paths) {
|
||||
$exportedPaths = array();
|
||||
foreach ($paths as $path) {
|
||||
$path = strtr($path, '\\', '/');
|
||||
$baseDir = '';
|
||||
if (!$filesystem->isAbsolutePath($path)) {
|
||||
// vendor dir == working dir
|
||||
if (preg_match('{^(\./?)?$}', $relVendorPath)) {
|
||||
$path = '/'.$path;
|
||||
$baseDir = '$vendorDir . ';
|
||||
} elseif (strpos($path, $relVendorPath) === 0) {
|
||||
// path starts with vendor dir
|
||||
$path = substr($path, strlen($relVendorPath));
|
||||
$baseDir = '$vendorDir . ';
|
||||
} else {
|
||||
$path = '/'.$path;
|
||||
$baseDir = $appBaseDir . ' . ';
|
||||
}
|
||||
} elseif (strpos($path, $vendorPath) === 0) {
|
||||
$path = substr($path, strlen($vendorPath));
|
||||
foreach ($autoloads['psr-0'] as $namespace => $paths) {
|
||||
$exportedPaths = array();
|
||||
foreach ($paths as $path) {
|
||||
$path = strtr($path, '\\', '/');
|
||||
$baseDir = '';
|
||||
if (!$filesystem->isAbsolutePath($path)) {
|
||||
// vendor dir == working dir
|
||||
if (preg_match('{^(\./?)?$}', $relVendorPath)) {
|
||||
$path = '/'.$path;
|
||||
$baseDir = '$vendorDir . ';
|
||||
} elseif (strpos($path, $relVendorPath) === 0) {
|
||||
// path starts with vendor dir
|
||||
$path = substr($path, strlen($relVendorPath));
|
||||
$baseDir = '$vendorDir . ';
|
||||
} else {
|
||||
$path = '/'.$path;
|
||||
$baseDir = '$baseDir . ';
|
||||
}
|
||||
$exportedPaths[] = $baseDir.var_export($path, true);
|
||||
}
|
||||
$exportedPrefix = var_export($namespace, true);
|
||||
$namespacesFile .= " $exportedPrefix => ";
|
||||
if (count($exportedPaths) > 1) {
|
||||
$namespacesFile .= "array(".implode(', ',$exportedPaths)."),\n";
|
||||
} else {
|
||||
$namespacesFile .= $exportedPaths[0].",\n";
|
||||
} elseif (strpos($path, $vendorPath) === 0) {
|
||||
$path = substr($path, strlen($vendorPath));
|
||||
$baseDir = '$vendorDir . ';
|
||||
}
|
||||
$exportedPaths[] = $baseDir.var_export($path, true);
|
||||
}
|
||||
$exportedPrefix = var_export($namespace, true);
|
||||
$namespacesFile .= " $exportedPrefix => ";
|
||||
if (count($exportedPaths) > 1) {
|
||||
$namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
|
||||
} else {
|
||||
$namespacesFile .= $exportedPaths[0].",\n";
|
||||
}
|
||||
}
|
||||
$namespacesFile .= ");\n";
|
||||
|
||||
if (isset($autoloads['classmap'])) {
|
||||
// flatten array
|
||||
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
|
||||
} else {
|
||||
$autoloads['classmap'] = array();
|
||||
}
|
||||
$classmapFile = <<<EOF
|
||||
<?php
|
||||
|
||||
ClassMapGenerator::dump($autoloads['classmap'], $targetDir.'/autoload_classmap.php');
|
||||
// autoload_classmap.php generated by Composer
|
||||
|
||||
\$vendorDir = $vendorDirCode;
|
||||
\$baseDir = $appBaseDir;
|
||||
|
||||
return array(
|
||||
|
||||
EOF;
|
||||
|
||||
// flatten array
|
||||
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
|
||||
foreach ($autoloads['classmap'] as $dir) {
|
||||
foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
|
||||
$path = '/'.$filesystem->findShortestPath(getcwd(), $path);
|
||||
$classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
|
||||
}
|
||||
}
|
||||
$classmapFile .= ");\n";
|
||||
|
||||
file_put_contents($targetDir.'/autoload.php', $autoloadFile);
|
||||
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
|
||||
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
|
||||
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
||||
}
|
||||
|
||||
|
@ -154,7 +167,7 @@ EOF;
|
|||
*/
|
||||
public function parseAutoloads(array $packageMap)
|
||||
{
|
||||
$autoloads = array();
|
||||
$autoloads = array('classmap' => array(), 'psr-0' => array());
|
||||
foreach ($packageMap as $item) {
|
||||
list($package, $installPath) = $item;
|
||||
|
||||
|
|
|
@ -160,11 +160,12 @@ class AutoloadGeneratorTest extends TestCase
|
|||
file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
|
||||
|
||||
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
||||
$this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
|
||||
$this->assertEquals(array(
|
||||
'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php',
|
||||
'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php',
|
||||
'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php',
|
||||
$this->assertTrue(file_exists($this->vendorDir.'/.composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php',
|
||||
'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php',
|
||||
'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php',
|
||||
),
|
||||
include ($this->vendorDir.'/.composer/autoload_classmap.php')
|
||||
);
|
||||
|
|
|
@ -3,8 +3,9 @@
|
|||
// autoload_namespace.php generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Main' => dirname($vendorDir) . '/src/',
|
||||
'Lala' => dirname($vendorDir) . '/src/',
|
||||
'Main' => $baseDir . '/src/',
|
||||
'Lala' => $baseDir . '/src/',
|
||||
);
|
||||
|
|
|
@ -3,8 +3,9 @@
|
|||
// autoload_namespace.php generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname(dirname($vendorDir));
|
||||
|
||||
return array(
|
||||
'Main' => dirname(dirname($vendorDir)) . '/src/',
|
||||
'Lala' => dirname(dirname($vendorDir)) . '/src/',
|
||||
'Main' => $baseDir . '/src/',
|
||||
'Lala' => $baseDir . '/src/',
|
||||
);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
// autoload_namespace.php generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Main' => $vendorDir . '/src/',
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
// autoload_namespace.php generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'B\\Sub\\Name' => $vendorDir . '/b/b/src/',
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
// autoload_namespace.php generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'B\\Sub\\Name' => $vendorDir . '/b/b/src/',
|
||||
|
|
Loading…
Reference in New Issue