From 6821e3efcc49a813b76f59bce6a625b14d88ab13 Mon Sep 17 00:00:00 2001 From: ntoniazzi Date: Wed, 2 Jan 2013 13:48:00 +0100 Subject: [PATCH] Conforming to XDG Base Directory Specification (http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) --- doc/03-cli.md | 8 +++++--- doc/04-schema.md | 5 +++-- src/Composer/Factory.php | 34 +++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 870509c3a..7bec79bba 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -400,9 +400,11 @@ The `COMPOSER_HOME` var allows you to change the composer home directory. This is a hidden, global (per-user on the machine) directory that is shared between all projects. -By default it points to `/home//.composer` on *nix, -`/Users//.composer` on OSX and -`C:\Users\\AppData\Roaming\Composer` on Windows. +By default it points to `C:\Users\\AppData\Roaming\Composer` on Windows +and `/Users//.composer` on OSX. On *nix systems that follow the [XDG Base +Directory Specifications](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html), +it points to `$XDG_CONFIG_HOME/composer`. On other *nix systems, it points to +`/home//.composer`. #### COMPOSER_HOME/config.json diff --git a/doc/04-schema.md b/doc/04-schema.md index 71c4fd692..0f7096f12 100644 --- a/doc/04-schema.md +++ b/doc/04-schema.md @@ -598,8 +598,9 @@ The following options are supported: `{"github.com": "oauthtoken"}` as the value of this option will use `oauthtoken` to access private repositories on github and to circumvent the low IP-based rate limiting of their API. -* **cache-dir:** Defaults to `$home/cache` on unix systems and - `C:\Users\\AppData\Local\Composer` on Windows. Stores all the caches +* **cache-dir:** 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). * **cache-files-dir:** Defaults to `$cache-dir/files`. Stores the zip archives of packages. diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index c2565c0b4..2dbf52dfa 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -38,15 +38,21 @@ class Factory // determine home and cache dirs $home = getenv('COMPOSER_HOME'); $cacheDir = getenv('COMPOSER_CACHE_DIR'); + $userDir = rtrim(getenv('HOME'), '/'); + $followXDG = false; if (!$home) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { $home = getenv('APPDATA') . '/Composer'; - } else { + } elseif (getenv('XDG_CONFIG_DIRS')) { + // XDG Base Directory Specifications + $followXDG = true; $xdgConfig = getenv('XDG_CONFIG_HOME'); if (!$xdgConfig) { - $xdgConfig = rtrim(getenv('HOME'), '/') . '/.config'; + $xdgConfig = $userDir . '/.config'; } $home = $xdgConfig . '/composer'; + } else { + $home = $userDir . '/.composer'; } } if (!$cacheDir) { @@ -56,15 +62,20 @@ class Factory } else { $cacheDir = getenv('APPDATA') . '/Composer/cache'; } - } else { + } elseif (getenv('XDG_CONFIG_DIRS')) { + $followXDG = true; $xdgCache = getenv('XDG_CACHE_HOME'); if (!$xdgCache) { - $xdgCache = rtrim(getenv('HOME'), '/') . '/.cache'; + $xdgCache = $userDir . '/.cache'; } $cacheDir = $xdgCache . '/composer'; + + + } else { + $cacheDir = $home . '/.cache'; } } - + // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk @@ -77,6 +88,19 @@ class Factory } } + // Move content of old composer dir to XDG + if ($followXDG && file_exists($userDir . '/.composer')) { + // migrate to XDG + @rename($userDir . '/.composer/config.json', $home . '/config.json'); + @unlink($userDir . '/.composer/.htaccess'); + @unlink($userDir . '/.composer/cache/.htaccess'); + foreach (glob($userDir . '/.composer/cache/*') as $oldCacheDir) { + @rename($oldCacheDir, $cacheDir . '/' . basename($oldCacheDir)); + } + @rmdir($userDir . '/.composer/cache'); + @rmdir($userDir . '/.composer'); + } + $config = new Config(); // add dirs to the config