Allow root packages packages with target-dir to be autoloaded, fixes #139
parent
437dd36e7d
commit
66135538c1
|
@ -79,6 +79,36 @@ return array(
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
|
// add custom psr-0 autoloading if the root package has a target dir
|
||||||
|
$targetDirLoader = null;
|
||||||
|
$mainAutoload = $mainPackage->getAutoload();
|
||||||
|
if ($mainPackage->getTargetDir() && $mainAutoload['psr-0']) {
|
||||||
|
$levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/')));
|
||||||
|
$prefixes = implode("', '", array_map(function ($prefix) {
|
||||||
|
return var_export($prefix, true);
|
||||||
|
}, array_keys($mainAutoload['psr-0'])));
|
||||||
|
$baseDirFromTargetDirCode = $filesystem->findShortestPathCode(realpath($targetDir), getcwd(), true);
|
||||||
|
|
||||||
|
$targetDirLoader = <<<EOF
|
||||||
|
spl_autoload_register(function(\$class) {
|
||||||
|
\$prefixes = array($prefixes);
|
||||||
|
foreach (\$prefixes as \$prefix) {
|
||||||
|
if (0 !== strpos(\$class, \$prefix)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
\$path = $baseDirFromTargetDirCode . '/' . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
|
||||||
|
if (!stream_resolve_include_path(\$path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
require_once \$path;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
EOF;
|
||||||
|
}
|
||||||
|
|
||||||
// flatten array
|
// flatten array
|
||||||
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
|
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
|
||||||
foreach ($autoloads['classmap'] as $dir) {
|
foreach ($autoloads['classmap'] as $dir) {
|
||||||
|
@ -94,7 +124,7 @@ EOF;
|
||||||
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) {
|
if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) {
|
||||||
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
|
file_put_contents($targetDir.'/include_paths.php', $includePathFile);
|
||||||
}
|
}
|
||||||
file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
|
file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile, $targetDirLoader));
|
||||||
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
||||||
|
|
||||||
// TODO BC feature, add E_DEPRECATED in autoload.php on April 30th, remove after May 30th
|
// TODO BC feature, add E_DEPRECATED in autoload.php on April 30th, remove after May 30th
|
||||||
|
@ -242,7 +272,7 @@ EOF;
|
||||||
return $baseDir.var_export($path, true);
|
return $baseDir.var_export($path, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
|
protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader)
|
||||||
{
|
{
|
||||||
$file = <<<'HEADER'
|
$file = <<<'HEADER'
|
||||||
<?php
|
<?php
|
||||||
|
@ -290,6 +320,8 @@ PSR0;
|
||||||
CLASSMAP;
|
CLASSMAP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$file .= $targetDirLoader;
|
||||||
|
|
||||||
return $file . <<<'FOOTER'
|
return $file . <<<'FOOTER'
|
||||||
$loader->register();
|
$loader->register();
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,22 @@ class AutoloadGeneratorTest extends TestCase
|
||||||
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap');
|
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMainPackageAutoloadingWithTargetDir()
|
||||||
|
{
|
||||||
|
$package = new MemoryPackage('a', '1.0', '1.0');
|
||||||
|
$package->setAutoload(array(
|
||||||
|
'psr-0' => array('Main\\Foo' => ''),
|
||||||
|
));
|
||||||
|
$package->setTargetDir('Main/Foo');
|
||||||
|
|
||||||
|
$this->repository->expects($this->once())
|
||||||
|
->method('getPackages')
|
||||||
|
->will($this->returnValue(array()));
|
||||||
|
|
||||||
|
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
|
||||||
|
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/.composer/autoload.php');
|
||||||
|
}
|
||||||
|
|
||||||
public function testVendorsAutoloading()
|
public function testVendorsAutoloading()
|
||||||
{
|
{
|
||||||
$package = new MemoryPackage('a', '1.0', '1.0');
|
$package = new MemoryPackage('a', '1.0', '1.0');
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// autoload.php generated by Composer
|
||||||
|
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
|
||||||
|
require __DIR__.'/ClassLoader.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
return call_user_func(function() {
|
||||||
|
$loader = new \Composer\Autoload\ClassLoader();
|
||||||
|
|
||||||
|
$map = require __DIR__.'/autoload_namespaces.php';
|
||||||
|
foreach ($map as $namespace => $path) {
|
||||||
|
$loader->add($namespace, $path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$classMap = require __DIR__.'/autoload_classmap.php';
|
||||||
|
if ($classMap) {
|
||||||
|
$loader->addClassMap($classMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
spl_autoload_register(function($class) {
|
||||||
|
$prefixes = array('Main\\Foo');
|
||||||
|
foreach ($prefixes as $prefix) {
|
||||||
|
if (0 !== strpos($class, $prefix)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$path = dirname(dirname(__DIR__)) . '/' . implode('/', array_slice(explode('\\', $class), 2)).'.php';
|
||||||
|
if (!stream_resolve_include_path($path)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
require_once $path;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$loader->register();
|
||||||
|
|
||||||
|
return $loader;
|
||||||
|
});
|
Loading…
Reference in New Issue