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;
}
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;
$namespacesFile = <<<'EOF'
<?php
// autoload_namespace.php generated by Composer
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();
$namespacesFile .= ");\n";
EOF;
file_put_contents($targetFilename, $file);
file_put_contents($targetDir.'/autoload.php', $autoloadFile);
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
}
private function parseAutoloads()

View File

@ -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');
}