فهرست منبع

Cleanups of XDG support, refs #1407

Jordi Boggiano 9 سال پیش
والد
کامیت
96ff17c520
4فایلهای تغییر یافته به همراه57 افزوده شده و 82 حذف شده
  1. 9 1
      doc/06-config.md
  2. 4 0
      res/composer-schema.json
  3. 1 0
      src/Composer/Command/ConfigCommand.php
  4. 43 81
      src/Composer/Factory.php

+ 9 - 1
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\<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).

+ 4 - 0
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."

+ 1 - 0
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; }),

+ 43 - 81
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, '\\', '/'), '/');
+    }
 }