Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. JsonManipulator.php

function JsonManipulator::addSubNode

Parameters

mixed $value:

4 calls to JsonManipulator::addSubNode()
JsonManipulator::addConfigSetting in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::addProperty in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::addRepository in vendor/composer/composer/src/Composer/Json/JsonManipulator.php
JsonManipulator::removeSubNode in vendor/composer/composer/src/Composer/Json/JsonManipulator.php

File

vendor/composer/composer/src/Composer/Json/JsonManipulator.php, line 231

Class

JsonManipulator
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Json

Code

public function addSubNode(string $mainNode, string $name, $value, bool $append = true) : bool {
    $decoded = JsonFile::parseJson($this->contents);
    $subName = null;
    if (in_array($mainNode, [
        'config',
        'extra',
        'scripts',
    ]) && false !== strpos($name, '.')) {
        [
            $name,
            $subName,
        ] = explode('.', $name, 2);
    }
    // no main node yet
    if (!isset($decoded[$mainNode])) {
        if ($subName !== null) {
            $this->addMainKey($mainNode, [
                $name => [
                    $subName => $value,
                ],
            ]);
        }
        else {
            $this->addMainKey($mainNode, [
                $name => $value,
            ]);
        }
        return true;
    }
    // main node content not match-able
    $nodeRegex = '{' . self::DEFINES . '^(?P<start> \\s* \\{ \\s* (?: (?&string) \\s* : (?&json) \\s* , \\s* )*?' . preg_quote(JsonFile::encode($mainNode)) . '\\s*:\\s*)(?P<content>(?&object))(?P<end>.*)}sx';
    try {
        if (!Preg::isMatch($nodeRegex, $this->contents, $match)) {
            return false;
        }
    } catch (\RuntimeException $e) {
        if ($e->getCode() === PREG_BACKTRACK_LIMIT_ERROR) {
            return false;
        }
        throw $e;
    }
    assert(is_string($match['start']));
    assert(is_string($match['content']));
    assert(is_string($match['end']));
    $children = $match['content'];
    // invalid match due to un-regexable content, abort
    if (!@json_decode($children)) {
        return false;
    }
    // child exists
    $childRegex = '{' . self::DEFINES . '(?P<start>"' . preg_quote($name) . '"\\s*:\\s*)(?P<content>(?&json))(?P<end>,?)}x';
    if (Preg::isMatch($childRegex, $children, $matches)) {
        $children = Preg::replaceCallback($childRegex, function ($matches) use ($subName, $value) : string {
            if ($subName !== null && is_string($matches['content'])) {
                $curVal = json_decode($matches['content'], true);
                if (!is_array($curVal)) {
                    $curVal = [];
                }
                $curVal[$subName] = $value;
                $value = $curVal;
            }
            return $matches['start'] . $this->format($value, 1) . $matches['end'];
        }, $children);
    }
    else {
        Preg::match('#^{ (?P<leadingspace>\\s*?) (?P<content>\\S+.*?)? (?P<trailingspace>\\s*) }$#sx', $children, $match);
        $whitespace = '';
        if (!empty($match['trailingspace'])) {
            $whitespace = $match['trailingspace'];
        }
        if (!empty($match['content'])) {
            if ($subName !== null) {
                $value = [
                    $subName => $value,
                ];
            }
            // child missing but non empty children
            if ($append) {
                $children = Preg::replace('#' . $whitespace . '}$#', addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name) . ': ' . $this->format($value, 1) . $whitespace . '}', '\\$'), $children);
            }
            else {
                $whitespace = '';
                if (!empty($match['leadingspace'])) {
                    $whitespace = $match['leadingspace'];
                }
                $children = Preg::replace('#^{' . $whitespace . '#', addcslashes('{' . $whitespace . JsonFile::encode($name) . ': ' . $this->format($value, 1) . ',' . $this->newline . $this->indent . $this->indent, '\\$'), $children);
            }
        }
        else {
            if ($subName !== null) {
                $value = [
                    $subName => $value,
                ];
            }
            // children present but empty
            $children = '{' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name) . ': ' . $this->format($value, 1) . $whitespace . '}';
        }
    }
    $this->contents = Preg::replaceCallback($nodeRegex, static function ($m) use ($children) : string {
        return $m['start'] . $children . $m['end'];
    }, $this->contents);
    return true;
}
RSS feed
Powered by Drupal