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: * Limited scope for PHP code evaluation and script including.
19: *
20: * @author David Grudl
21: */
22: final class LimitedScope
23: {
24: private static $vars;
25:
26: /**
27: * Static class - cannot be instantiated.
28: */
29: final public function __construct()
30: {
31: throw new Nette\StaticClassException;
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 Nette\FatalErrorException($error['message'], 0, $error['type'], $error['file'], $error['line'], NULL);
50: }
51: return $res;
52: }
53:
54:
55: /**
56: * Includes script in a limited scope.
57: * @param string file to include
58: * @param array local variables or TRUE meaning include once
59: * @return mixed the return value of the included file
60: */
61: public static function load(/*$file, array $vars = NULL*/)
62: {
63: if (func_num_args() > 1) {
64: self::$vars = func_get_arg(1);
65: if (self::$vars === TRUE) {
66: return include_once func_get_arg(0);
67: }
68: extract(self::$vars);
69: }
70: return include func_get_arg(0);
71: }
72:
73: }
74: