1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: class RouterForIncludes extends Router {
12:
13:
14:
15: protected $context = array();
16:
17:
18: 19: 20: 21: 22: 23: 24: 25: 26:
27: public function addToContext() {
28:
29: if (func_num_args() === 1) {
30: $data = func_get_arg(0);
31: if (!is_array($data)) {
32: throw new InvalidArgumentException("Router (include kontext): Pokud je volána jen s jedním argumentem, musí to být pole.");
33: }
34: foreach ($data as $key => $value) {
35: $this->addToContext($key, $value);
36: }
37:
38: } elseif (func_num_args() === 2) {
39: $name = func_get_arg(0);
40: $value = func_get_arg(1);
41: if (isset($this->context[$name])) {
42: trigger_error("Router: Proměnná '$name' už v kontextu existuje.", E_USER_NOTICE);
43: }
44: if (!is_scalar($name)) {
45: throw new InvalidArgumentException("Router (include kontext): Název proměnné musí být řetězec.");
46: }
47: $this->context[$name] = $value;
48: }
49: return $this;
50: }
51:
52:
53: 54: 55: 56: 57: 58: 59:
60: protected function callbackFromString($str) {
61: $cb = array();
62: foreach (explode(',', $str) as $file) {
63: if (!file_exists($file)) {
64: throw new RuntimeException("Router: Soubor '$file' neexistuje.");
65: }
66: $cb[] = $file;
67: }
68: return $cb;
69: }
70:
71:
72: 73: 74:
75: public function delegate() {
76: try {
77: foreach ($this->callbackFromString($this->callback) as $filename) {
78: $this->localInclude($filename);
79: }
80: } catch (RuntimeException $e) {
81: if (empty($this->errorCallback)) {
82: throw $e;
83: }
84: foreach ($this->callbackFromString($this->errorCallback) as $filename) {
85: $this->localInclude($filename);
86: }
87: }
88: }
89:
90:
91: 92: 93: 94:
95: protected function localInclude($filename) {
96: if (!empty($this->context)) {
97: extract($this->context);
98: }
99: $router = $this;
100:
101: include $filename;
102: }
103:
104: }
105: