Example #1
0
 public function testExpandPath()
 {
     putenv('TESTENV=/home/test');
     $this->assertEquals('/home/test/myPath', Platform::expandPath('%TESTENV%/myPath'));
     $this->assertEquals('/home/test/myPath', Platform::expandPath('$TESTENV/myPath'));
     $this->assertEquals((getenv('HOME') ?: getenv('USERPROFILE')) . '/test', Platform::expandPath('~/test'));
 }
Example #2
0
 /**
  * Returns a setting
  *
  * @param  string            $key
  * @param  int               $flags Options (see class constants)
  * @throws \RuntimeException
  * @return mixed
  */
 public function get($key, $flags = 0)
 {
     switch ($key) {
         case 'vendor-dir':
         case 'bin-dir':
         case 'process-timeout':
         case 'data-dir':
         case 'cache-dir':
         case 'cache-files-dir':
         case 'cache-repo-dir':
         case 'cache-vcs-dir':
         case 'cafile':
         case 'capath':
             // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
             $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
             $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
             $val = Platform::expandPath($val);
             if (substr($key, -4) !== '-dir') {
                 return $val;
             }
             return ($flags & self::RELATIVE_PATHS) == self::RELATIVE_PATHS ? $val : $this->realpath($val);
         case 'cache-ttl':
             return (int) $this->config[$key];
         case 'cache-files-maxsize':
             if (!preg_match('/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i', $this->config[$key], $matches)) {
                 throw new \RuntimeException("Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}");
             }
             $size = $matches[1];
             if (isset($matches[2])) {
                 switch (strtolower($matches[2])) {
                     case 'g':
                         $size *= 1024;
                         // intentional fallthrough
                     // intentional fallthrough
                     case 'm':
                         $size *= 1024;
                         // intentional fallthrough
                     // intentional fallthrough
                     case 'k':
                         $size *= 1024;
                         break;
                 }
             }
             return $size;
         case 'cache-files-ttl':
             if (isset($this->config[$key])) {
                 return (int) $this->config[$key];
             }
             return (int) $this->config['cache-ttl'];
         case 'home':
             $val = preg_replace('#^(\\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $this->config[$key]);
             return rtrim($this->process($val, $flags), '/\\');
         case 'bin-compat':
             $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key];
             if (!in_array($value, array('auto', 'full'))) {
                 throw new \RuntimeException("Invalid value for 'bin-compat': {$value}. Expected auto, full");
             }
             return $value;
         case 'discard-changes':
             if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
                 if (!in_array($env, array('stash', 'true', 'false', '1', '0'), true)) {
                     throw new \RuntimeException("Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash");
                 }
                 if ('stash' === $env) {
                     return 'stash';
                 }
                 // convert string value to bool
                 return $env !== 'false' && (bool) $env;
             }
             if (!in_array($this->config[$key], array(true, false, 'stash'), true)) {
                 throw new \RuntimeException("Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash");
             }
             return $this->config[$key];
         case 'github-protocols':
             $protos = $this->config['github-protocols'];
             if ($this->config['secure-http'] && false !== ($index = array_search('git', $protos))) {
                 unset($protos[$index]);
             }
             if (reset($protos) === 'http') {
                 throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"');
             }
             return $protos;
         case 'disable-tls':
             return $this->config[$key] !== 'false' && (bool) $this->config[$key];
         case 'secure-http':
             return $this->config[$key] !== 'false' && (bool) $this->config[$key];
         default:
             if (!isset($this->config[$key])) {
                 return null;
             }
             return $this->process($this->config[$key], $flags);
     }
 }
Example #3
0
 /**
  * Initializes path repository.
  *
  * @param array       $repoConfig
  * @param IOInterface $io
  * @param Config      $config
  */
 public function __construct(array $repoConfig, IOInterface $io, Config $config)
 {
     if (!isset($repoConfig['url'])) {
         throw new \RuntimeException('You must specify the `url` configuration for the path repository');
     }
     $this->loader = new ArrayLoader(null, true);
     $this->url = Platform::expandPath($repoConfig['url']);
     $this->process = new ProcessExecutor($io);
     $this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
     $this->repoConfig = $repoConfig;
     $this->options = isset($repoConfig['options']) ? $repoConfig['options'] : array();
     parent::__construct();
 }