Make use of Filesystem class in AutoloadGenerator
parent
abb926a60c
commit
bc88d86983
|
@ -17,6 +17,7 @@ use Composer\Json\JsonFile;
|
||||||
use Composer\Package\Loader\JsonLoader;
|
use Composer\Package\Loader\JsonLoader;
|
||||||
use Composer\Package\PackageInterface;
|
use Composer\Package\PackageInterface;
|
||||||
use Composer\Repository\RepositoryInterface;
|
use Composer\Repository\RepositoryInterface;
|
||||||
|
use Composer\Downloader\Util\Filesystem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
|
@ -27,7 +28,6 @@ class AutoloadGenerator
|
||||||
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
|
public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
|
||||||
{
|
{
|
||||||
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
|
$autoloadFile = file_get_contents(__DIR__.'/ClassLoader.php');
|
||||||
|
|
||||||
$autoloadFile .= <<<'EOF'
|
$autoloadFile .= <<<'EOF'
|
||||||
|
|
||||||
// autoload.php generated by Composer
|
// autoload.php generated by Composer
|
||||||
|
@ -49,12 +49,17 @@ function init() {
|
||||||
return init();
|
return init();
|
||||||
EOF;
|
EOF;
|
||||||
|
|
||||||
$namespacesFile = <<<'EOF'
|
$filesystem = new Filesystem();
|
||||||
|
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
|
||||||
|
$relVendorPath = ltrim(substr($vendorPath, strlen(getcwd())), '/');
|
||||||
|
$vendorDirCode = $filesystem->findShortestPathCode($targetDir, $vendorPath, true);
|
||||||
|
|
||||||
|
$namespacesFile = <<<EOF
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// autoload_namespace.php generated by Composer
|
// autoload_namespace.php generated by Composer
|
||||||
|
|
||||||
$baseDir = dirname(__DIR__);
|
\$vendorDir = $vendorDirCode;
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
|
|
||||||
|
@ -71,26 +76,26 @@ EOF;
|
||||||
|
|
||||||
// add main package
|
// add main package
|
||||||
$packageMap[] = array($mainPackage, '');
|
$packageMap[] = array($mainPackage, '');
|
||||||
|
|
||||||
$autoloads = $this->parseAutoloads($packageMap);
|
$autoloads = $this->parseAutoloads($packageMap);
|
||||||
$vendorPath = $installationManager->getVendorPath();
|
|
||||||
|
|
||||||
$realVendorPath = realpath($vendorPath);
|
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
|
||||||
$vendorPathDepth = substr_count(strtr(substr($realVendorPath, strlen(getcwd())), '\\', '/'), '/');
|
$appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir);
|
||||||
$appBaseDir = str_repeat('dirname(', $vendorPathDepth).'$baseDir'.str_repeat(')', $vendorPathDepth);
|
|
||||||
|
|
||||||
if (isset($autoloads['psr-0'])) {
|
if (isset($autoloads['psr-0'])) {
|
||||||
foreach ($autoloads['psr-0'] as $def) {
|
foreach ($autoloads['psr-0'] as $def) {
|
||||||
if (!$this->isAbsolutePath($def['path'])) {
|
$def['path'] = strtr($def['path'], '\\', '/');
|
||||||
if (strpos($def['path'], $vendorPath) === 0) {
|
$baseDir = '';
|
||||||
$def['path'] = substr($def['path'], strlen($vendorPath));
|
if (!$filesystem->isAbsolutePath($def['path'])) {
|
||||||
$baseDir = '$baseDir . ';
|
if (strpos($def['path'], $relVendorPath) === 0) {
|
||||||
|
$def['path'] = substr($def['path'], strlen($relVendorPath));
|
||||||
|
$baseDir = '$vendorDir . ';
|
||||||
} else {
|
} else {
|
||||||
$def['path'] = '/'.$def['path'];
|
$def['path'] = '/'.$def['path'];
|
||||||
$baseDir = $appBaseDir . ' . ';
|
$baseDir = $appBaseDir . ' . ';
|
||||||
}
|
}
|
||||||
} else {
|
} elseif (strpos($def['path'], $vendorPath) === 0) {
|
||||||
$baseDir = '';
|
$def['path'] = substr($def['path'], strlen($vendorPath));
|
||||||
|
$baseDir = '$vendorDir . ';
|
||||||
}
|
}
|
||||||
$exportedPrefix = var_export($def['namespace'], true);
|
$exportedPrefix = var_export($def['namespace'], true);
|
||||||
$exportedPath = var_export($def['path'], true);
|
$exportedPath = var_export($def['path'], true);
|
||||||
|
@ -158,9 +163,4 @@ EOF;
|
||||||
|
|
||||||
return $loader;
|
return $loader;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isAbsolutePath($path)
|
|
||||||
{
|
|
||||||
return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue