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