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

Breadcrumb

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

function JsonConfigSource::manipulateJson

Parameters

mixed ...$args:

8 calls to JsonConfigSource::manipulateJson()
JsonConfigSource::addConfigSetting in vendor/composer/composer/src/Composer/Config/JsonConfigSource.php
@inheritDoc
JsonConfigSource::addLink in vendor/composer/composer/src/Composer/Config/JsonConfigSource.php
@inheritDoc
JsonConfigSource::addProperty in vendor/composer/composer/src/Composer/Config/JsonConfigSource.php
@inheritDoc
JsonConfigSource::addRepository in vendor/composer/composer/src/Composer/Config/JsonConfigSource.php
@inheritDoc
JsonConfigSource::removeConfigSetting in vendor/composer/composer/src/Composer/Config/JsonConfigSource.php
@inheritDoc

... See full list

File

vendor/composer/composer/src/Composer/Config/JsonConfigSource.php, line 210

Class

JsonConfigSource
JSON Configuration Source

Namespace

Composer\Config

Code

private function manipulateJson(string $method, callable $fallback, ...$args) : void {
    if ($this->file
        ->exists()) {
        if (!is_writable($this->file
            ->getPath())) {
            throw new \RuntimeException(sprintf('The file "%s" is not writable.', $this->file
                ->getPath()));
        }
        if (!Filesystem::isReadable($this->file
            ->getPath())) {
            throw new \RuntimeException(sprintf('The file "%s" is not readable.', $this->file
                ->getPath()));
        }
        $contents = file_get_contents($this->file
            ->getPath());
    }
    elseif ($this->authConfig) {
        $contents = "{\n}\n";
    }
    else {
        $contents = "{\n    \"config\": {\n    }\n}\n";
    }
    $manipulator = new JsonManipulator($contents);
    $newFile = !$this->file
        ->exists();
    // override manipulator method for auth config files
    if ($this->authConfig && $method === 'addConfigSetting') {
        $method = 'addSubNode';
        [
            $mainNode,
            $name,
        ] = explode('.', $args[0], 2);
        $args = [
            $mainNode,
            $name,
            $args[1],
        ];
    }
    elseif ($this->authConfig && $method === 'removeConfigSetting') {
        $method = 'removeSubNode';
        [
            $mainNode,
            $name,
        ] = explode('.', $args[0], 2);
        $args = [
            $mainNode,
            $name,
        ];
    }
    // try to update cleanly
    if (call_user_func_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();
        $this->arrayUnshiftRef($args, $config);
        $fallback(...$args);
        // avoid ending up with arrays for keys that should be objects
        foreach ([
            'require',
            'require-dev',
            'conflict',
            'provide',
            'replace',
            'suggest',
            'config',
            'autoload',
            'autoload-dev',
            'scripts',
            'scripts-descriptions',
            'scripts-aliases',
            'support',
        ] as $prop) {
            if (isset($config[$prop]) && $config[$prop] === []) {
                $config[$prop] = new \stdClass();
            }
        }
        foreach ([
            'psr-0',
            'psr-4',
        ] as $prop) {
            if (isset($config['autoload'][$prop]) && $config['autoload'][$prop] === []) {
                $config['autoload'][$prop] = new \stdClass();
            }
            if (isset($config['autoload-dev'][$prop]) && $config['autoload-dev'][$prop] === []) {
                $config['autoload-dev'][$prop] = new \stdClass();
            }
        }
        foreach ([
            'platform',
            'http-basic',
            'bearer',
            'gitlab-token',
            'gitlab-oauth',
            'github-oauth',
            'preferred-install',
        ] as $prop) {
            if (isset($config['config'][$prop]) && $config['config'][$prop] === []) {
                $config['config'][$prop] = new \stdClass();
            }
        }
        $this->file
            ->write($config);
    }
    try {
        $this->file
            ->validateSchema(JsonFile::LAX_SCHEMA);
    } catch (JsonValidationException $e) {
        // restore contents to the original state
        file_put_contents($this->file
            ->getPath(), $contents);
        throw new \RuntimeException('Failed to update composer.json with a valid format, reverting to the original content. Please report an issue to us with details (command you run and a copy of your composer.json). ' . PHP_EOL . implode(PHP_EOL, $e->getErrors()), 0, $e);
    }
    if ($newFile) {
        Silencer::call('chmod', $this->file
            ->getPath(), 0600);
    }
}
RSS feed
Powered by Drupal