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