Browse Source

Make use of Filesystem class in AutoloadGenerator

Jordi Boggiano 13 years ago
parent
commit
bc88d86983
1 changed files with 19 additions and 19 deletions
  1. 19 19
      src/Composer/Autoload/AutoloadGenerator.php

+ 19 - 19
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 <igor@wiedler.ch>
@@ -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 = <<<EOF
 <?php
 
 // autoload_namespace.php generated by Composer
 
-$baseDir = dirname(__DIR__);
+\$vendorDir = $vendorDirCode;
 
 return array(
 
@@ -71,26 +76,26 @@ EOF;
 
         // add main package
         $packageMap[] = array($mainPackage, '');
-
         $autoloads = $this->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) === ':';
-    }
 }