Skip to content

Instantly share code, notes, and snippets.

@palmic
Created March 26, 2013 10:37
Show Gist options
  • Save palmic/5244459 to your computer and use it in GitHub Desktop.
Save palmic/5244459 to your computer and use it in GitHub Desktop.
/**
* This file is part of the Nette Framework (http://nette.org)
*
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Nette\Utils;
use Nette;
/**
* Limited scope for PHP code evaluation and script including.
*
* @author David Grudl
*/
final class LimitedScope
{
/**
* Evaluates code in limited scope.
* @param string PHP code
* @param array local variables
* @return mixed the return value of the evaluated code
*/
public static function evaluate(/*$code, array $vars = NULL*/)
{
if (func_num_args() > 1) {
self::$vars = func_get_arg(1);
extract(self::$vars);
}
$res = eval('?>' . func_get_arg(0));
if ($res === FALSE && ($error = error_get_last()) && $error['type'] === E_PARSE) {
throw new Nette\FatalErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'], NULL);
}
return $res;
}
/**
* Includes script in a limited scope.
* @param string file to include
* @param array local variables
* @return mixed the return value of the included file
*/
public static function load(/*$file, array $vars = NULL*/)
{
if (func_num_args() > 1) {
self::$vars = func_get_arg(1);
extract(self::$vars);
}
return include func_get_arg(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment