426 lines
14 KiB
PHP
426 lines
14 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Composer.
|
|
*
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Composer\Autoload;
|
|
|
|
use Composer\Config;
|
|
use Composer\Installer\InstallationManager;
|
|
use Composer\Package\AliasPackage;
|
|
use Composer\Package\PackageInterface;
|
|
use Composer\Repository\RepositoryInterface;
|
|
use Composer\Util\Filesystem;
|
|
|
|
/**
|
|
* @author Igor Wiedler <igor@wiedler.ch>
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
*/
|
|
class AutoloadGenerator
|
|
{
|
|
public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
|
|
{
|
|
$filesystem = new Filesystem();
|
|
$filesystem->ensureDirectoryExists($config->get('vendor-dir'));
|
|
$vendorPath = strtr(realpath($config->get('vendor-dir')), '\\', '/');
|
|
$targetDir = $vendorPath.'/'.$targetDir;
|
|
$filesystem->ensureDirectoryExists($targetDir);
|
|
|
|
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
|
|
$vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
|
|
$vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true);
|
|
|
|
$appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
|
|
$appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
|
|
|
|
$namespacesFile = <<<EOF
|
|
<?php
|
|
|
|
// autoload_namespaces.php generated by Composer
|
|
|
|
\$vendorDir = $vendorPathCode;
|
|
\$baseDir = $appBaseDirCode;
|
|
|
|
return array(
|
|
|
|
EOF;
|
|
|
|
$packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages());
|
|
$autoloads = $this->parseAutoloads($packageMap);
|
|
|
|
foreach ($autoloads['psr-0'] as $namespace => $paths) {
|
|
$exportedPaths = array();
|
|
foreach ($paths as $path) {
|
|
$exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path);
|
|
}
|
|
$exportedPrefix = var_export($namespace, true);
|
|
$namespacesFile .= " $exportedPrefix => ";
|
|
if (count($exportedPaths) > 1) {
|
|
$namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n";
|
|
} else {
|
|
$namespacesFile .= $exportedPaths[0].",\n";
|
|
}
|
|
}
|
|
$namespacesFile .= ");\n";
|
|
|
|
$classmapFile = <<<EOF
|
|
<?php
|
|
|
|
// autoload_classmap.php generated by Composer
|
|
|
|
\$vendorDir = $vendorPathCode;
|
|
\$baseDir = $appBaseDirCode;
|
|
|
|
return array(
|
|
|
|
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($targetDir, getcwd(), true);
|
|
|
|
$targetDirLoader = <<<EOF
|
|
|
|
public static function autoload(\$class)
|
|
{
|
|
\$dir = $baseDirFromTargetDirCode . '/';
|
|
\$prefixes = array($prefixes);
|
|
foreach (\$prefixes as \$prefix) {
|
|
if (0 !== strpos(\$class, \$prefix)) {
|
|
continue;
|
|
}
|
|
\$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
|
|
if (!\$path = stream_resolve_include_path(\$path)) {
|
|
return false;
|
|
}
|
|
require \$path;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
EOF;
|
|
}
|
|
|
|
// flatten array
|
|
$classMap = array();
|
|
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
|
|
|
|
if ($scanPsr0Packages) {
|
|
foreach ($autoloads['psr-0'] as $namespace => $paths) {
|
|
foreach ($paths as $dir) {
|
|
$dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
|
|
$whitelist = sprintf(
|
|
'{%s/%s.+(?<!(?<!/)Test\.php)$}',
|
|
preg_quote(rtrim($dir, '/')),
|
|
strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
|
|
);
|
|
foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
|
|
if ('' === $namespace || 0 === strpos($class, $namespace)) {
|
|
$path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
|
|
if (!isset($classMap[$class])) {
|
|
$classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
foreach ($autoloads['classmap'] as $dir) {
|
|
foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
|
|
$path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
|
|
$classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
|
|
}
|
|
}
|
|
foreach ($classMap as $class => $code) {
|
|
$classmapFile .= ' '.var_export($class, true).' => '.$code;
|
|
}
|
|
$classmapFile .= ");\n";
|
|
|
|
$filesCode = "";
|
|
$autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
|
|
foreach ($autoloads['files'] as $functionFile) {
|
|
$filesCode .= ' require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n";
|
|
}
|
|
|
|
file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
|
|
file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
|
|
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($vendorPathToTargetDirCode, $suffix));
|
|
file_put_contents($targetDir.'/autoload_real'.$suffix.'.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix));
|
|
copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
|
|
}
|
|
|
|
public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages)
|
|
{
|
|
// build package => install path map
|
|
$packageMap = array();
|
|
|
|
// add main package
|
|
$packageMap[] = array($mainPackage, '');
|
|
|
|
foreach ($packages as $package) {
|
|
if ($package instanceof AliasPackage) {
|
|
continue;
|
|
}
|
|
$packageMap[] = array(
|
|
$package,
|
|
$installationManager->getInstallPath($package)
|
|
);
|
|
}
|
|
|
|
return $packageMap;
|
|
}
|
|
|
|
/**
|
|
* Compiles an ordered list of namespace => path mappings
|
|
*
|
|
* @param array $packageMap array of array(package, installDir-relative-to-composer.json)
|
|
* @return array array('psr-0' => array('Ns\\Foo' => array('installDir')))
|
|
*/
|
|
public function parseAutoloads(array $packageMap)
|
|
{
|
|
$autoloads = array('classmap' => array(), 'psr-0' => array(), 'files' => array());
|
|
foreach ($packageMap as $item) {
|
|
list($package, $installPath) = $item;
|
|
|
|
if (null !== $package->getTargetDir()) {
|
|
$installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
|
|
}
|
|
|
|
foreach ($package->getAutoload() as $type => $mapping) {
|
|
// skip misconfigured packages
|
|
if (!is_array($mapping)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($mapping as $namespace => $paths) {
|
|
foreach ((array) $paths as $path) {
|
|
$autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($autoloads as $type => $maps) {
|
|
krsort($autoloads[$type]);
|
|
}
|
|
|
|
return $autoloads;
|
|
}
|
|
|
|
/**
|
|
* Registers an autoloader based on an autoload map returned by parseAutoloads
|
|
*
|
|
* @param array $autoloads see parseAutoloads return value
|
|
* @return ClassLoader
|
|
*/
|
|
public function createLoader(array $autoloads)
|
|
{
|
|
$loader = new ClassLoader();
|
|
|
|
if (isset($autoloads['psr-0'])) {
|
|
foreach ($autoloads['psr-0'] as $namespace => $path) {
|
|
$loader->add($namespace, $path);
|
|
}
|
|
}
|
|
|
|
return $loader;
|
|
}
|
|
|
|
protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)
|
|
{
|
|
$includePaths = array();
|
|
|
|
foreach ($packageMap as $item) {
|
|
list($package, $installPath) = $item;
|
|
|
|
if (null !== $package->getTargetDir() && strlen($package->getTargetDir()) > 0) {
|
|
$installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
|
|
}
|
|
|
|
foreach ($package->getIncludePaths() as $includePath) {
|
|
$includePath = trim($includePath, '/');
|
|
$includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
|
|
}
|
|
}
|
|
|
|
if (!$includePaths) {
|
|
return;
|
|
}
|
|
|
|
$includePathsFile = <<<EOF
|
|
<?php
|
|
|
|
// include_paths.php generated by Composer
|
|
|
|
\$vendorDir = $vendorPathCode;
|
|
\$baseDir = $appBaseDirCode;
|
|
|
|
return array(
|
|
|
|
EOF;
|
|
|
|
foreach ($includePaths as $path) {
|
|
$includePathsFile .= " " . $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path) . ",\n";
|
|
}
|
|
|
|
return $includePathsFile . ");\n";
|
|
}
|
|
|
|
protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
|
|
{
|
|
$path = strtr($path, '\\', '/');
|
|
$baseDir = '';
|
|
if (!$filesystem->isAbsolutePath($path)) {
|
|
if (strpos($path, $relVendorPath) === 0) {
|
|
// path starts with vendor dir
|
|
$path = substr($path, strlen($relVendorPath));
|
|
$baseDir = '$vendorDir . ';
|
|
} else {
|
|
$path = '/'.$path;
|
|
$baseDir = '$baseDir . ';
|
|
}
|
|
} elseif (strpos($path, $vendorPath) === 0) {
|
|
$path = substr($path, strlen($vendorPath));
|
|
$baseDir = '$vendorDir . ';
|
|
}
|
|
|
|
return $baseDir.var_export($path, true);
|
|
}
|
|
|
|
protected function getPath(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
|
|
{
|
|
$path = strtr($path, '\\', '/');
|
|
if (!$filesystem->isAbsolutePath($path)) {
|
|
if (strpos($path, $relVendorPath) === 0) {
|
|
// path starts with vendor dir
|
|
return $vendorPath . substr($path, strlen($relVendorPath));
|
|
}
|
|
|
|
return strtr(getcwd(), '\\', '/').'/'.$path;
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
|
|
{
|
|
return <<<AUTOLOAD
|
|
<?php
|
|
|
|
// autoload.php generated by Composer
|
|
|
|
require_once $vendorPathToTargetDirCode . '/autoload_real$suffix.php';
|
|
|
|
return ComposerAutoloaderInit$suffix::getLoader();
|
|
|
|
AUTOLOAD;
|
|
}
|
|
|
|
protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $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\n".rtrim($filesCode);
|
|
}
|
|
|
|
$file = <<<HEADER
|
|
<?php
|
|
|
|
// autoload_real$suffix.php generated by Composer
|
|
|
|
require __DIR__ . '/ClassLoader.php';
|
|
|
|
class ComposerAutoloaderInit$suffix
|
|
{
|
|
public static function getLoader()
|
|
{
|
|
\$loader = new \\Composer\\Autoload\\ClassLoader();
|
|
\$vendorDir = $vendorPathCode;
|
|
\$baseDir = $appBaseDirCode;
|
|
|
|
|
|
HEADER;
|
|
|
|
if ($useIncludePath) {
|
|
$file .= <<<'INCLUDE_PATH'
|
|
$includePaths = require __DIR__ . '/include_paths.php';
|
|
array_push($includePaths, get_include_path());
|
|
set_include_path(join(PATH_SEPARATOR, $includePaths));
|
|
|
|
|
|
INCLUDE_PATH;
|
|
}
|
|
|
|
if ($usePSR0) {
|
|
$file .= <<<'PSR0'
|
|
$map = require __DIR__ . '/autoload_namespaces.php';
|
|
foreach ($map as $namespace => $path) {
|
|
$loader->add($namespace, $path);
|
|
}
|
|
|
|
|
|
PSR0;
|
|
}
|
|
|
|
if ($useClassMap) {
|
|
$file .= <<<'CLASSMAP'
|
|
$classMap = require __DIR__ . '/autoload_classmap.php';
|
|
if ($classMap) {
|
|
$loader->addClassMap($classMap);
|
|
}
|
|
|
|
|
|
CLASSMAP;
|
|
}
|
|
|
|
if ($targetDirLoader) {
|
|
$file .= <<<REGISTER_AUTOLOAD
|
|
spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'autoload'));
|
|
|
|
|
|
REGISTER_AUTOLOAD;
|
|
|
|
}
|
|
|
|
$file .= <<<METHOD_FOOTER
|
|
\$loader->register();{$filesCode}
|
|
|
|
return \$loader;
|
|
}
|
|
|
|
METHOD_FOOTER;
|
|
|
|
$file .= $targetDirLoader;
|
|
|
|
return $file . <<<FOOTER
|
|
}
|
|
|
|
FOOTER;
|
|
|
|
}
|
|
|
|
}
|