1
0
Fork 0

Make use of Filesystem class in AutoloadGenerator

pull/142/head
Jordi Boggiano 2011-12-03 23:20:36 +01:00
parent abb926a60c
commit bc88d86983
1 changed files with 19 additions and 19 deletions

View File

@ -17,6 +17,7 @@ use Composer\Json\JsonFile;
use Composer\Package\Loader\JsonLoader; use Composer\Package\Loader\JsonLoader;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Downloader\Util\Filesystem;
/** /**
* @author Igor Wiedler <igor@wiedler.ch> * @author Igor Wiedler <igor@wiedler.ch>
@ -27,7 +28,6 @@ class AutoloadGenerator
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
{ {
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
$autoloadFile .= <<<'EOF' $autoloadFile .= <<<'EOF'
// autoload.php generated by Composer // autoload.php generated by Composer
@ -49,12 +49,17 @@ function init() {
return init(); return init();
EOF; EOF;
$namespacesFile = <<<'EOF' $filesystem = new Filesystem();
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
$relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/');
$vendorDirCode = $filesystem->findShortestPathCode($targetDir, $vendorPath, true);
$namespacesFile = <<<EOF
<?php <?php
// autoload_namespace.php generated by Composer // autoload_namespace.php generated by Composer
$baseDir = dirname(__DIR__); \$vendorDir = $vendorDirCode;
return array( return array(
@ -71,26 +76,26 @@ EOF;
// add main package // add main package
$packageMap[] = array($mainPackage, ''); $packageMap[] = array($mainPackage, '');
$autoloads = $this->parseAutoloads($packageMap); $autoloads = $this->parseAutoloads($packageMap);
$vendorPath = $installationManager->getVendorPath();
$realVendorPath = realpath($vendorPath); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
$vendorPathDepth = substr_count(strtr(substr($realVendorPath, strlen(getcwd())), '\\', '/'), '/'); $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
$appBaseDir = str_repeat('dirname(', $vendorPathDepth).'$baseDir'.str_repeat(')', $vendorPathDepth);
if (isset($autoloads['psr-0'])) { if (isset($autoloads['psr-0'])) {
foreach ($autoloads['psr-0'] as $def) { foreach ($autoloads['psr-0'] as $def) {
if (!$this->isAbsolutePath($def['path'])) { $def['path'] = strtr($def['path'], '\\', '/');
if (strpos($def['path'], $vendorPath) === 0) { $baseDir = '';
$def['path'] = substr($def['path'], strlen($vendorPath)); if (!$filesystem->isAbsolutePath($def['path'])) {
$baseDir = '$baseDir . '; if (strpos($def['path'], $relVendorPath) === 0) {
$def['path'] = substr($def['path'], strlen($relVendorPath));
$baseDir = '$vendorDir . ';
} else { } else {
$def['path'] = '/'.$def['path']; $def['path'] = '/'.$def['path'];
$baseDir = $appBaseDir . ' . '; $baseDir = $appBaseDir . ' . ';
} }
} else { } elseif (strpos($def['path'], $vendorPath) === 0) {
$baseDir = ''; $def['path'] = substr($def['path'], strlen($vendorPath));
$baseDir = '$vendorDir . ';
} }
$exportedPrefix = var_export($def['namespace'], true); $exportedPrefix = var_export($def['namespace'], true);
$exportedPath = var_export($def['path'], true); $exportedPath = var_export($def['path'], true);
@ -158,9 +163,4 @@ EOF;
return $loader; return $loader;
} }
protected function isAbsolutePath($path)
{
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
}
} }