Change detection of config/cache/data dirs
parent
7d63eb5718
commit
e8a6cb536b
|
@ -35,6 +35,21 @@ use Composer\Package\Version\VersionParser;
|
|||
*/
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private static function useXdg()
|
||||
{
|
||||
foreach (array_keys($_SERVER) as $key) {
|
||||
if (substr($key, 0, 4) === 'XDG_') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \RuntimeException
|
||||
|
@ -56,27 +71,39 @@ class Factory
|
|||
protected static function getHomeDir()
|
||||
{
|
||||
$home = getenv('COMPOSER_HOME');
|
||||
if (!$home) {
|
||||
if ($home) {
|
||||
return $home;
|
||||
}
|
||||
|
||||
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
||||
if (!getenv('APPDATA')) {
|
||||
throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly');
|
||||
}
|
||||
$home = strtr(getenv('APPDATA'), '\\', '/') . '/Composer';
|
||||
} else {
|
||||
|
||||
return $home;
|
||||
}
|
||||
|
||||
$userDir = self::getUserDir();
|
||||
|
||||
if (getenv('XDG_CONFIG_DIRS')) {
|
||||
if (is_dir($userDir . '/.composer')) {
|
||||
$home = $userDir . '/.composer';
|
||||
|
||||
return $home;
|
||||
}
|
||||
|
||||
if (self::useXdg()) {
|
||||
// XDG Base Directory Specifications
|
||||
$xdgConfig = getenv('XDG_CONFIG_HOME');
|
||||
if (!$xdgConfig) {
|
||||
$xdgConfig = $userDir . '/.config';
|
||||
}
|
||||
$home = $xdgConfig . '/composer';
|
||||
} else {
|
||||
|
||||
return $home;
|
||||
}
|
||||
|
||||
$home = $userDir . '/.composer';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $home;
|
||||
}
|
||||
|
@ -89,7 +116,10 @@ class Factory
|
|||
protected static function getCacheDir($home)
|
||||
{
|
||||
$cacheDir = getenv('COMPOSER_CACHE_DIR');
|
||||
if (!$cacheDir) {
|
||||
if ($cacheDir) {
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
||||
if ($cacheDir = getenv('LOCALAPPDATA')) {
|
||||
$cacheDir .= '/Composer';
|
||||
|
@ -97,20 +127,29 @@ class Factory
|
|||
$cacheDir = $home . '/cache';
|
||||
}
|
||||
$cacheDir = strtr($cacheDir, '\\', '/');
|
||||
} else {
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
$userDir = self::getUserDir();
|
||||
|
||||
if (getenv('XDG_CONFIG_DIRS')) {
|
||||
if ($home === $userDir . '/.composer' && is_dir($home . '/cache')) {
|
||||
$cacheDir = $home . '/cache';
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
if (self::useXdg()) {
|
||||
$xdgCache = getenv('XDG_CACHE_HOME');
|
||||
if (!$xdgCache) {
|
||||
$xdgCache = $userDir . '/.cache';
|
||||
}
|
||||
$cacheDir = $xdgCache . '/composer';
|
||||
} else {
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
$cacheDir = $home . '/cache';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
@ -124,17 +163,31 @@ class Factory
|
|||
{
|
||||
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
||||
$dataDir = strtr($home, '\\', '/');
|
||||
} elseif (getenv('XDG_CONFIG_DIRS')) {
|
||||
|
||||
return $dataDir;
|
||||
}
|
||||
|
||||
$userDir = self::getUserDir();
|
||||
|
||||
if ($home === $userDir . '/.composer') {
|
||||
$cacheDir = $home;
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
if (self::useXdg()) {
|
||||
$xdgData = getenv('XDG_DATA_HOME');
|
||||
if (!$xdgData) {
|
||||
$userDir = self::getUserDir();
|
||||
$xdgData = $userDir . '/.local/share';
|
||||
}
|
||||
$dataDir = $xdgData . '/composer';
|
||||
} else {
|
||||
$dataDir = $home;
|
||||
|
||||
return $dataDir;
|
||||
}
|
||||
|
||||
$dataDir = $home;
|
||||
|
||||
return $dataDir;
|
||||
}
|
||||
|
||||
|
@ -163,31 +216,6 @@ class Factory
|
|||
}
|
||||
}
|
||||
|
||||
// Move content of old composer dir to XDG
|
||||
if (getenv('XDG_CONFIG_DIRS') !== false && getenv('COMPOSER_HOME') === false && getenv('COMPOSER_CACHE_DIR') === false) {
|
||||
$userDir = rtrim(getenv('HOME'), '/');
|
||||
$oldComposerHome = $userDir . '/.composer';
|
||||
if (file_exists($oldComposerHome)) {
|
||||
// migrate to XDG
|
||||
foreach (glob($oldComposerHome . '/*.json') as $file) {
|
||||
rename($file, $home . '/' . basename($file));
|
||||
}
|
||||
|
||||
foreach (glob($oldComposerHome . '/*.phar') as $file) {
|
||||
rename($file, $dataDir . '/' . basename($file));
|
||||
}
|
||||
|
||||
foreach (glob($oldComposerHome . '/cache/*') as $oldCacheDir) {
|
||||
rename($oldCacheDir, $cacheDir . '/' . basename($oldCacheDir));
|
||||
}
|
||||
|
||||
unlink($oldComposerHome . '/.htaccess');
|
||||
unlink($oldComposerHome . '/cache/.htaccess');
|
||||
rmdir($oldComposerHome . '/cache');
|
||||
rmdir($oldComposerHome);
|
||||
}
|
||||
}
|
||||
|
||||
$config = new Config(true, $cwd);
|
||||
|
||||
// add dirs to the config
|
||||
|
|
Loading…
Reference in New Issue