You are here

function coder_format_recursive in Coder 5

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

Recursively process .module and .inc files in directory.

Parameters

bool undo Whether to undo batch replacements.:

1 call to coder_format_recursive()
coder_format.php in scripts/coder_format/coder_format.php

File

scripts/coder_format/coder_format.inc, line 8

Code

function coder_format_recursive($root, $undo = false, $file_inc = null) {

  // Convert Windows paths
  $root = str_replace("\\", '/', $root);

  // Check if directory exists
  if (!file_exists($root)) {
    echo 'ERROR: ' . $root . ' does not exist!';
    return;
  }

  // Trim root directory
  if (substr($root, -1) == '/') {
    $root = substr($root, 0, -1);
  }

  // Include Drupal's file.inc
  if (!isset($file_inc)) {
    if (!file_exists($root . '/includes/file.inc')) {
      echo 'ERROR: ' . $root . '/includes/file.inc not found!';
      return;
    }
  }
  else {
    if (!file_exists($file_inc)) {
      echo 'ERROR: ' . $file_inc . ' not found!';
      return;
    }
  }
  require_once !isset($file_inc) ? $root . '/includes/file.inc' : $file_inc;
  if (!$undo) {

    // Fetch files to process
    $mask = '.module$|.inc$|.install|.profile$';
    $nomask = array(
      '.',
      '..',
      'CVS',
      '.svn',
    );
    $files = file_scan_directory($root, $mask, $nomask, 0, true);
    foreach ($files as $file) {

      // file_copy() assigns $file->filename by reference
      $sourcefile = $file->filename;
      echo $file->filename . "\n";
      file_copy($file->filename, $file->filename . '.coder.orig');
      coder_format_file($sourcefile, $sourcefile);
    }
  }
  else {

    // Fetch files to process
    $mask = '.coder.orig$';
    $nomask = array(
      '.',
      '..',
      'CVS',
      '.svn',
    );
    $files = file_scan_directory($root, $mask, $nomask, 0, true);
    foreach ($files as $file) {
      $file->origname = str_replace('.coder.orig', '', $file->filename);
      echo $file->origname . "\n";
      file_move($file->filename, $file->origname, FILE_EXISTS_REPLACE);
    }
  }
}