You are here

function coder_format_file in Coder 7.2

Same name and namespace in other branches
  1. 5.2 scripts/coder_format/coder_format.inc \coder_format_file()
  2. 5 scripts/coder_format/coder_format.inc \coder_format_file()
  3. 6.2 scripts/coder_format/coder_format.inc \coder_format_file()
  4. 6 scripts/coder_format/coder_format.inc \coder_format_file()
  5. 7 scripts/coder_format/coder_format.inc \coder_format_file()

Reads, backups, processes and writes the source code from and to a file.

Parameters

$filename: Path to a file to process or restore. Pass original filename to restore an already processed file.

$undo: Whether to restore a processed file. Always restores the last backup.

Return value

TRUE on success.

3 calls to coder_format_file()
coder_format.php in scripts/coder_format/coder_format.php
Coder format shell invocation script.
coder_format_recursive in scripts/coder_format/coder_format.inc
Recursively process .module and .inc files in directory with coder_format_file().
drush_coder_format in ./coder.drush.inc
Performs the desired coder-format command for drush.

File

scripts/coder_format/coder_format.inc, line 52
Coder format helper functions.

Code

function coder_format_file($filename, $undo = FALSE) {

  // Restore a processed file.
  if ($undo) {

    // Do nothing if no backup file exists at all.
    if (!file_exists($filename . '.coder.orig')) {
      return;
    }

    // Save original filename.
    $original = $filename;

    // Retrieve the file's basename.
    $basename = basename($filename);

    // Find all backups.
    $dirname = dirname($filename);
    $mask = '^' . preg_quote($basename, '/') . '(\\.coder\\.orig)+$';
    $nomask = array(
      '.',
      '..',
      'CVS',
      '.svn',
      '.git',
    );
    if (function_exists('drush_scan_directory')) {
      $backups = drush_scan_directory($dirname, '/' . $mask . '/', $nomask, 0, FALSE);
    }
    else {
      $backups = file_scan_directory($dirname, $mask, $nomask, 0, FALSE);
    }

    // Find the latest backup to restore.
    ksort($backups);
    $latest = array_pop($backups);

    // Restore latest backup.
    if (unlink($original) && rename($latest->filename, $original)) {
      drupal_set_message(t('%file restored.', array(
        '%file' => $original,
      )));
      return TRUE;
    }
    else {
      drupal_set_message(t('%file could not be restored.', array(
        '%file' => $original,
      )), 'error');
      return FALSE;
    }
  }

  // Backup original file.
  // file_copy() replaces source filepath with target filepath.
  $sourcefile = $filename;
  $targetfile = $filename . '.coder.orig';
  $status = file_exists($filename);
  $status = $status && copy($filename, $targetfile);
  $status = $status && file_exists($targetfile);
  if (!$status) {
    drupal_set_message(t('%file could not be backup.', array(
      '%file' => $filename,
    )), 'error');
    return FALSE;
  }

  // Read source code from source file.
  $fd = fopen($sourcefile, 'r');
  $code = fread($fd, filesize($sourcefile));
  fclose($fd);
  if ($code !== FALSE) {
    $code = coder_format_string_all($code);
    if ($code !== FALSE) {

      // Write formatted source code to target file.
      $fd = fopen($sourcefile, 'w');
      $status = fwrite($fd, $code);
      fclose($fd);
      drupal_set_message(t('%file processed.', array(
        '%file' => $sourcefile,
      )));
      return $status;
    }
    else {
      drupal_set_message(t('An error occurred while processing %file.', array(
        '%file' => $sourcefile,
      )), 'error');
      return FALSE;
    }
  }
  else {
    drupal_set_message(t('%file could not be opened.', array(
      '%file' => $sourcefile,
    )), 'error');
    return FALSE;
  }
}