/**
  * Get the target path for a rendered file from a template file.
  *
  * @since 0.1.0
  *
  * @param string $pathname The path and file name to the template file.
  *
  * @return string The target path and file name to use for the rendered file.
  */
 protected function getTargetPath($pathname)
 {
     $filesystem = new Filesystem();
     $templatesFolder = $this->getConfigKey('Folders', 'templates');
     $folderDiff = '/' . $filesystem->findShortestPath(SetupHelper::getRootFolder(), $templatesFolder);
     return (string) $this->removeTemplateExtension(str_replace($folderDiff, '', $pathname));
 }
 /**
  * Creates an instance of InstallationManager
  *
  * @param    string    $vendorDir    Relative path to the vendor directory
  * @throws   \InvalidArgumentException
  */
 public function __construct($vendorDir = 'vendor')
 {
     $fs = new Filesystem();
     if ($fs->isAbsolutePath($vendorDir)) {
         $basePath = getcwd();
         $relativePath = $fs->findShortestPath($basePath . '/file', $vendorDir);
         if ($fs->isAbsolutePath($relativePath)) {
             throw new \InvalidArgumentException("Vendor dir ({$vendorDir}) must be accessible from the directory ({$basePath}).");
         }
         $this->vendorPath = $relativePath;
     } else {
         $this->vendorPath = rtrim($vendorDir, '/');
     }
 }
    /**
     * Generate file with path to Composer 'vendor' dir to be used by the application
     *
     * @param \Composer\Composer $composer
     * @throws \UnexpectedValueException
     */
    private function saveVendorDirPath(Composer $composer)
    {
        $magentoDir = $this->installer->getTargetDir();
        $vendorDirPath = $this->filesystem->findShortestPath($magentoDir, realpath($composer->getConfig()->get('vendor-dir')), true);
        $vendorPathFile = $magentoDir . '/app/etc/vendor_path.php';
        $content = <<<AUTOLOAD
<?php
/**
 * Path to Composer vendor directory
 */
return '{$vendorDirPath}';

AUTOLOAD;
        file_put_contents($vendorPathFile, $content);
    }
Beispiel #4
0
 public function process($message = '')
 {
     // Mirror each package's assets into the component directory.
     $fs = new Filesystem();
     foreach ($this->packages as $package) {
         $packageDir = $this->getVendorDir($package);
         $name = isset($package['name']) ? $package['name'] : '__component__';
         $extra = isset($package['extra']) ? $package['extra'] : array();
         $componentName = $this->getComponentName($name, $extra);
         $fileType = array('scripts', 'styles', 'files');
         foreach ($fileType as $type) {
             // Only act on the files if they're available.
             if (isset($extra['component'][$type]) && is_array($extra['component'][$type])) {
                 foreach ($extra['component'][$type] as $file) {
                     // Make sure the file itself is available.
                     $source = $packageDir . DIRECTORY_SEPARATOR . $file;
                     if (file_exists($source)) {
                         // Find where the file destination should be.
                         $destination = $this->componentDir . DIRECTORY_SEPARATOR . $componentName . DIRECTORY_SEPARATOR . $file;
                         // Ensure the directory is available.
                         $fs->ensureDirectoryExists(dirname($destination));
                         // Delete the file before creating its mirror.
                         $fs->remove($destination);
                         // Symlink the file using a relative path from the destination.
                         $cwd = getcwd();
                         $fullDestination = $cwd . DIRECTORY_SEPARATOR . $destination;
                         $relative = $fs->findShortestPath($fullDestination, realpath($source));
                         try {
                             chdir(dirname($destination));
                             symlink($relative, $fullDestination);
                         } catch (\ErrorException $e) {
                             // If Symlinking failed, try copying.
                             if (!copy($relative, $fullDestination)) {
                                 $this->io->write(sprintf('<error>Failed to produce %s as %s.</error>', $fullDestination, $relative));
                                 return false;
                             }
                         }
                         chdir($cwd);
                     }
                 }
             }
         }
     }
     return true;
 }
 protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.', $fileName = null)
 {
     $factory = new Factory();
     $downloadManager = $factory->createDownloadManager($io, $config);
     $archiveManager = $factory->createArchiveManager($config, $downloadManager);
     if ($packageName) {
         $package = $this->selectPackage($io, $packageName, $version);
         if (!$package) {
             return 1;
         }
     } else {
         $package = $this->getComposer()->getPackage();
     }
     $io->writeError('<info>Creating the archive into "' . $dest . '".</info>');
     $packagePath = $archiveManager->archive($package, $format, $dest, $fileName);
     $fs = new Filesystem();
     $shortPath = $fs->findShortestPath(getcwd(), $packagePath, true);
     $io->writeError('Created: ', false);
     $io->write(strlen($shortPath) < strlen($packagePath) ? $shortPath : $packagePath);
     return 0;
 }
 protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path)
 {
     if (!$filesystem->isAbsolutePath($path)) {
         $path = $basePath . '/' . $path;
     }
     $path = $filesystem->normalizePath($path);
     $baseDir = '';
     if (strpos($path . '/', $vendorPath . '/') === 0) {
         $path = substr($path, strlen($vendorPath));
         $baseDir = '$vendorDir';
         if ($path !== false) {
             $baseDir .= " . ";
         }
     } else {
         $path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true));
         if (!$filesystem->isAbsolutePath($path)) {
             $baseDir = '$baseDir . ';
             $path = '/' . $path;
         }
     }
     if (preg_match('/\\.phar$/', $path)) {
         $baseDir = "'phar://' . " . $baseDir;
     }
     return $baseDir . ($path !== false ? var_export($path, true) : "");
 }
Beispiel #7
0
 /**
  * @dataProvider providePathCouples
  */
 public function testFindShortestPath($a, $b, $expected, $directory = false)
 {
     $fs = new Filesystem();
     $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
 }
Beispiel #8
0
    public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
    {
        $filesystem = new Filesystem();
        $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
        $vendorPath = strtr(realpath($config->get('vendor-dir')), '\\', '/');
        $targetDir = $vendorPath . '/' . $targetDir;
        $filesystem->ensureDirectoryExists($targetDir);
        $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
        $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
        $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
        $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
        $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
        $namespacesFile = <<<EOF
<?php

// autoload_namespaces.php generated by Composer

\$vendorDir = {$vendorPathCode};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
        $autoloads = $this->parseAutoloads($packageMap);
        foreach ($autoloads['psr-0'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $namespacesFile .= "    {$exportedPrefix} => ";
            if (count($exportedPaths) > 1) {
                $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n";
            } else {
                $namespacesFile .= $exportedPaths[0] . ",\n";
            }
        }
        $namespacesFile .= ");\n";
        $classmapFile = <<<EOF
<?php

// autoload_classmap.php generated by Composer

\$vendorDir = {$vendorPathCode};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // add custom psr-0 autoloading if the root package has a target dir
        $targetDirLoader = null;
        $mainAutoload = $mainPackage->getAutoload();
        if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
            $levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/')));
            $prefixes = implode(', ', array_map(function ($prefix) {
                return var_export($prefix, true);
            }, array_keys($mainAutoload['psr-0'])));
            $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, getcwd(), true);
            $targetDirLoader = <<<EOF

    public static function autoload(\$class)
    {
        \$dir = {$baseDirFromTargetDirCode} . '/';
        \$prefixes = array({$prefixes});
        foreach (\$prefixes as \$prefix) {
            if (0 !== strpos(\$class, \$prefix)) {
                continue;
            }
            \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), {$levels})).'.php';
            if (!\$path = stream_resolve_include_path(\$path)) {
                return false;
            }
            require \$path;

            return true;
        }
    }

EOF;
        }
        // flatten array
        $classMap = array();
        $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
        if ($scanPsr0Packages) {
            foreach ($autoloads['psr-0'] as $namespace => $paths) {
                foreach ($paths as $dir) {
                    $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
                    $whitelist = sprintf('{%s/%s.+(?<!(?<!/)Test\\.php)$}', preg_quote(rtrim($dir, '/')), strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : '');
                    foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
                        if ('' === $namespace || 0 === strpos($class, $namespace)) {
                            $path = '/' . $filesystem->findShortestPath(getcwd(), $path, true);
                            if (!isset($classMap[$class])) {
                                $classMap[$class] = '$baseDir . ' . var_export($path, true) . ",\n";
                            }
                        }
                    }
                }
            }
        }
        foreach ($autoloads['classmap'] as $dir) {
            foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
                $path = '/' . $filesystem->findShortestPath(getcwd(), $path, true);
                $classMap[$class] = '$baseDir . ' . var_export($path, true) . ",\n";
            }
        }
        foreach ($classMap as $class => $code) {
            $classmapFile .= '    ' . var_export($class, true) . ' => ' . $code;
        }
        $classmapFile .= ");\n";
        $filesCode = "";
        $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
        foreach ($autoloads['files'] as $functionFile) {
            $filesCode .= '        require ' . $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile) . ";\n";
        }
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile);
        if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)) {
            file_put_contents($targetDir . '/include_paths.php', $includePathFile);
        }
        file_put_contents($vendorPath . '/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix));
        file_put_contents($targetDir . '/autoload_real' . $suffix . '.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix));
        copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php');
    }
Beispiel #9
0
    public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
    {
        $autoloadFile = <<<'EOF'
<?php

// autoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
    require __DIR__.'/ClassLoader.php';
}

$__composer_autoload_init = function() {
    $loader = new \Composer\Autoload\ClassLoader();

    $map = require __DIR__.'/autoload_namespaces.php';

    foreach ($map as $namespace => $path) {
        $loader->add($namespace, $path);
    }

    $loader->register();

    return $loader;
};

return $__composer_autoload_init();
EOF;
        $filesystem = new Filesystem();
        $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
        $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath);
        $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
        $namespacesFile = <<<EOF
<?php

// autoload_namespace.php generated by Composer

\$vendorDir = {$vendorDirCode};

return array(

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));
                        $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";
        file_put_contents($targetDir . '/autoload.php', $autoloadFile);
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php');
    }
 function setup()
 {
     $allModulesDir = $this->parameters->getAllModulesDirs();
     $allPluginsDir = $this->parameters->getAllPluginsDirs();
     $allModules = $this->parameters->getAllSingleModuleDirs();
     if (!count($allModulesDir) && !count($allModules) && !count($allPluginsDir)) {
         // nothing to setup
         return;
     }
     $appDir = $this->parameters->getAppDir();
     if (!$appDir) {
         throw new \Exception("No application directory is set in JelixParameters");
     }
     $configDir = $this->parameters->getVarConfigDir();
     $vendorDir = $this->parameters->getVendorDir();
     $fs = new Filesystem();
     // open the localconfig.ini.php file
     $localinifile = $configDir . 'localconfig.ini.php';
     if (!file_exists($localinifile)) {
         if (!file_exists($configDir)) {
             throw new \Exception('Configuration directory "' . $configDir . '" for the app does not exist');
         }
         file_put_contents($localinifile, "<" . "?php\n;die(''); ?" . ">\n\n");
     }
     $ini = new IniModifier($localinifile);
     if (count($allModulesDir)) {
         $modulesPath = '';
         foreach ($allModulesDir as $path) {
             $path = $fs->findShortestPath($appDir, $vendorDir . $path, true);
             if ($fs->isAbsolutePath($path)) {
                 $modulesPath .= ',' . $path;
             } else {
                 $modulesPath .= ',app:' . $path;
             }
         }
         $modulesPath = trim($modulesPath, ',');
         $ini->setValue('modulesPath', $modulesPath);
     }
     if (count($allPluginsDir)) {
         $pluginsPath = '';
         foreach ($allPluginsDir as $path) {
             $path = $fs->findShortestPath($appDir, $vendorDir . $path, true);
             if ($fs->isAbsolutePath($path)) {
                 $pluginsPath .= ',' . $path;
             } else {
                 $pluginsPath .= ',app:' . $path;
             }
         }
         $pluginsPath = trim($pluginsPath, ',');
         $ini->setValue('pluginsPath', $pluginsPath);
     }
     if (count($allModules)) {
         // erase first all "<module>.path" keys
         foreach ($ini->getValues('modules') as $key => $val) {
             if (preg_match("/\\.path\$/", $key)) {
                 $ini->removeValue($key, "modules");
             }
         }
         foreach ($allModules as $path) {
             $path = $fs->normalizePath($path);
             $moduleName = basename($path);
             $path = $fs->findShortestPath($appDir, $vendorDir . $path, true);
             if (!$fs->isAbsolutePath($path)) {
                 $path = 'app:' . $path;
             }
             $ini->setValue($moduleName . '.path', $path, 'modules');
         }
     }
     $ini->save();
 }
Beispiel #11
0
    public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $bcLinks = false)
    {
        $filesystem = new Filesystem();
        $filesystem->ensureDirectoryExists($installationManager->getVendorPath());
        $filesystem->ensureDirectoryExists($targetDir);
        $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
        $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
        $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
        $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
        $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
        $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
        $namespacesFile = <<<EOF
<?php

// autoload_namespace.php generated by Composer

\$vendorDir = {$vendorPathCode};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
        $autoloads = $this->parseAutoloads($packageMap);
        foreach ($autoloads['psr-0'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
            }
            $exportedPrefix = var_export($namespace, true);
            $namespacesFile .= "    {$exportedPrefix} => ";
            if (count($exportedPaths) > 1) {
                $namespacesFile .= "array(" . implode(', ', $exportedPaths) . "),\n";
            } else {
                $namespacesFile .= $exportedPaths[0] . ",\n";
            }
        }
        $namespacesFile .= ");\n";
        $classmapFile = <<<EOF
<?php

// autoload_classmap.php generated by Composer

\$vendorDir = {$vendorPathCode};
\$baseDir = {$appBaseDirCode};

return array(

EOF;
        // add custom psr-0 autoloading if the root package has a target dir
        $targetDirLoader = null;
        $mainAutoload = $mainPackage->getAutoload();
        if ($mainPackage->getTargetDir() && $mainAutoload['psr-0']) {
            $levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/')));
            $prefixes = implode(', ', array_map(function ($prefix) {
                return var_export($prefix, true);
            }, array_keys($mainAutoload['psr-0'])));
            $baseDirFromVendorDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
            $targetDirLoader = <<<EOF
    spl_autoload_register(function(\$class) {
        \$dir = {$baseDirFromVendorDirCode} . '/';
        \$prefixes = array({$prefixes});
        foreach (\$prefixes as \$prefix) {
            if (0 !== strpos(\$class, \$prefix)) {
                continue;
            }
            \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), {$levels})).'.php';
            if (!stream_resolve_include_path(\$path)) {
                return false;
            }
            require_once \$path;

            return true;
        }
    });


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, true);
                $classmapFile .= '    ' . var_export($class, true) . ' => $baseDir . ' . var_export($path, true) . ",\n";
            }
        }
        $classmapFile .= ");\n";
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile);
        if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)) {
            file_put_contents($targetDir . '/include_paths.php', $includePathFile);
        }
        file_put_contents($vendorPath . '/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, true, true, (bool) $includePathFile, $targetDirLoader));
        copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php');
        // TODO BC feature, remove after June 15th
        if ($bcLinks) {
            $filesystem->ensureDirectoryExists($vendorPath . '/.composer');
            $deprecated = "// Deprecated file, use the one in root of vendor dir\n" . "trigger_error(__FILE__.' is deprecated, please use vendor/autoload.php or vendor/composer/autoload_* instead'.PHP_EOL.'See https://groups.google.com/forum/#!msg/composer-dev/fWIs3KocwoA/nU3aLko9LhQJ for details', E_USER_DEPRECATED);\n";
            file_put_contents($vendorPath . '/.composer/autoload_namespaces.php', "<?php\n{$deprecated}\nreturn include dirname(__DIR__).'/composer/autoload_namespaces.php';\n");
            file_put_contents($vendorPath . '/.composer/autoload_classmap.php', "<?php\n{$deprecated}\nreturn include dirname(__DIR__).'/composer/autoload_classmap.php';\n");
            file_put_contents($vendorPath . '/.composer/autoload.php', "<?php\n{$deprecated}\nreturn include dirname(__DIR__).'/autoload.php';\n");
            file_put_contents($vendorPath . '/.composer/ClassLoader.php', "<?php\n{$deprecated}\nreturn include dirname(__DIR__).'/composer/ClassLoader.php';\n");
            if ($includePathFile) {
                file_put_contents($vendorPath . '/.composer/include_paths.php', "<?php\n{$deprecated}\nreturn include dirname(__DIR__).'/composer/include_paths.php';\n");
            }
        }
    }
Beispiel #12
0
    public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
    {
        $filesystem = new Filesystem();
        $filesystem->ensureDirectoryExists($installationManager->getVendorPath());
        $filesystem->ensureDirectoryExists($targetDir);
        $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
        $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
        $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(

EOF;
        $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
        $autoloads = $this->parseAutoloads($packageMap);
        foreach ($autoloads['psr-0'] as $namespace => $paths) {
            $exportedPaths = array();
            foreach ($paths as $path) {
                $path = strtr($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 . ';
                    }
                } 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";
        $classmapFile = <<<EOF
<?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, true);
                $classmapFile .= '    ' . var_export($class, true) . ' => $baseDir . ' . var_export($path, true) . ",\n";
            }
        }
        $classmapFile .= ");\n";
        file_put_contents($targetDir . '/autoload_namespaces.php', $namespacesFile);
        file_put_contents($targetDir . '/autoload_classmap.php', $classmapFile);
        if ($includePathFile = $this->getIncludePathsFile($packageMap)) {
            file_put_contents($targetDir . '/include_paths.php', $includePathFile);
        }
        file_put_contents($targetDir . '/autoload.php', $this->getAutoloadFile(true, true, (bool) $includePathFile));
        copy(__DIR__ . '/ClassLoader.php', $targetDir . '/ClassLoader.php');
    }