Просмотр исходного кода

Conforming to XDG Base Directory Specification (http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html)

ntoniazzi 12 лет назад
Родитель
Сommit
b79f38fd99
3 измененных файлов с 39 добавлено и 38 удалено
  1. 5 3
      doc/03-cli.md
  2. 3 12
      doc/04-schema.md
  3. 31 23
      src/Composer/Factory.php

+ 5 - 3
doc/03-cli.md

@@ -598,9 +598,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/<user>/.composer` on \*nix,
-`/Users/<user>/.composer` on OSX and
-`C:\Users\<user>\AppData\Roaming\Composer` on Windows.
+By default it points to `C:\Users\<user>\AppData\Roaming\Composer` on Windows
+and `/Users/<user>/.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/<user>/.composer`.
 
 #### COMPOSER_HOME/config.json
 

+ 3 - 12
doc/04-schema.md

@@ -754,18 +754,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.
-  [Read more](articles/troubleshooting.md#api-rate-limit-and-oauth-tokens)
-  on how to get an OAuth token for GitHub.
-* **http-basic:** A list of domain names and username/passwords to authenticate
-  against them. For example using
-  `{"example.org": {"username": "alice", "password": "foo"}` as the value of this option will let composer authenticate against example.org.
-* **vendor-dir:** Defaults to `vendor`. You can install dependencies into a
-  different directory if you want to. `$HOME` and `~` will be replaced by your
-  home directory's path in vendor-dir and all `*-dir` options below.
-* **bin-dir:** Defaults to `vendor/bin`. If a project includes binaries, they
-  will be symlinked into this directory.
-* **cache-dir:** Defaults to `$home/cache` on unix systems and
-  `C:\Users\<user>\AppData\Local\Composer` on Windows. Stores all the caches
+* **cache-dir:** 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).
 * **cache-files-dir:** Defaults to `$cache-dir/files`. Stores the zip archives
   of packages.

+ 31 - 23
src/Composer/Factory.php

@@ -43,18 +43,22 @@ class Factory
     protected static function getHomeDir()
     {
         $home = getenv('COMPOSER_HOME');
+        $cacheDir = getenv('COMPOSER_CACHE_DIR');
+        $userDir = rtrim(getenv('HOME'), '/');
+        $followXDG = false;
         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 {
+                $home = getenv('APPDATA') . '/Composer';
+            } 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';
             }
         }
 
@@ -76,29 +80,20 @@ class Factory
                 } else {
                     $cacheDir = $home . '/cache';
                 }
-                $cacheDir = strtr($cacheDir, '\\', '/');
-            } 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';
-            }
-        }
-
-        return $cacheDir;
-    }
 
-    /**
-     * @param  IOInterface|null $io
-     * @return Config
-     */
-    public static function createConfig(IOInterface $io = null)
-    {
-        // determine home and cache dirs
-        $home     = self::getHomeDir();
-        $cacheDir = self::getCacheDir($home);
 
+            } 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
@@ -111,6 +106,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