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