Jelajahi Sumber

Allow $HOME and ~ usage in all *-dir config values, fixes #3060

Jordi Boggiano 10 tahun lalu
induk
melakukan
449f8165ef
3 mengubah file dengan 18 tambahan dan 2 penghapusan
  1. 2 1
      doc/04-schema.md
  2. 4 1
      src/Composer/Config.php
  3. 12 0
      tests/Composer/Test/ConfigTest.php

+ 2 - 1
doc/04-schema.md

@@ -760,7 +760,8 @@ The following options are supported:
   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.
+  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

+ 4 - 1
src/Composer/Config.php

@@ -158,7 +158,10 @@ class Config
                 // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
                 $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
 
-                return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
+                $val = rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
+                $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
+
+                return $val;
 
             case 'cache-ttl':
                 return (int) $this->config[$key];

+ 12 - 0
tests/Composer/Test/ConfigTest.php

@@ -109,6 +109,18 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $config->get('github-oauth'));
     }
 
+    public function testVarReplacement()
+    {
+        $config = new Config();
+        $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}')));
+        $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/')));
+
+        $home = rtrim(getenv('HOME'), '\\/');
+        $this->assertEquals('b', $config->get('c'));
+        $this->assertEquals($home.'/', $config->get('bin-dir'));
+        $this->assertEquals($home.'/foo', $config->get('cache-dir'));
+    }
+
     public function testOverrideGithubProtocols()
     {
         $config = new Config();