function JsonManipulator::addMainKey
Parameters
mixed $content:
3 calls to JsonManipulator::addMainKey()
- JsonManipulator::addLink in vendor/
composer/ composer/ src/ Composer/ Json/ JsonManipulator.php - JsonManipulator::addProperty in vendor/
composer/ composer/ src/ Composer/ Json/ JsonManipulator.php - JsonManipulator::addSubNode in vendor/
composer/ composer/ src/ Composer/ Json/ JsonManipulator.php
File
-
vendor/
composer/ composer/ src/ Composer/ Json/ JsonManipulator.php, line 456
Class
- JsonManipulator
- @author Jordi Boggiano <j.boggiano@seld.be>
Namespace
Composer\JsonCode
public function addMainKey(string $key, $content) : bool {
$decoded = JsonFile::parseJson($this->contents);
$content = $this->format($content);
// key exists already
$regex = '{' . self::DEFINES . '^(?P<start>\\s*\\{\\s*(?:(?&string)\\s*:\\s*(?&json)\\s*,\\s*)*?)' . '(?P<key>' . preg_quote(JsonFile::encode($key)) . '\\s*:\\s*(?&json))(?P<end>.*)}sx';
if (isset($decoded[$key]) && Preg::isMatch($regex, $this->contents, $matches)) {
// invalid match due to un-regexable content, abort
if (!@json_decode('{' . $matches['key'] . '}')) {
return false;
}
$this->contents = $matches['start'] . JsonFile::encode($key) . ': ' . $content . $matches['end'];
return true;
}
// append at the end of the file and keep whitespace
if (Preg::isMatch('#[^{\\s](\\s*)\\}$#', $this->contents, $match)) {
$this->contents = Preg::replace('#' . $match[1] . '\\}$#', addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key) . ': ' . $content . $this->newline . '}', '\\$'), $this->contents);
return true;
}
// append at the end of the file
$this->contents = Preg::replace('#\\}$#', addcslashes($this->indent . JsonFile::encode($key) . ': ' . $content . $this->newline . '}', '\\$'), $this->contents);
return true;
}