You are here

public static function HudtInternal::writeFile in Hook Update Deploy Tools 8

Same name and namespace in other branches
  1. 7 src/HudtInternal.php \HookUpdateDeployTools\HudtInternal::writeFile()

Writes a file to a path with contents.

Parameters

string $file_uri: The full root path to the contents and the filename with extension.

string $file_contents: The contents of the file.

Return value

mixed string of the path is writing was successful. FALSE if otherwise.

Throws

\Exception If writing is unsuccessful an Exception is thrown and caught.

2 calls to HudtInternal::writeFile()
PageManager::export in src/PageManager.php
Exports a single PageManager page (typically called from Drush).
Rules::export in src/Rules.php
Exports a single Rule when called (typically called from Drush).

File

src/HudtInternal.php, line 259

Class

HudtInternal
Methods for processes internal to Hook Deploy Update Tools.

Namespace

HookUpdateDeployTools

Code

public static function writeFile($file_uri, $file_contents) {
  $variables = array(
    '@file_uri' => $file_uri,
  );
  $msg_return = FALSE;
  try {
    $fh = fopen($file_uri, 'w');
    if ($fh) {
      fwrite($fh, $file_contents);
      fclose($fh);

      // Successful, so return the uri of the file.
      $msg_return = $file_uri;
    }
    else {
      $message = "Error (likely permissions) creating the file: @file_uri";
      throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
    }
  } catch (\Exception $e) {
    $variables['@error'] = $e
      ->getMessage();
    $msg = dt("Failed writing to @file_uri.  Caught exception:  @error", $variables);
    drush_log($msg, 'error');

    // Output file to terminal so it is available to use.
    drush_print(dt("The file was not generated. Outputting contents to terminal.\n"));
    drush_print('------------------------------------------------------------');
    drush_print($file_contents);
    drush_print("------------------------------------------------------------\n\n");
  }
  return $msg_return;
}