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

Breadcrumb

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

function JsonFile::write

Writes json file.

Parameters

mixed[] $hash writes hash into json file:

int $options json_encode options:

Return value

void

Throws

\UnexpectedValueException|\Exception

File

vendor/composer/composer/src/Composer/Json/JsonFile.php, line 139

Class

JsonFile
Reads/writes json files.

Namespace

Composer\Json

Code

public function write(array $hash, int $options = JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) {
    if ($this->path === 'php://memory') {
        file_put_contents($this->path, static::encode($hash, $options, $this->indent));
        return;
    }
    $dir = dirname($this->path);
    if (!is_dir($dir)) {
        if (file_exists($dir)) {
            throw new \UnexpectedValueException(realpath($dir) . ' exists and is not a directory.');
        }
        if (!@mkdir($dir, 0777, true)) {
            throw new \UnexpectedValueException($dir . ' does not exist and could not be created.');
        }
    }
    $retries = 3;
    while ($retries--) {
        try {
            $this->filePutContentsIfModified($this->path, static::encode($hash, $options, $this->indent) . ($options & JSON_PRETTY_PRINT ? "\n" : ''));
            break;
        } catch (\Exception $e) {
            if ($retries > 0) {
                usleep(500000);
                continue;
            }
            throw $e;
        }
    }
}
RSS feed
Powered by Drupal