1
0
Fork 0

Cleanups of XDG support, refs #1407

pull/4790/head
Jordi Boggiano 2016-01-18 18:26:05 +00:00
parent 3e9efcfb85
commit 96ff17c520
4 changed files with 57 additions and 82 deletions

View File

@ -87,9 +87,17 @@ vendor-dir and all `*-dir` options below.
Defaults to `vendor/bin`. If a project includes binaries, they will be symlinked Defaults to `vendor/bin`. If a project includes binaries, they will be symlinked
into this directory. into this directory.
## data-dir
Defaults to `C:\Users\<user>\AppData\Roaming\Composer` on Windows,
`$XDG_DATA_HOME/composer` on unix systems that follow the XDG Base Directory
Specifications, and `$home` on other unix systems. Right now it is only
used for storing past composer.phar files to be able to rollback to older
versions. See also [COMPOSER_HOME](03-cli.md#composer-home).
## cache-dir ## cache-dir
Defaults `C:\Users\<user>\AppData\Local\Composer` on Windows, Defaults to `C:\Users\<user>\AppData\Local\Composer` on Windows,
`$XDG_CACHE_HOME/composer` on unix systems that follow the XDG Base Directory `$XDG_CACHE_HOME/composer` on unix systems that follow the XDG Base Directory
Specifications, and `$home/cache` on other unix systems. Stores all the caches Specifications, and `$home/cache` on other unix systems. Stores all the caches
used by Composer. See also [COMPOSER_HOME](03-cli.md#composer-home). used by Composer. See also [COMPOSER_HOME](03-cli.md#composer-home).

View File

@ -171,6 +171,10 @@
"type": "string", "type": "string",
"description": "The location where all binaries are linked, defaults to \"vendor/bin\"." "description": "The location where all binaries are linked, defaults to \"vendor/bin\"."
}, },
"data-dir": {
"type": "string",
"description": "The location where old phar files are stored, defaults to \"$home\" except on XDG Base Directory compliant unixes."
},
"cache-dir": { "cache-dir": {
"type": "string", "type": "string",
"description": "The location where all caches are located, defaults to \"~/.composer/cache\" on *nix and \"%LOCALAPPDATA%\\Composer\" on windows." "description": "The location where all caches are located, defaults to \"~/.composer/cache\" on *nix and \"%LOCALAPPDATA%\\Composer\" on windows."

View File

@ -298,6 +298,7 @@ EOT
'bin-dir' => array('is_string', function ($val) { return $val; }), 'bin-dir' => array('is_string', function ($val) { return $val; }),
'archive-dir' => array('is_string', function ($val) { return $val; }), 'archive-dir' => array('is_string', function ($val) { return $val; }),
'archive-format' => array('is_string', function ($val) { return $val; }), 'archive-format' => array('is_string', function ($val) { return $val; }),
'data-dir' => array('is_string', function ($val) { return $val; }),
'cache-dir' => array('is_string', function ($val) { return $val; }), 'cache-dir' => array('is_string', function ($val) { return $val; }),
'cache-files-dir' => array('is_string', function ($val) { return $val; }), 'cache-files-dir' => array('is_string', function ($val) { return $val; }),
'cache-repo-dir' => array('is_string', function ($val) { return $val; }), 'cache-repo-dir' => array('is_string', function ($val) { return $val; }),

View File

@ -39,35 +39,6 @@ use Seld\JsonLint\JsonParser;
*/ */
class Factory 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
*/
private static function getUserDir()
{
if (!getenv('HOME')) {
throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly');
}
$userDir = rtrim(getenv('HOME'), '/');
return $userDir;
}
/** /**
* @return string * @return string
* @throws \RuntimeException * @throws \RuntimeException
@ -83,33 +54,23 @@ class Factory
if (!getenv('APPDATA')) { if (!getenv('APPDATA')) {
throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly'); 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; return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer';
} }
$userDir = self::getUserDir(); $userDir = self::getUserDir();
if (is_dir($userDir . '/.composer')) { if (is_dir($userDir . '/.composer')) {
$home = $userDir . '/.composer'; return $userDir . '/.composer';
return $home;
} }
if (self::useXdg()) { if (self::useXdg()) {
// XDG Base Directory Specifications // XDG Base Directory Specifications
$xdgConfig = getenv('XDG_CONFIG_HOME'); $xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config';
if (!$xdgConfig) {
$xdgConfig = $userDir . '/.config';
}
$home = $xdgConfig . '/composer';
return $home; return $xdgConfig . '/composer';
} }
$home = $userDir . '/.composer'; return $userDir . '/.composer';
return $home;
} }
/** /**
@ -129,69 +90,42 @@ class Factory
} else { } else {
$cacheDir = $home . '/cache'; $cacheDir = $home . '/cache';
} }
$cacheDir = strtr($cacheDir, '\\', '/');
return $cacheDir; return rtrim(strtr($cacheDir, '\\', '/'), '/');
} }
$userDir = self::getUserDir(); $userDir = self::getUserDir();
if ($home === $userDir . '/.composer' && is_dir($home . '/cache')) { if ($home === $userDir . '/.composer' && is_dir($home . '/cache')) {
$cacheDir = $home . '/cache'; return $home . '/cache';
return $cacheDir;
} }
if (self::useXdg()) { if (self::useXdg()) {
$xdgCache = getenv('XDG_CACHE_HOME'); $xdgCache = getenv('XDG_CACHE_HOME') ?: $userDir . '/.cache';
if (!$xdgCache) {
$xdgCache = $userDir . '/.cache';
}
$cacheDir = $xdgCache . '/composer';
return $cacheDir; return $xdgCache . '/composer';
} }
$cacheDir = $home . '/cache'; return $home . '/cache';
return $cacheDir;
} }
/** /**
* @param string $home * @param string $home
*
* @return string * @return string
*/ */
protected static function getDataDir($home) protected static function getDataDir($home)
{ {
if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$dataDir = strtr($home, '\\', '/'); return strtr($home, '\\', '/');
return $dataDir;
} }
$userDir = self::getUserDir(); $userDir = self::getUserDir();
if ($home !== $userDir . '/.composer' && self::useXdg()) {
$xdgData = getenv('XDG_DATA_HOME') ?: $userDir . '/.local/share';
if ($home === $userDir . '/.composer') { return $xdgData . '/composer';
$cacheDir = $home;
return $cacheDir;
} }
if (self::useXdg()) { return $home;
$xdgData = getenv('XDG_DATA_HOME');
if (!$xdgData) {
$userDir = self::getUserDir();
$xdgData = $userDir . '/.local/share';
}
$dataDir = $xdgData . '/composer';
return $dataDir;
}
$dataDir = $home;
return $dataDir;
} }
/** /**
@ -665,4 +599,32 @@ class Factory
return $remoteFilesystem; return $remoteFilesystem;
} }
/**
* @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
*/
private static function getUserDir()
{
$home = getenv('HOME');
if (!$home) {
throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly');
}
return rtrim(strtr($home, '\\', '/'), '/');
}
} }