1
0
Fork 0

[Autoload] Generate the autoloader_real file in the target directory

pull/963/head
Victor Berchet 2012-08-06 10:56:57 +02:00
parent 2c065416eb
commit 4c82c35dbc
6 changed files with 23 additions and 25 deletions

View File

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

View File

@ -159,7 +159,7 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir'); $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_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() public function testVendorsAutoloading()
@ -276,7 +276,7 @@ class AutoloadGeneratorTest extends TestCase
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'FilesAutoload'); $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_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'; include $this->vendorDir . '/autoload.php';
$this->assertTrue(function_exists('testFilesAutoloadGeneration1')); $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));

View File

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

View File

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

View File

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

View File

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