1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: /**
14: * Limited scope for PHP code evaluation and script including.
15: *
16: * @author David Grudl
17: */
18: class LimitedScope
19: {
20:
21: /**
22: * Static class - cannot be instantiated.
23: */
24: final public function __construct()
25: {
26: throw new Nette\StaticClassException;
27: }
28:
29:
30: /**
31: * Evaluates code in limited scope.
32: * @param string PHP code
33: * @param array local variables
34: * @return mixed the return value of the evaluated code
35: */
36: public static function evaluate(/*$code, array $vars = NULL*/)
37: {
38: if (func_num_args() > 1) {
39: foreach (func_get_arg(1) as $__k => $__v) $$__k = $__v;
40: unset($__k, $__v);
41: }
42: $res = eval('?>' . func_get_arg(0));
43: if ($res === FALSE && ($error = error_get_last()) && $error['type'] === E_PARSE) {
44: throw new Nette\FatalErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'], NULL);
45: }
46: return $res;
47: }
48:
49:
50: /**
51: * Includes script in a limited scope.
52: * @param string file to include
53: * @param array local variables or TRUE meaning include once
54: * @return mixed the return value of the included file
55: */
56: public static function load(/*$file, array $vars = NULL*/)
57: {
58: if (func_num_args() > 1) {
59: if (func_get_arg(1) === TRUE) {
60: return require func_get_arg(0);
61: }
62: foreach (func_get_arg(1) as $__k => $__v) $$__k = $__v;
63: unset($__k, $__v);
64: }
65: return require func_get_arg(0);
66: }
67:
68: }
69: