You are here

function composer_manager_put_file in Composer Manager 6

Same name and namespace in other branches
  1. 6.2 composer_manager.writer.inc \composer_manager_put_file()
  2. 7.2 composer_manager.writer.inc \composer_manager_put_file()
  3. 7 composer_manager.writer.inc \composer_manager_put_file()

Writes the composer.json file in the specified directory.

Parameters

string $dir_uri: The URI of the directory that the composer.json file is being written to. This is usually the "composer_manager_file_dir" system variable.

array $json: A model of the JSON data being written. This is usually the return value of composer_manager_build_json().

Throws

\RuntimeException

1 call to composer_manager_put_file()
composer_manager_write_file in ./composer_manager.module
Writes the consolidated composer.json file for all modules that require third-party packages managed by Composer.

File

./composer_manager.writer.inc, line 152
Functions related to the creation of the consolidated composer.json file.

Code

function composer_manager_put_file($dir_uri, array $json) {
  composer_manager_prepare_directory($dir_uri);

  // Make the composer.json file human readable for PHP >= 5.4.
  // @see drupal_json_encode()
  // @see http://drupal.org/node/1943608
  // @see http://drupal.org/node/1948012
  $json_options = JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
  if (defined('JSON_PRETTY_PRINT')) {
    $json_options = $json_options | JSON_PRETTY_PRINT;
  }
  if (defined('JSON_UNESCAPED_SLASHES')) {
    $json_options = $json_options | JSON_UNESCAPED_SLASHES;
  }
  $file_uri = $dir_uri . '/composer.json';
  if (!@file_put_contents($file_uri, json_encode($json, $json_options))) {
    throw new \RuntimeException(t('Error writing composer.json file: @file', array(
      '@file' => $file_uri,
    )));
  }

  // Displays a message to admins that a file was written.
  if (user_access('administer site configuration')) {
    $command = file_exists($dir_uri . '/composer.lock') ? 'update' : 'install';
    drupal_set_message(t('A composer.json file was written to @path.', array(
      '@path' => realpath($dir_uri),
    )));
    if ('admin/settings/composer-manager' != $_GET['q']) {
      $args = array(
        '!command' => $command,
        '@url' => url('http://drupal.org/project/composer_manager', array(
          'absolute' => TRUE,
        )),
      );
      if ('install' == $command) {
        $message = t('Composer\'s <code>!command</code> command must be run to generate the autoloader and install the required packages.<br/>Refer to the instructions on the <a href="@url" target="_blank">Composer Manager project page</a> for installing packages.', $args);
      }
      else {
        $message = t('Composer\'s <code>!command</code> command must be run to install the required packages.<br/>Refer to the instructions on the <a href="@url" target="_blank">Composer Manager project page</a> for updating packages.', $args);
      }
      drupal_set_message($message, 'warning');
    }
  }
}