You are here

function coder_upgrade_find_hook in Coder 7

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

Finds the text of a particular function.

This is used with regular expressions, but is obsoleted by the parser.

Parameters

string $hook: By default, the suffix of the function name to find. Example: passing $hook = 'menu' will find a function whose name ends in '_menu'. When $hook_is_suffix = FALSE, then $hook is the entire function name to find.

string $file: The file to search.

boolean $match_all: When TRUE, find all functions with $hook in the name.

boolean $hook_is_suffix: The $hook is only the suffix of the function name.

Return value

string The function text.

File

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

Code

function coder_upgrade_find_hook($hook, $file, $match_all = FALSE, $hook_is_suffix = TRUE) {

  // Construct pattern based on function parameters.
  $prefix = $hook_is_suffix ? '\\w+_' : '';
  $pattern = '/^function\\s*';

  //  $pattern .= $hook_is_suffix ? '\w+_' : '';
  $pattern .= $prefix . $hook . '\\s*\\(.*?(?=(\\/\\*\\*|^function|\\z))/ms';
  if ($match_all) {
    preg_match_all($pattern, $file, $matches, PREG_PATTERN_ORDER);

    // This block should be unnecessary with the changes to pattern above.
    if (!isset($matches[0][0])) {

      // Check to see if the function name exists.
      $pattern = '/^function\\s*' . $prefix . $hook . '\\s*\\(/m';
      preg_match($pattern, $file, $matches);
      if (!isset($matches[0])) {
        return array();
      }

      // Find last function in file.
      $pattern = '/^function\\s*' . $prefix . $hook . '.*\\z/ms';
      preg_match_all($pattern, $file, $matches, PREG_PATTERN_ORDER);
      coder_upgrade_log_print('Primary search failed to find function text for _' . $hook . '. Resorting to secondary pattern to find function.');
    }
    return isset($matches[0]) ? $matches[0] : array();
  }
  else {
    preg_match($pattern, $file, $matches);

    // This block should be unnecessary with the changes to pattern above.
    if (!isset($matches[0])) {

      // Check to see if the function name exists.
      $pattern = '/^function\\s*' . $prefix . $hook . '\\s*\\(/m';
      preg_match($pattern, $file, $matches);
      if (!isset($matches[0])) {
        return '';
      }

      // Find last function in file.
      $pattern = '/^function\\s*' . $prefix . $hook . '.*\\z/ms';
      preg_match($pattern, $file, $matches);
      coder_upgrade_log_print('Primary search failed to find function text for _' . $hook . '. Resorting to secondary pattern to find function.');
    }
    return isset($matches[0]) ? $matches[0] : '';
  }
}