You are here

function coder_format_file in Coder 5

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

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

2 calls to coder_format_file()
coder_format.php in scripts/coder_format/coder_format.php
coder_format_recursive in scripts/coder_format/coder_format.inc
Recursively process .module and .inc files in directory.

File

scripts/coder_format/coder_format.inc, line 67

Code

function coder_format_file($sourcefile = null, $targetfile = null) {
  if (!isset($sourcefile) || !isset($targetfile)) {
    return;
  }

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

    // Preprocess source code
    $code = coder_exec_processors($code, 'coder_preprocessor');

    // Process source code
    $code = coder_format_string($code);

    // Postprocess source code
    $code = coder_exec_processors($code, 'coder_postprocessor');

    // Fix beginning and end of code
    $code = coder_trim_php($code);
    if ($code !== false) {

      // Write formatted source code to target file
      $fd = fopen($targetfile, 'w');
      $status = fwrite($fd, $code);
      fclose($fd);
      return $status;
    }
  }
  return false;
}