From 96ff17c520b398d2a102d082bf0d4df3bd5c18a8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 18 Jan 2016 18:26:05 +0000 Subject: [PATCH] Cleanups of XDG support, refs #1407 --- doc/06-config.md | 10 +- res/composer-schema.json | 4 + src/Composer/Command/ConfigCommand.php | 1 + src/Composer/Factory.php | 124 +++++++++---------------- 4 files changed, 57 insertions(+), 82 deletions(-) diff --git a/doc/06-config.md b/doc/06-config.md index fd1988136..59390ea42 100644 --- a/doc/06-config.md +++ b/doc/06-config.md @@ -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\\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\\AppData\Local\Composer` on Windows, +Defaults to `C:\Users\\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). diff --git a/res/composer-schema.json b/res/composer-schema.json index 6b89b08b7..966b19b86 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -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." diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 2ae6edadf..8378bc5b4 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -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; }), diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 940f84a12..f6537c101 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -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, '\\', '/'), '/'); + } }