1
0
Fork 0

Generate a map separated from the autoloader, fixes #74

Also the loader is now returned by the autoload.php
pull/77/merge
Jordi Boggiano 2011-10-30 20:29:06 +01:00
parent b100df33d8
commit 1ca3e5e5e2
2 changed files with 34 additions and 17 deletions

View File

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

View File

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