1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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\Loaders
11: */
12:
13:
14:
15: /**
16: * Limited scope for PHP code evaluation and script including.
17: *
18: * @author David Grudl
19: */
20: final class LimitedScope
21: {
22: private static $vars;
23:
24: /**
25: * Static class - cannot be instantiated.
26: */
27: final public function __construct()
28: {
29: throw new LogicException("Cannot instantiate static class " . get_class($this));
30: }
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: return eval('?>' . func_get_arg(0));
47: }
48:
49:
50:
51: /**
52: * Includes script in a limited scope.
53: * @param string file to include
54: * @param array local variables
55: * @return mixed the return value of the included file
56: */
57: public static function load(/*$file, array $vars = NULL*/)
58: {
59: if (func_num_args() > 1) {
60: self::$vars = func_get_arg(1);
61: extract(self::$vars);
62: }
63: return include func_get_arg(0);
64: }
65:
66: }