You are here

function finder_eval in Finder 7.2

Same name and namespace in other branches
  1. 6 finder.module \finder_eval()
  2. 7 finder.module \finder_eval()

Evaluate a string of PHP code.

This is a wrapper around PHP's eval(). It uses output buffering to capture both returned value and printed text. Allows to use variables with the given code. Using this wrapper also ensures that the PHP code which is evaluated can not overwrite any variables in the calling code, unlike a regular eval() call. In other words, we evaluate the code with independent variable scope.

Parameters

$code: The code to evaluate.

$variables: Variables to import to local variable scope.

Return value

A string containing the printed output of the code, followed by the returned output of the code.

4 calls to finder_eval()
finder::choice_repopulate in includes/finder.inc
Finder choice repopulate.
finder::results in includes/finder.inc
Finder results.
finder_build_style_render in includes/build.inc
Does stuff during the style plugin's render.
finder_get_args in includes/build.inc
Convert finder arguments text field entry to an array of arguments.

File

includes/build.inc, line 408
The finder build functions.

Code

function finder_eval($code, $variables = array()) {
  global $theme_path, $theme_info, $conf;

  // Store current theme path.
  $old_theme_path = $theme_path;

  // Restore theme_path to the theme, as long as drupal_eval() executes,
  // so code evaluted will not see the caller module as the current theme.
  // If theme info is not initialized get the path from theme_default.
  if (!isset($theme_info)) {
    $theme_path = drupal_get_path('theme', $conf['theme_default']);
  }
  else {
    $theme_path = dirname($theme_info->filename);
  }
  extract($variables);
  ob_start();
  print eval('?>' . $code);
  $output = ob_get_contents();
  ob_end_clean();

  // Recover original theme path.
  $theme_path = $old_theme_path;
  return $output;
}