Generate a map separated from the autoloader, fixes #74

Also the loader is now returned by the autoload.php
main
Jordi Boggiano 13 years ago
parent b100df33d8
commit 1ca3e5e5e2

@ -35,39 +35,56 @@ class AutoloadGenerator
$this->installationManager = $installationManager;
}
public function dump($targetFilename)
public function dump($targetDir)
{
$autoloads = $this->parseAutoloads();
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
$autoloadFile .= <<<'EOF'
// autoload.php generated by Composer
function init() {
$loader = new ClassLoader();
$map = require __DIR__.'/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$loader->register();
return $loader;
}
$file = file_get_contents(__DIR__.'/ClassLoader.php');
return init();
EOF;
$file .= <<<'EOF'
$namespacesFile = <<<'EOF'
<?php
// autoload.php generated by composer
// autoload_namespace.php generated by Composer
$loader = new ClassLoader();
return array(
EOF;
$autoloads = $this->parseAutoloads();
if (isset($autoloads['psr-0'])) {
foreach ($autoloads['psr-0'] as $def) {
foreach ($def['mapping'] as $prefix => $path) {
$exportedPrefix = var_export($prefix, true);
$exportedPath = var_export(($def['path'] ? '/'.$def['path'] : '').'/'.$path, true);
$file .= <<<EOF
\$loader->add($exportedPrefix, dirname(dirname(__DIR__)).$exportedPath);
EOF;
$namespacesFile .= " $exportedPrefix => dirname(dirname(__DIR__)).$exportedPath,\n";
}
}
}
$file .= <<<'EOF'
$loader->register();
EOF;
$namespacesFile .= ");\n";
file_put_contents($targetFilename, $file);
file_put_contents($targetDir.'/autoload.php', $autoloadFile);
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
}
private function parseAutoloads()

@ -115,9 +115,9 @@ EOT
$localRepo->write();
$output->writeln('> Generating autoload.php');
$output->writeln('> Generating autoload files');
$generator = new AutoloadGenerator($localRepo, $composer->getPackage(), $installationManager);
$generator->dump('vendor/.composer/autoload.php');
$generator->dump('vendor/.composer/');
$output->writeln('> Done');
}

Loading…
Cancel
Save