You are here

public static function JsonFile::write in Composer Manager 8

Encodes and writes the provided json data to a file.

Parameters

string $filename: Name of the file to write.

array $data: The data to encode.

Return value

int The number of bytes that were written to the file.

Throws

\RuntimeException

\UnexpectedValueException

2 calls to JsonFile::write()
composer_manager_initialize in ./composer_manager.module
Initializes Composer Manager.
PackageManager::rebuildRootPackage in src/PackageManager.php
Rebuilds the root package by merging in the extension requirements.

File

src/JsonFile.php, line 51

Class

JsonFile
Reads and writes json files.

Namespace

Drupal\composer_manager

Code

public static function write($filename, array $data) {
  if (!is_writable($filename)) {
    throw new \RuntimeException(sprintf('%s is not writable.', $filename));
  }

  // Strip empty config elements.
  foreach ($data as $key => $item) {
    if (is_array($item) && empty($item)) {
      unset($data[$key]);
    }
  }
  $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  if (JSON_ERROR_NONE !== json_last_error()) {
    throw new \UnexpectedValueException('Could not encode JSON: ' . json_last_error_msg());
  }
  $bytes = file_put_contents($filename, $json);
  if ($bytes === FALSE) {
    throw new \RuntimeException(sprintf('Could not write to %s', $filename));
  }
  return $bytes;
}