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: * Evaluates code in limited scope.
36: * @param string PHP code
37: * @param array local variables
38: * @return mixed the return value of the evaluated code
39: */
40: public static function evaluate(/*$code, array $vars = NULL*/)
41: {
42: if (func_num_args() > 1) {
43: self::$vars = func_get_arg(1);
44: extract(self::$vars);
45: }
46: $res = eval('?>' . func_get_arg(0));
47: if ($res === FALSE && ($error = error_get_last()) && $error['type'] === E_PARSE) {
48: throw new FatalErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'], NULL);
49: }
50: return $res;
51: }
52:
53:
54: /**
55: * Includes script in a limited scope.
56: * @param string file to include
57: * @param array local variables or TRUE meaning include once
58: * @return mixed the return value of the included file
59: */
60: public static function load(/*$file, array $vars = NULL*/)
61: {
62: if (func_num_args() > 1) {
63: self::$vars = func_get_arg(1);
64: if (self::$vars === TRUE) {
65: return require func_get_arg(0);
66: }
67: extract(self::$vars);
68: }
69: return require func_get_arg(0);
70: }
71:
72: }
73: