finder_eval
| Versions | |
|---|---|
| 6.x-1.x – 7.x-1.x | finder_eval($code, $variables = array()) |
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.
Code
./
<?php
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;
}
?>
