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
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
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
Specifications, and `$home/cache` on other unix systems. Stores all the caches
used by Composer. See also [COMPOSER_HOME](03-cli.md#composer-home).

View File

@ -171,6 +171,10 @@
"type": "string",
"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": {
"type": "string",
"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; }),
'archive-dir' => 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-files-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
{
/**
*
* @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
* @throws \RuntimeException
@ -83,33 +54,23 @@ class Factory
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;
return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer';
}
$userDir = self::getUserDir();
if (is_dir($userDir . '/.composer')) {
$home = $userDir . '/.composer';
return $home;
return $userDir . '/.composer';
}
if (self::useXdg()) {
// XDG Base Directory Specifications
$xdgConfig = getenv('XDG_CONFIG_HOME');
if (!$xdgConfig) {
$xdgConfig = $userDir . '/.config';
}
$home = $xdgConfig . '/composer';
$xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config';
return $home;
return $xdgConfig . '/composer';
}
$home = $userDir . '/.composer';
return $home;
return $userDir . '/.composer';
}
/**
@ -129,69 +90,42 @@ class Factory
} else {
$cacheDir = $home . '/cache';
}
$cacheDir = strtr($cacheDir, '\\', '/');
return $cacheDir;
return rtrim(strtr($cacheDir, '\\', '/'), '/');
}
$userDir = self::getUserDir();
if ($home === $userDir . '/.composer' && is_dir($home . '/cache')) {
$cacheDir = $home . '/cache';
return $cacheDir;
return $home . '/cache';
}
if (self::useXdg()) {
$xdgCache = getenv('XDG_CACHE_HOME');
if (!$xdgCache) {
$xdgCache = $userDir . '/.cache';
}
$cacheDir = $xdgCache . '/composer';
$xdgCache = getenv('XDG_CACHE_HOME') ?: $userDir . '/.cache';
return $cacheDir;
return $xdgCache . '/composer';
}
$cacheDir = $home . '/cache';
return $cacheDir;
return $home . '/cache';
}
/**
* @param string $home
*
* @return string
*/
protected static function getDataDir($home)
{
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$dataDir = strtr($home, '\\', '/');
return $dataDir;
return strtr($home, '\\', '/');
}
$userDir = self::getUserDir();
if ($home !== $userDir . '/.composer' && self::useXdg()) {
$xdgData = getenv('XDG_DATA_HOME') ?: $userDir . '/.local/share';
if ($home === $userDir . '/.composer') {
$cacheDir = $home;
return $cacheDir;
return $xdgData . '/composer';
}
if (self::useXdg()) {
$xdgData = getenv('XDG_DATA_HOME');
if (!$xdgData) {
$userDir = self::getUserDir();
$xdgData = $userDir . '/.local/share';
}
$dataDir = $xdgData . '/composer';
return $dataDir;
}
$dataDir = $home;
return $dataDir;
return $home;
}
/**
@ -665,4 +599,32 @@ class Factory
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, '\\', '/'), '/');
}
}