forked from composer/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConfigSourceInterface and matching class
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\JsonManipulator; | ||
|
||
/** | ||
* @author Jordi Boggiano <j.boggiano@seld.be> | ||
*/ | ||
interface ConfigSourceInterface | ||
{ | ||
public function addRepository($name, $config); | ||
|
||
public function removeRepository($name); | ||
|
||
public function addConfigSetting($name, $value); | ||
|
||
public function removeConfigSetting($name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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\JsonManipulator; | ||
use Composer\Json\JsonFile; | ||
|
||
/** | ||
* @author Jordi Boggiano <j.boggiano@seld.be> | ||
*/ | ||
class JsonConfigSource implements ConfigSourceInterface | ||
{ | ||
private $file; | ||
private $manipulator; | ||
|
||
public function __construct(JsonFile $file) | ||
{ | ||
$this->file = $file; | ||
} | ||
|
||
public function addRepository($name, $config) | ||
{ | ||
return $this->manipulateJson('addRepository', $name, $config, function (&$config, $repo, $repoConfig) { | ||
$config['repositories'][$repo] = $repoConfig; | ||
}); | ||
} | ||
|
||
public function removeRepository($name) | ||
{ | ||
return $this->manipulateJson('removeRepository', $name, function (&$config, $repo) { | ||
unset($config['repositories'][$repo]); | ||
}); | ||
} | ||
|
||
public function addConfigSetting($name, $value) | ||
{ | ||
$this->manipulateJson('addConfigSetting', $name, $value, function (&$config, $key, $val) { | ||
$config['config'][$key] = $val; | ||
}); | ||
} | ||
|
||
public function removeConfigSetting($name) | ||
{ | ||
return $this->manipulateJson('removeConfigSetting', $name, function (&$config, $key) { | ||
unset($config['config'][$key]); | ||
}); | ||
} | ||
|
||
protected function manipulateJson($method, $args, $fallback) | ||
{ | ||
$args = func_get_args(); | ||
// remove method & fallback | ||
array_shift($args); | ||
$fallback = array_pop($args); | ||
|
||
$contents = file_get_contents($this->file->getPath()); | ||
$manipulator = new JsonManipulator($contents); | ||
|
||
// 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(); | ||
array_unshift($args, $config); | ||
call_user_func_array($fallback, $args); | ||
$this->file->write($config); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters