[Autoload] generate an autoload_real.php file
parent
65e95ed796
commit
2c065416eb
|
@ -25,7 +25,7 @@ use Composer\Util\Filesystem;
|
||||||
*/
|
*/
|
||||||
class AutoloadGenerator
|
class AutoloadGenerator
|
||||||
{
|
{
|
||||||
public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $classSuffix = '')
|
public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $suffix = '')
|
||||||
{
|
{
|
||||||
$filesystem = new Filesystem();
|
$filesystem = new Filesystem();
|
||||||
$filesystem->ensureDirectoryExists($config->get('vendor-dir'));
|
$filesystem->ensureDirectoryExists($config->get('vendor-dir'));
|
||||||
|
@ -94,23 +94,23 @@ EOF;
|
||||||
|
|
||||||
$targetDirLoader = <<<EOF
|
$targetDirLoader = <<<EOF
|
||||||
|
|
||||||
public static function autoload(\$class)
|
public static function autoload(\$class)
|
||||||
{
|
{
|
||||||
\$dir = $baseDirFromVendorDirCode . '/';
|
\$dir = $baseDirFromVendorDirCode . '/';
|
||||||
\$prefixes = array($prefixes);
|
\$prefixes = array($prefixes);
|
||||||
foreach (\$prefixes as \$prefix) {
|
foreach (\$prefixes as \$prefix) {
|
||||||
if (0 !== strpos(\$class, \$prefix)) {
|
if (0 !== strpos(\$class, \$prefix)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
\$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
|
|
||||||
if (!\$path = stream_resolve_include_path(\$path)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
require \$path;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
\$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
|
||||||
|
if (!\$path = stream_resolve_include_path(\$path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
require \$path;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ EOF;
|
||||||
$filesCode = "";
|
$filesCode = "";
|
||||||
$autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
|
$autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
|
||||||
foreach ($autoloads['files'] as $functionFile) {
|
foreach ($autoloads['files'] as $functionFile) {
|
||||||
$filesCode .= ' require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n";
|
$filesCode .= ' require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
|
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
|
||||||
|
@ -136,7 +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($vendorPathToTargetDirCode, true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $classSuffix));
|
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));
|
||||||
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +279,23 @@ EOF;
|
||||||
return $baseDir.var_export($path, true);
|
return $baseDir.var_export($path, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $classSuffix)
|
protected function getAutoloadFile($suffix)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return <<<AUTOLOAD
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// autoload.php generated by Composer
|
||||||
|
|
||||||
|
require_once 'autoload_real$suffix.php';
|
||||||
|
|
||||||
|
return ComposerAutoloaderInit$suffix::getLoader();
|
||||||
|
|
||||||
|
AUTOLOAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getAutoloadRealFile($vendorPathToTargetDirCode, $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:
|
||||||
|
@ -293,27 +310,26 @@ EOF;
|
||||||
$file = <<<HEADER
|
$file = <<<HEADER
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// autoload.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 $vendorPathToTargetDirCode . '/ClassLoader.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!class_exists('ComposerAutoloaderInit$classSuffix', false)) {
|
class ComposerAutoloaderInit$suffix
|
||||||
class ComposerAutoloaderInit$classSuffix
|
{
|
||||||
|
public static function getLoader()
|
||||||
{
|
{
|
||||||
public static function getLoader()
|
\$loader = new \\Composer\\Autoload\\ClassLoader();
|
||||||
{
|
\$composerDir = $vendorPathToTargetDirCode;
|
||||||
\$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 $composerDir . '/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));
|
||||||
|
|
||||||
|
|
||||||
INCLUDE_PATH;
|
INCLUDE_PATH;
|
||||||
|
@ -321,10 +337,10 @@ INCLUDE_PATH;
|
||||||
|
|
||||||
if ($usePSR0) {
|
if ($usePSR0) {
|
||||||
$file .= <<<'PSR0'
|
$file .= <<<'PSR0'
|
||||||
$map = require $composerDir . '/autoload_namespaces.php';
|
$map = require $composerDir . '/autoload_namespaces.php';
|
||||||
foreach ($map as $namespace => $path) {
|
foreach ($map as $namespace => $path) {
|
||||||
$loader->add($namespace, $path);
|
$loader->add($namespace, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PSR0;
|
PSR0;
|
||||||
|
@ -332,10 +348,10 @@ PSR0;
|
||||||
|
|
||||||
if ($useClassMap) {
|
if ($useClassMap) {
|
||||||
$file .= <<<'CLASSMAP'
|
$file .= <<<'CLASSMAP'
|
||||||
$classMap = require $composerDir . '/autoload_classmap.php';
|
$classMap = require $composerDir . '/autoload_classmap.php';
|
||||||
if ($classMap) {
|
if ($classMap) {
|
||||||
$loader->addClassMap($classMap);
|
$loader->addClassMap($classMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CLASSMAP;
|
CLASSMAP;
|
||||||
|
@ -343,7 +359,7 @@ CLASSMAP;
|
||||||
|
|
||||||
if ($targetDirLoader) {
|
if ($targetDirLoader) {
|
||||||
$file .= <<<REGISTER_AUTOLOAD
|
$file .= <<<REGISTER_AUTOLOAD
|
||||||
spl_autoload_register(array('ComposerAutoloaderInit$classSuffix', 'autoload'));
|
spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'autoload'));
|
||||||
|
|
||||||
|
|
||||||
REGISTER_AUTOLOAD;
|
REGISTER_AUTOLOAD;
|
||||||
|
@ -351,23 +367,21 @@ REGISTER_AUTOLOAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file .= <<<METHOD_FOOTER
|
$file .= <<<METHOD_FOOTER
|
||||||
\$loader->register();
|
\$loader->register();
|
||||||
$filesCode
|
$filesCode
|
||||||
return \$loader;
|
return \$loader;
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD_FOOTER;
|
METHOD_FOOTER;
|
||||||
|
|
||||||
$file .= $targetDirLoader;
|
$file .= $targetDirLoader;
|
||||||
|
|
||||||
return $file . <<<FOOTER
|
return $file . <<<FOOTER
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ComposerAutoloaderInit$classSuffix::getLoader();
|
|
||||||
|
|
||||||
FOOTER;
|
FOOTER;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -159,6 +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');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVendorsAutoloading()
|
public function testVendorsAutoloading()
|
||||||
|
@ -275,6 +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');
|
||||||
|
|
||||||
include $this->vendorDir . '/autoload.php';
|
include $this->vendorDir . '/autoload.php';
|
||||||
$this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
|
$this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
|
||||||
|
|
|
@ -1,36 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// autoload.php generated by Composer
|
// autoload.php generated by Composer
|
||||||
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
|
|
||||||
require __DIR__ . '/composer' . '/ClassLoader.php';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!class_exists('ComposerAutoloaderInitFilesAutoload', false)) {
|
require_once 'autoload_realFilesAutoload.php';
|
||||||
class ComposerAutoloaderInitFilesAutoload
|
|
||||||
{
|
|
||||||
public static function getLoader()
|
|
||||||
{
|
|
||||||
$loader = new \Composer\Autoload\ClassLoader();
|
|
||||||
$composerDir = __DIR__ . '/composer';
|
|
||||||
|
|
||||||
$map = require $composerDir . '/autoload_namespaces.php';
|
|
||||||
foreach ($map as $namespace => $path) {
|
|
||||||
$loader->add($namespace, $path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$classMap = require $composerDir . '/autoload_classmap.php';
|
|
||||||
if ($classMap) {
|
|
||||||
$loader->addClassMap($classMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
$loader->register();
|
|
||||||
|
|
||||||
require $vendorDir . '/a/a/test.php';
|
|
||||||
require $vendorDir . '/b/b/test2.php';
|
|
||||||
|
|
||||||
return $loader;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ComposerAutoloaderInitFilesAutoload::getLoader();
|
return ComposerAutoloaderInitFilesAutoload::getLoader();
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// autoload_realFilesAutoload.php generated by Composer
|
||||||
|
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
|
||||||
|
require __DIR__ . '/composer' . '/ClassLoader.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
class ComposerAutoloaderInitFilesAutoload
|
||||||
|
{
|
||||||
|
public static function getLoader()
|
||||||
|
{
|
||||||
|
$loader = new \Composer\Autoload\ClassLoader();
|
||||||
|
$composerDir = __DIR__ . '/composer';
|
||||||
|
|
||||||
|
$map = require $composerDir . '/autoload_namespaces.php';
|
||||||
|
foreach ($map as $namespace => $path) {
|
||||||
|
$loader->add($namespace, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$classMap = require $composerDir . '/autoload_classmap.php';
|
||||||
|
if ($classMap) {
|
||||||
|
$loader->addClassMap($classMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
$loader->register();
|
||||||
|
|
||||||
|
require $vendorDir . '/a/a/test.php';
|
||||||
|
require $vendorDir . '/b/b/test2.php';
|
||||||
|
|
||||||
|
return $loader;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// autoload_realTargetDir.php generated by Composer
|
||||||
|
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
|
||||||
|
require __DIR__ . '/composer' . '/ClassLoader.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
class ComposerAutoloaderInitTargetDir
|
||||||
|
{
|
||||||
|
public static function getLoader()
|
||||||
|
{
|
||||||
|
$loader = new \Composer\Autoload\ClassLoader();
|
||||||
|
$composerDir = __DIR__ . '/composer';
|
||||||
|
|
||||||
|
$map = require $composerDir . '/autoload_namespaces.php';
|
||||||
|
foreach ($map as $namespace => $path) {
|
||||||
|
$loader->add($namespace, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$classMap = require $composerDir . '/autoload_classmap.php';
|
||||||
|
if ($classMap) {
|
||||||
|
$loader->addClassMap($classMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
spl_autoload_register(array('ComposerAutoloaderInitTargetDir', 'autoload'));
|
||||||
|
|
||||||
|
$loader->register();
|
||||||
|
|
||||||
|
return $loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function autoload($class)
|
||||||
|
{
|
||||||
|
$dir = dirname(__DIR__) . '/';
|
||||||
|
$prefixes = array('Main\\Foo', 'Main\\Bar');
|
||||||
|
foreach ($prefixes as $prefix) {
|
||||||
|
if (0 !== strpos($class, $prefix)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$path = $dir . implode('/', array_slice(explode('\\', $class), 2)).'.php';
|
||||||
|
if (!$path = stream_resolve_include_path($path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
require $path;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,53 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// autoload.php generated by Composer
|
// autoload.php generated by Composer
|
||||||
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
|
|
||||||
require __DIR__ . '/composer' . '/ClassLoader.php';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!class_exists('ComposerAutoloaderInitTargetDir', false)) {
|
require_once 'autoload_realTargetDir.php';
|
||||||
class ComposerAutoloaderInitTargetDir
|
|
||||||
{
|
|
||||||
public static function getLoader()
|
|
||||||
{
|
|
||||||
$loader = new \Composer\Autoload\ClassLoader();
|
|
||||||
$composerDir = __DIR__ . '/composer';
|
|
||||||
|
|
||||||
$map = require $composerDir . '/autoload_namespaces.php';
|
|
||||||
foreach ($map as $namespace => $path) {
|
|
||||||
$loader->add($namespace, $path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$classMap = require $composerDir . '/autoload_classmap.php';
|
|
||||||
if ($classMap) {
|
|
||||||
$loader->addClassMap($classMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
spl_autoload_register(array('ComposerAutoloaderInitTargetDir', 'autoload'));
|
|
||||||
|
|
||||||
$loader->register();
|
|
||||||
|
|
||||||
return $loader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function autoload($class)
|
|
||||||
{
|
|
||||||
$dir = dirname(__DIR__) . '/';
|
|
||||||
$prefixes = array('Main\\Foo', 'Main\\Bar');
|
|
||||||
foreach ($prefixes as $prefix) {
|
|
||||||
if (0 !== strpos($class, $prefix)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$path = $dir . implode('/', array_slice(explode('\\', $class), 2)).'.php';
|
|
||||||
if (!$path = stream_resolve_include_path($path)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
require $path;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ComposerAutoloaderInitTargetDir::getLoader();
|
return ComposerAutoloaderInitTargetDir::getLoader();
|
||||||
|
|
Loading…
Reference in New Issue