From bc88d86983665850303aa2b5d4c7568bb63befa1 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 3 Dec 2011 23:20:36 +0100 Subject: [PATCH] Make use of Filesystem class in AutoloadGenerator --- src/Composer/Autoload/AutoloadGenerator.php | 38 ++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 8627552bb..9c6665ab6 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -17,6 +17,7 @@ use Composer\Json\JsonFile; use Composer\Package\Loader\JsonLoader; use Composer\Package\PackageInterface; use Composer\Repository\RepositoryInterface; +use Composer\Downloader\Util\Filesystem; /** * @author Igor Wiedler @@ -27,7 +28,6 @@ class AutoloadGenerator public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) { $autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php'); - $autoloadFile .= <<<'EOF' // autoload.php generated by Composer @@ -49,12 +49,17 @@ function init() { return init(); EOF; - $namespacesFile = <<<'EOF' + $filesystem = new Filesystem(); + $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); + $relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/'); + $vendorDirCode = $filesystem->findShortestPathCode($targetDir, $vendorPath, true); + + $namespacesFile = <<parseAutoloads($packageMap); - $vendorPath = $installationManager->getVendorPath(); - $realVendorPath = realpath($vendorPath); - $vendorPathDepth = substr_count(strtr(substr($realVendorPath, strlen(getcwd())), '\\', '/'), '/'); - $appBaseDir = str_repeat('dirname(', $vendorPathDepth).'$baseDir'.str_repeat(')', $vendorPathDepth); + $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); + $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $def) { - if (!$this->isAbsolutePath($def['path'])) { - if (strpos($def['path'], $vendorPath) === 0) { - $def['path'] = substr($def['path'], strlen($vendorPath)); - $baseDir = '$baseDir . '; + $def['path'] = strtr($def['path'], '\\', '/'); + $baseDir = ''; + if (!$filesystem->isAbsolutePath($def['path'])) { + if (strpos($def['path'], $relVendorPath) === 0) { + $def['path'] = substr($def['path'], strlen($relVendorPath)); + $baseDir = '$vendorDir . '; } else { $def['path'] = '/'.$def['path']; $baseDir = $appBaseDir . ' . '; } - } else { - $baseDir = ''; + } elseif (strpos($def['path'], $vendorPath) === 0) { + $def['path'] = substr($def['path'], strlen($vendorPath)); + $baseDir = '$vendorDir . '; } $exportedPrefix = var_export($def['namespace'], true); $exportedPath = var_export($def['path'], true); @@ -158,9 +163,4 @@ EOF; return $loader; } - - protected function isAbsolutePath($path) - { - return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; - } }