1
0
Fork 0

Change detection of config/cache/data dirs

pull/1407/head
Nicolas Toniazzi 2015-07-22 10:38:12 +02:00
parent 7d63eb5718
commit e8a6cb536b
1 changed files with 97 additions and 69 deletions

View File

@ -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,28 +71,40 @@ class Factory
protected static function getHomeDir()
{
$home = getenv('COMPOSER_HOME');
if (!$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 {
$userDir = self::getUserDir();
if (getenv('XDG_CONFIG_DIRS')) {
// XDG Base Directory Specifications
$xdgConfig = getenv('XDG_CONFIG_HOME');
if (!$xdgConfig) {
$xdgConfig = $userDir . '/.config';
}
$home = $xdgConfig . '/composer';
} else {
$home = $userDir . '/.composer';
}
}
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';
return $home;
}
$userDir = self::getUserDir();
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';
return $home;
}
$home = $userDir . '/.composer';
return $home;
}
@ -89,29 +116,41 @@ class Factory
protected static function getCacheDir($home)
{
$cacheDir = getenv('COMPOSER_CACHE_DIR');
if (!$cacheDir) {
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
if ($cacheDir = getenv('LOCALAPPDATA')) {
$cacheDir .= '/Composer';
} else {
$cacheDir = $home . '/cache';
}
$cacheDir = strtr($cacheDir, '\\', '/');
} else {
$userDir = self::getUserDir();
if (getenv('XDG_CONFIG_DIRS')) {
$xdgCache = getenv('XDG_CACHE_HOME');
if (!$xdgCache) {
$xdgCache = $userDir . '/.cache';
}
$cacheDir = $xdgCache . '/composer';
} else {
$cacheDir = $home . '/cache';
}
}
if ($cacheDir) {
return $cacheDir;
}
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
if ($cacheDir = getenv('LOCALAPPDATA')) {
$cacheDir .= '/Composer';
} else {
$cacheDir = $home . '/cache';
}
$cacheDir = strtr($cacheDir, '\\', '/');
return $cacheDir;
}
$userDir = self::getUserDir();
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';
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