You are here

function coder_upgrade_convert_perm in Coder 7

Same name and namespace in other branches
  1. 7.2 coder_upgrade/conversions/other.inc \coder_upgrade_convert_perm()

Updates hook_perm().

Rename hook_perm() to hook_permission(). Permissions are required to have titles and descriptions.

Parameters

PGPNode $node: A node object containing a PGPClass (or function) item.

1 call to coder_upgrade_convert_perm()
coder_upgrade_callback_functions in coder_upgrade/conversions/other.inc
Callback routine for function changes using grammar parser.

File

coder_upgrade/conversions/other.inc, line 1196
Other conversion routine file for the coder_upgrade module.

Code

function coder_upgrade_convert_perm(&$node) {
  cdp("inside " . __FUNCTION__);
  $item =& $node->data;
  cdp($item
    ->print_r());

  // Rename function.
  $item->name .= 'ission';

  // Update document comment.
  $item->comment = preg_replace('@hook_perm([^i])@', "hook_permission\$1", $item->comment);

  // Restructure the permissions array.
  $body =& $item->body;
  if (!($return = $body
    ->find(T_RETURN, 'reverse'))) {
    clp("ERROR: return statement not found in hook_perm");
    return;
  }

  //  cdp("Printing return item");
  $value =& $return->value;
  cdp($value
    ->print_r());
  cdp($value
    ->toString());

  //  cdp("Printing return item DONE");
  $array = $value
    ->getElement();
  if (!is_a($array, 'PGPArray')) {
    clp("ERROR: return statement does not include an array of values in hook_perm");
    return;
  }

  // Grab the PGPList of values.
  $values = $array->values;
  $string = "array(\n";
  $current = $values
    ->first();
  while ($current->next != NULL) {
    if ($current->type == 'value') {

      // If the permission is other than a string (e.g. T_VARIABLE expression),
      // then do not enclose in quotes.
      $expression =& $current->data
        ->first()->data;

      // TODO Fix next line when condition is TRUE
      $type = is_object($expression) ? '$expression->type' : $expression['type'];

      // Operand object does not have a type parameter // $type = is_object($expression) ? $expression->type : $expression['type'];
      $add_quotes = $type == T_CONSTANT_ENCAPSED_STRING;
      $permission =& $current->data
        ->toString();

      //      cdp("permission = $permission");
      $permission = trim($permission, "'\"");
      if ($add_quotes) {
        $string .= "'{$permission}' => array('title' => t('{$permission}'), 'description' => t('TODO Add a description for {$permission}'),),\n";
      }
      else {
        $permission2 = str_replace("'", "\\'", $permission);
        $string .= "{$permission} => array('title' => t({$permission}), 'description' => t('TODO Add a description for {$permission2}'),),\n";
      }
    }
    $current =& $current->next;
  }
  $string .= ");\n";

  //  cdp($string);
  $editor = new PGPEditor();
  $expression = $editor
    ->expressionToStatement($string);

  //  cdp("PRINT NEW expression");
  //  cdp($expression->print_r(3));
  //  cdp("PRINT NEW expression DONE");
  // Set the return array to the new array.
  $value
    ->setElement(0, $expression);
}