You are here

function coder_upgrade_apply_parser in Coder 7

Same name and namespace in other branches
  1. 7.2 coder_upgrade/includes/main.inc \coder_upgrade_apply_parser()

Applies grammar parser conversion routines to a file.

Parameters

string $filename: The name of the file to convert.

string $new: The contents of the file to convert.

1 call to coder_upgrade_apply_parser()
coder_upgrade_convert_file in coder_upgrade/includes/main.inc
Converts a file.

File

coder_upgrade/includes/main.inc, line 337
Manages application of conversion routines, logging, and patch file creation.

Code

function coder_upgrade_apply_parser($filename, &$new) {
  global $_coder_upgrade_class_files;

  // Categorize certain files.
  $is_info_file = pathinfo($filename, PATHINFO_EXTENSION) == 'info';
  $is_install_file = pathinfo($filename, PATHINFO_EXTENSION) == 'install';
  if ($is_info_file) {

    // Grammar parser only parses PHP code files.
    return;
  }

  // Create reader object.
  $reader = PGPReader::getInstance();
  coder_upgrade_memory_print('create reader for file ' . $filename);

  // Set array formatting preference.
  $reader
    ->setPreserveArrayFormat(variable_get('coder_upgrade_preserve_array_format', FALSE));

  // Set debug output preference.
  $reader
    ->setDebug(variable_get('coder_upgrade_enable_parser_debug_output', FALSE));
  $reader
    ->setSnippet($new);
  coder_upgrade_memory_print('set snippet');
  $reader
    ->addTokenNames();
  coder_upgrade_memory_print('add token names');

  // Parse code file.
  $reader
    ->buildGrammar();
  coder_upgrade_memory_print('build grammar');
  $nodes =& $reader
    ->getClasses();
  if (!empty($nodes)) {
    $_coder_upgrade_class_files[] = $filename;
  }

  // Apply parser conversion routines for function calls.
  coder_upgrade_log_print("Calling hook_upgrade_call_alter");
  coder_upgrade_convert_function_calls($reader);
  coder_upgrade_log_print("Completed hook_upgrade_call_alter");
  coder_upgrade_memory_print('apply function call conversions');

  // Apply parser conversion routines for hook functions.
  coder_upgrade_log_print("Calling hook_upgrade_hook_alter");
  coder_upgrade_convert_functions($reader);
  coder_upgrade_log_print("Completed hook_upgrade_hook_alter");
  coder_upgrade_memory_print('apply hook function conversions');

  // Apply parser conversion routines for the file.
  coder_upgrade_log_print("Calling hook_upgrade_file_alter");
  drupal_alter('upgrade_file', $reader);
  coder_upgrade_log_print("Completed hook_upgrade_file_alter");
  coder_upgrade_memory_print('apply file conversions');
  if ($is_install_file) {

    // Apply parser conversion routines for install file.
    coder_upgrade_log_print("Calling hook_upgrade_parser_install_alter");
    drupal_alter('upgrade_parser_install', $reader);
    coder_upgrade_log_print("Completed hook_upgrade_parser_install_alter");
    coder_upgrade_memory_print('apply install file conversions');
  }

  // Use writer to redo file.
  $writer = PGPWriter::getInstance();
  coder_upgrade_memory_print('create writer');
  $new = $writer
    ->toString($reader
    ->getStatements());
  coder_upgrade_memory_print('writer->toString');

  // Free up memory.
  $reader
    ->reset();
  coder_upgrade_memory_print('reset reader');
}