2012-10-18 14:39:47 +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\Config;
|
|
|
|
|
|
|
|
use Composer\Json\JsonFile;
|
2012-11-14 17:03:11 +00:00
|
|
|
use Composer\Json\JsonManipulator;
|
2016-01-25 22:37:54 +00:00
|
|
|
use Composer\Util\Silencer;
|
2012-10-18 14:39:47 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-14 17:03:11 +00:00
|
|
|
* JSON Configuration Source
|
|
|
|
*
|
2012-10-18 14:39:47 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2012-11-14 17:03:11 +00:00
|
|
|
* @author Beau Simensen <beau@dflydev.com>
|
2012-10-18 14:39:47 +00:00
|
|
|
*/
|
|
|
|
class JsonConfigSource implements ConfigSourceInterface
|
|
|
|
{
|
2014-02-18 09:38:13 +00:00
|
|
|
/**
|
|
|
|
* @var \Composer\Json\JsonFile
|
|
|
|
*/
|
2012-10-18 14:39:47 +00:00
|
|
|
private $file;
|
|
|
|
|
2014-05-27 11:50:47 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $authConfig;
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param JsonFile $file
|
2014-07-16 13:17:38 +00:00
|
|
|
* @param bool $authConfig
|
2012-11-14 17:03:11 +00:00
|
|
|
*/
|
2014-05-27 11:50:47 +00:00
|
|
|
public function __construct(JsonFile $file, $authConfig = false)
|
2012-10-18 14:39:47 +00:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
2014-05-27 11:50:47 +00:00
|
|
|
$this->authConfig = $authConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->file->getPath();
|
2012-10-18 14:39:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-10-18 14:39:47 +00:00
|
|
|
public function addRepository($name, $config)
|
|
|
|
{
|
2012-10-18 16:43:31 +00:00
|
|
|
$this->manipulateJson('addRepository', $name, $config, function (&$config, $repo, $repoConfig) {
|
2012-10-18 14:39:47 +00:00
|
|
|
$config['repositories'][$repo] = $repoConfig;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-10-18 14:39:47 +00:00
|
|
|
public function removeRepository($name)
|
|
|
|
{
|
2012-10-18 16:43:31 +00:00
|
|
|
$this->manipulateJson('removeRepository', $name, function (&$config, $repo) {
|
2012-10-18 14:39:47 +00:00
|
|
|
unset($config['repositories'][$repo]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-10-18 14:39:47 +00:00
|
|
|
public function addConfigSetting($name, $value)
|
|
|
|
{
|
|
|
|
$this->manipulateJson('addConfigSetting', $name, $value, function (&$config, $key, $val) {
|
2015-12-03 13:24:26 +00:00
|
|
|
if (preg_match('{^(github-oauth|gitlab-oauth|http-basic|platform)\.}', $key)) {
|
2014-05-27 11:50:47 +00:00
|
|
|
list($key, $host) = explode('.', $key, 2);
|
|
|
|
if ($this->authConfig) {
|
|
|
|
$config[$key][$host] = $val;
|
|
|
|
} else {
|
|
|
|
$config['config'][$key][$host] = $val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$config['config'][$key] = $val;
|
|
|
|
}
|
2012-10-18 14:39:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-10-18 14:39:47 +00:00
|
|
|
public function removeConfigSetting($name)
|
|
|
|
{
|
2012-10-18 16:43:31 +00:00
|
|
|
$this->manipulateJson('removeConfigSetting', $name, function (&$config, $key) {
|
2015-12-03 13:24:26 +00:00
|
|
|
if (preg_match('{^(github-oauth|gitlab-oauth|http-basic|platform)\.}', $key)) {
|
2014-05-27 11:50:47 +00:00
|
|
|
list($key, $host) = explode('.', $key, 2);
|
|
|
|
if ($this->authConfig) {
|
|
|
|
unset($config[$key][$host]);
|
|
|
|
} else {
|
|
|
|
unset($config['config'][$key][$host]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($config['config'][$key]);
|
|
|
|
}
|
2012-10-18 14:39:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-14 17:03:11 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function addLink($type, $name, $value)
|
|
|
|
{
|
2013-05-06 09:31:22 +00:00
|
|
|
$this->manipulateJson('addLink', $type, $name, $value, function (&$config, $type, $name, $value) {
|
2012-11-14 17:03:11 +00:00
|
|
|
$config[$type][$name] = $value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function removeLink($type, $name)
|
|
|
|
{
|
2013-05-06 09:31:22 +00:00
|
|
|
$this->manipulateJson('removeSubNode', $type, $name, function (&$config, $type, $name) {
|
2012-11-14 17:03:11 +00:00
|
|
|
unset($config[$type][$name]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
protected function manipulateJson($method, $args, $fallback)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
// remove method & fallback
|
|
|
|
array_shift($args);
|
|
|
|
$fallback = array_pop($args);
|
|
|
|
|
2012-10-18 16:18:40 +00:00
|
|
|
if ($this->file->exists()) {
|
|
|
|
$contents = file_get_contents($this->file->getPath());
|
2014-05-27 11:50:47 +00:00
|
|
|
} elseif ($this->authConfig) {
|
|
|
|
$contents = "{\n}\n";
|
2012-10-18 16:18:40 +00:00
|
|
|
} else {
|
|
|
|
$contents = "{\n \"config\": {\n }\n}\n";
|
|
|
|
}
|
2014-05-27 11:50:47 +00:00
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
$manipulator = new JsonManipulator($contents);
|
|
|
|
|
2012-10-21 16:01:53 +00:00
|
|
|
$newFile = !$this->file->exists();
|
|
|
|
|
2014-05-27 11:50:47 +00:00
|
|
|
// override manipulator method for auth config files
|
|
|
|
if ($this->authConfig && $method === 'addConfigSetting') {
|
|
|
|
$method = 'addSubNode';
|
|
|
|
list($mainNode, $name) = explode('.', $args[0], 2);
|
|
|
|
$args = array($mainNode, $name, $args[1]);
|
|
|
|
} elseif ($this->authConfig && $method === 'removeConfigSetting') {
|
|
|
|
$method = 'removeSubNode';
|
|
|
|
list($mainNode, $name) = explode('.', $args[0], 2);
|
|
|
|
$args = array($mainNode, $name);
|
|
|
|
}
|
|
|
|
|
2012-10-18 14:39:47 +00:00
|
|
|
// try to update cleanly
|
|
|
|
if (call_user_func_array(array($manipulator, $method), $args)) {
|
|
|
|
file_put_contents($this->file->getPath(), $manipulator->getContents());
|
|
|
|
} else {
|
|
|
|
// on failed clean update, call the fallback and rewrite the whole file
|
|
|
|
$config = $this->file->read();
|
2014-05-16 01:48:30 +00:00
|
|
|
$this->arrayUnshiftRef($args, $config);
|
2012-10-18 14:39:47 +00:00
|
|
|
call_user_func_array($fallback, $args);
|
|
|
|
$this->file->write($config);
|
|
|
|
}
|
2012-10-21 16:01:53 +00:00
|
|
|
|
|
|
|
if ($newFile) {
|
2016-01-25 22:37:54 +00:00
|
|
|
Silencer::call('chmod', $this->file->getPath(), 0600);
|
2012-10-21 16:01:53 +00:00
|
|
|
}
|
2012-10-18 14:39:47 +00:00
|
|
|
}
|
2014-02-18 09:38:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepend a reference to an element to the beginning of an array.
|
|
|
|
*
|
2014-06-10 14:02:44 +00:00
|
|
|
* @param array $array
|
|
|
|
* @param mixed $value
|
2014-02-18 09:38:13 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2014-05-16 01:48:30 +00:00
|
|
|
private function arrayUnshiftRef(&$array, &$value)
|
2014-02-18 09:38:13 +00:00
|
|
|
{
|
|
|
|
$return = array_unshift($array, '');
|
2014-10-17 17:46:01 +00:00
|
|
|
$array[0] = &$value;
|
2014-06-10 14:02:44 +00:00
|
|
|
|
2014-02-18 09:38:13 +00:00
|
|
|
return $return;
|
|
|
|
}
|
2012-10-18 14:39:47 +00:00
|
|
|
}
|