You are here

function coder_upgrade_convert_functions in Coder 7

Same name in this branch
  1. 7 coder_upgrade/includes/main.inc \coder_upgrade_convert_functions()
  2. 7 coder_upgrade/conversions/other.inc \coder_upgrade_convert_functions()
Same name and namespace in other branches
  1. 7.2 coder_upgrade/includes/main.inc \coder_upgrade_convert_functions()
  2. 7.2 coder_upgrade/conversions/other.inc \coder_upgrade_convert_functions()

Upgrades functions (or hooks) using grammar parser.

Parameters

PGPReader $reader: The object containing the grammar statements of the file to convert.

1 call to coder_upgrade_convert_functions()
coder_upgrade_apply_parser in coder_upgrade/includes/main.inc
Applies grammar parser conversion routines to a file.

File

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

Code

function coder_upgrade_convert_functions(&$reader) {
  cdp("inside " . __FUNCTION__);
  global $_coder_upgrade_module_name;
  $nodes =& $reader
    ->getFunctions();
  foreach ($nodes as &$node) {
    $item =& $node->data;
    if (!isset($item) || !is_object($item) || !$item instanceof PGPClass || $item->type != T_FUNCTION) {

      // The reference could have been changed in another routine so that it
      // no longer refers to an object.
      continue;
    }
    $name =& $item->name;
    cdp("name = {$name}");

    // Convert theme_xxx function calls.
    //    if (strpos($name, 'theme_') === 0) {
    //      drupal_alter('upgrade_theme', $node, $reader);
    //      continue;
    //    }

    /*
     * If the function name does not begin with the module name, then ignore it.
     * This assumes such a function would be an instance of an API hook defined
     * by the contributed module but implemented on behalf of another module. For
     * this use case, the contributed module would define upgrade routines to
     * allow other contributed modules that implement said API to upgrade their
     * code.
     *
     * Example: the Views module defines hooks and implements them on behalf of
     * core modules.
     *
     * Strip the module name from the function name and use this as the key in
     * a switch statement. In some cases (e.g. hook_update_N), some additional
     * manipulation and checking needs to be done.
     */
    if (strpos($name, $_coder_upgrade_module_name . '_') !== 0 && strpos($name, 'theme_') !== 0) {
      clp("Ignoring function '{$name}' as its name does not begin with the module name or 'theme_'");
      continue;
    }

    // By convention, the module name should be followed by an underscore.
    $hook = substr($name, strlen($_coder_upgrade_module_name) + 1);
    cdp("hook = {$hook}");

    // Update hooks need additional manipulation.
    if (preg_match('@update_\\d+$@', $hook, $matches)) {
      $hook = 'update_N';
    }

    // TODO The alter hooks can get the $reader from PGPReader::getInstance();
    // We could do the same in this function.
    drupal_alter('upgrade_hook_' . $hook, $node, $reader);
    if (!isset($item) || !is_object($item) || !$item instanceof PGPClass || $item->type != T_FUNCTION) {
      continue;
    }
    if (strpos($name, $_coder_upgrade_module_name . '_') !== 0 && strpos($name, 'theme_') !== 0) {
      clp("Ignoring function '{$name}' as its name does not begin with the module name or 'theme_'");
      continue;
    }
    drupal_alter('upgrade_hook', $node, $reader, $hook);
  }
}