2012-04-09 14:10:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer;
|
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
use Composer\Config\ConfigSourceInterface;
|
|
|
|
|
2012-04-09 14:10:25 +00:00
|
|
|
/**
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*/
|
|
|
|
class Config
|
|
|
|
{
|
2012-06-23 10:55:05 +00:00
|
|
|
public static $defaultConfig = array(
|
|
|
|
'process-timeout' => 300,
|
2012-11-10 22:17:36 +00:00
|
|
|
'cache-ttl' => 15552000, // 6 months
|
2012-06-23 10:55:05 +00:00
|
|
|
'vendor-dir' => 'vendor',
|
|
|
|
'bin-dir' => '{$vendor-dir}/bin',
|
|
|
|
'notify-on-install' => true,
|
2012-09-07 13:02:39 +00:00
|
|
|
'github-protocols' => array('git', 'https', 'http'),
|
2012-06-23 10:55:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
public static $defaultRepositories = array(
|
2012-06-24 11:03:55 +00:00
|
|
|
'packagist' => array(
|
|
|
|
'type' => 'composer',
|
2012-10-11 18:54:59 +00:00
|
|
|
'url' => 'https?://packagist.org',
|
2012-06-24 11:03:55 +00:00
|
|
|
)
|
2012-06-23 10:55:05 +00:00
|
|
|
);
|
|
|
|
|
2012-04-09 14:10:25 +00:00
|
|
|
private $config;
|
2012-06-23 10:55:05 +00:00
|
|
|
private $repositories;
|
2012-10-18 14:39:47 +00:00
|
|
|
private $configSource;
|
2012-04-09 14:10:25 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// load defaults
|
2012-06-23 10:55:05 +00:00
|
|
|
$this->config = static::$defaultConfig;
|
|
|
|
$this->repositories = static::$defaultRepositories;
|
2012-04-09 14:10:25 +00:00
|
|
|
}
|
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
public function setConfigSource(ConfigSourceInterface $source)
|
|
|
|
{
|
|
|
|
$this->configSource = $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfigSource()
|
|
|
|
{
|
|
|
|
return $this->configSource;
|
|
|
|
}
|
|
|
|
|
2012-04-09 14:13:46 +00:00
|
|
|
/**
|
|
|
|
* Merges new config values with the existing ones (overriding)
|
|
|
|
*
|
|
|
|
* @param array $config
|
|
|
|
*/
|
2012-04-09 14:10:25 +00:00
|
|
|
public function merge(array $config)
|
|
|
|
{
|
|
|
|
// override defaults with given config
|
|
|
|
if (!empty($config['config']) && is_array($config['config'])) {
|
2012-04-09 19:40:35 +00:00
|
|
|
$this->config = array_replace_recursive($this->config, $config['config']);
|
2012-04-09 14:10:25 +00:00
|
|
|
}
|
2012-06-23 10:55:05 +00:00
|
|
|
|
|
|
|
if (!empty($config['repositories']) && is_array($config['repositories'])) {
|
2012-06-23 11:04:23 +00:00
|
|
|
$this->repositories = array_reverse($this->repositories, true);
|
2012-06-24 11:03:55 +00:00
|
|
|
$newRepos = array_reverse($config['repositories'], true);
|
|
|
|
foreach ($newRepos as $name => $repository) {
|
|
|
|
// disable a repository by name
|
|
|
|
if (false === $repository) {
|
2012-06-23 11:04:23 +00:00
|
|
|
unset($this->repositories[$name]);
|
2012-06-24 11:03:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable a repository with an anonymous {"name": false} repo
|
2012-06-29 09:45:06 +00:00
|
|
|
if (1 === count($repository) && false === current($repository)) {
|
|
|
|
unset($this->repositories[key($repository)]);
|
|
|
|
continue;
|
2012-06-24 11:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// store repo
|
|
|
|
if (is_int($name)) {
|
|
|
|
$this->repositories[] = $repository;
|
|
|
|
} else {
|
|
|
|
$this->repositories[$name] = $repository;
|
2012-06-23 11:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-24 11:03:55 +00:00
|
|
|
$this->repositories = array_reverse($this->repositories, true);
|
2012-06-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRepositories()
|
|
|
|
{
|
|
|
|
return $this->repositories;
|
2012-04-09 14:10:25 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 14:13:46 +00:00
|
|
|
/**
|
|
|
|
* Returns a setting
|
|
|
|
*
|
2012-05-22 10:07:08 +00:00
|
|
|
* @param string $key
|
2012-04-09 14:13:46 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-04-09 14:10:25 +00:00
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'vendor-dir':
|
|
|
|
case 'bin-dir':
|
|
|
|
case 'process-timeout':
|
|
|
|
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
|
|
|
|
$env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
|
2012-05-22 10:07:08 +00:00
|
|
|
|
2012-06-24 19:58:51 +00:00
|
|
|
return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\');
|
2012-04-09 14:10:25 +00:00
|
|
|
|
2012-11-11 14:31:50 +00:00
|
|
|
case 'cache-ttl':
|
|
|
|
return (int) $this->config[$key];
|
|
|
|
|
|
|
|
case 'cache-files-ttl':
|
|
|
|
if (isset($this->config[$key])) {
|
|
|
|
return (int) $this->config[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $this->config['cache-ttl'];
|
|
|
|
|
2012-04-09 14:36:06 +00:00
|
|
|
case 'home':
|
|
|
|
return rtrim($this->process($this->config[$key]), '/\\');
|
|
|
|
|
2012-04-09 14:10:25 +00:00
|
|
|
default:
|
2012-10-18 14:39:47 +00:00
|
|
|
if (!isset($this->config[$key])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-04-09 14:10:25 +00:00
|
|
|
return $this->process($this->config[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-09 14:13:46 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a setting exists
|
|
|
|
*
|
2012-06-23 09:58:18 +00:00
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
2012-04-09 14:13:46 +00:00
|
|
|
*/
|
2012-04-09 14:10:25 +00:00
|
|
|
public function has($key)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces {$refs} inside a config string
|
|
|
|
*
|
|
|
|
* @param string a config string that can contain {$refs-to-other-config}
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function process($value)
|
|
|
|
{
|
|
|
|
$config = $this;
|
2012-05-22 10:07:08 +00:00
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
if (!is_string($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2012-04-09 14:10:25 +00:00
|
|
|
return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) {
|
|
|
|
return $config->get($match[1]);
|
|
|
|
}, $value);
|
|
|
|
}
|
|
|
|
}
|