[Autoload] Generate the autoloader_real file in the target directory

main
Victor Berchet 12 years ago
parent 2c065416eb
commit 4c82c35dbc

@ -90,13 +90,13 @@ EOF;
$prefixes = implode(', ', array_map(function ($prefix) {
return var_export($prefix, true);
}, array_keys($mainAutoload['psr-0'])));
$baseDirFromVendorDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
$baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, getcwd(), true);
$targetDirLoader = <<<EOF
public static function autoload(\$class)
{
\$dir = $baseDirFromVendorDirCode . '/';
\$dir = $baseDirFromTargetDirCode . '/';
\$prefixes = array($prefixes);
foreach (\$prefixes as \$prefix) {
if (0 !== strpos(\$class, \$prefix)) {
@ -136,8 +136,8 @@ EOF;
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($suffix));
file_put_contents($vendorPath.'/autoload_real'.$suffix.'.php', $this->getAutoloadRealFile($vendorPathToTargetDirCode, true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $suffix));
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, $suffix));
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
}
@ -279,7 +279,7 @@ EOF;
return $baseDir.var_export($path, true);
}
protected function getAutoloadFile($suffix)
protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
{
@ -288,20 +288,21 @@ EOF;
// autoload.php generated by Composer
require_once 'autoload_real$suffix.php';
require_once $vendorPathToTargetDirCode . '/autoload_real$suffix.php';
return ComposerAutoloaderInit$suffix::getLoader();
AUTOLOAD;
}
protected function getAutoloadRealFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $suffix)
protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $suffix)
{
// TODO the class ComposerAutoloaderInit should be revert to a closure
// when APC has been fixed:
// - https://github.com/composer/composer/issues/959
// - https://bugs.php.net/bug.php?id=52144
// - https://bugs.php.net/bug.php?id=61576
// - https://bugs.php.net/bug.php?id=59298
if ($filesCode) {
$filesCode = "\n".$filesCode;
@ -312,7 +313,7 @@ AUTOLOAD;
// autoload_real$suffix.php generated by Composer
if (!class_exists('Composer\\\\Autoload\\\\ClassLoader', false)) {
require $vendorPathToTargetDirCode . '/ClassLoader.php';
require __DIR__ . '/ClassLoader.php';
}
class ComposerAutoloaderInit$suffix
@ -320,14 +321,13 @@ class ComposerAutoloaderInit$suffix
public static function getLoader()
{
\$loader = new \\Composer\\Autoload\\ClassLoader();
\$composerDir = $vendorPathToTargetDirCode;
HEADER;
if ($useIncludePath) {
$file .= <<<'INCLUDE_PATH'
$includePaths = require $composerDir . '/include_paths.php';
$includePaths = require __DIR__ . '/include_paths.php';
array_push($includePaths, get_include_path());
set_include_path(join(PATH_SEPARATOR, $includePaths));
@ -337,7 +337,7 @@ INCLUDE_PATH;
if ($usePSR0) {
$file .= <<<'PSR0'
$map = require $composerDir . '/autoload_namespaces.php';
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
@ -348,7 +348,7 @@ PSR0;
if ($useClassMap) {
$file .= <<<'CLASSMAP'
$classMap = require $composerDir . '/autoload_classmap.php';
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}

@ -159,7 +159,7 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/autoload_realTargetDir.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_realTargetDir.php');
}
public function testVendorsAutoloading()
@ -276,7 +276,7 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'FilesAutoload');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/autoload_realFilesAutoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php');
include $this->vendorDir . '/autoload.php';
$this->assertTrue(function_exists('testFilesAutoloadGeneration1'));

@ -2,6 +2,6 @@
// autoload.php generated by Composer
require_once 'autoload_realFilesAutoload.php';
require_once __DIR__ . '/composer' . '/autoload_realFilesAutoload.php';
return ComposerAutoloaderInitFilesAutoload::getLoader();

@ -2,7 +2,7 @@
// autoload_realFilesAutoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__ . '/composer' . '/ClassLoader.php';
require __DIR__ . '/ClassLoader.php';
}
class ComposerAutoloaderInitFilesAutoload
@ -10,14 +10,13 @@ class ComposerAutoloaderInitFilesAutoload
public static function getLoader()
{
$loader = new \Composer\Autoload\ClassLoader();
$composerDir = __DIR__ . '/composer';
$map = require $composerDir . '/autoload_namespaces.php';
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$classMap = require $composerDir . '/autoload_classmap.php';
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}

@ -2,7 +2,7 @@
// autoload_realTargetDir.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__ . '/composer' . '/ClassLoader.php';
require __DIR__ . '/ClassLoader.php';
}
class ComposerAutoloaderInitTargetDir
@ -10,14 +10,13 @@ class ComposerAutoloaderInitTargetDir
public static function getLoader()
{
$loader = new \Composer\Autoload\ClassLoader();
$composerDir = __DIR__ . '/composer';
$map = require $composerDir . '/autoload_namespaces.php';
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$classMap = require $composerDir . '/autoload_classmap.php';
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
@ -31,7 +30,7 @@ class ComposerAutoloaderInitTargetDir
public static function autoload($class)
{
$dir = dirname(__DIR__) . '/';
$dir = dirname(dirname(__DIR__)) . '/';
$prefixes = array('Main\\Foo', 'Main\\Bar');
foreach ($prefixes as $prefix) {
if (0 !== strpos($class, $prefix)) {

@ -2,6 +2,6 @@
// autoload.php generated by Composer
require_once 'autoload_realTargetDir.php';
require_once __DIR__ . '/composer' . '/autoload_realTargetDir.php';
return ComposerAutoloaderInitTargetDir::getLoader();

Loading…
Cancel
Save