1 | <?php |
||
18 | class View implements ViewInterface |
||
19 | { |
||
20 | /** Default view file extension */ |
||
21 | const DEFAULT_EXT = 'vphp'; |
||
22 | |||
23 | /** @var array Collection of $key => $value view data */ |
||
24 | protected $data = array(); |
||
25 | |||
26 | /** @var string Full path to view file */ |
||
27 | protected $file; |
||
28 | |||
29 | /** @var string View source code */ |
||
30 | protected $source; |
||
31 | |||
32 | /** @var string Rendered view contents */ |
||
33 | protected $output; |
||
34 | |||
35 | /** @var string Parent view class name */ |
||
36 | protected $parentView; |
||
37 | |||
38 | /** @var string Parent view block name */ |
||
39 | protected $parentBlock; |
||
40 | |||
41 | /** @var array Collection of view blocks */ |
||
42 | protected $blocks = array(); |
||
43 | |||
44 | /** |
||
45 | * Set current view for rendering. |
||
46 | * Method searches for the shortest matching view path by $pathPattern, |
||
47 | * from loaded views. |
||
48 | * |
||
49 | * Module saves all view data that has been set to a specific view in appropriate |
||
50 | * view data collection entry. By default module creates vied data entry - VD_POINTER_DEF, |
||
51 | * and until any call of iModule::view() or iModule::output(), all data that is iModule::set(), |
||
52 | * is stored to that location. |
||
53 | * |
||
54 | 2 | * On the first call of iModule::view() or iModule::output(), this method changes the view data |
|
55 | * pointer to actual relative view path, and copies(actually just sets view data pointer) all view |
||
56 | 2 | * data set before to new view data pointer. This guarantees backward compatibility and gives |
|
57 | 2 | * opportunity not to set the view path before setting view data to it. |
|
58 | 2 | * |
|
59 | * @param string $pathPattern Path pattern for view searching |
||
60 | * |
||
61 | 1 | * @return $this Chaining |
|
62 | * @throws \Exception |
||
63 | */ |
||
64 | public function view($pathPattern) |
||
65 | { |
||
66 | if (file_exists($pathPattern)) { |
||
67 | $this->file = $pathPattern; |
||
68 | return $this; |
||
69 | } |
||
70 | |||
71 | 2 | throw new ViewFileNotFound($pathPattern); |
|
72 | } |
||
73 | |||
74 | 2 | /** |
|
75 | * Render current view. |
||
76 | * Method uses current view context and outputs rendering |
||
77 | 2 | * result. |
|
78 | * |
||
79 | * @return string Rendered view |
||
80 | 2 | */ |
|
81 | 1 | public function output() |
|
82 | 1 | { |
|
83 | 1 | // Start buffering |
|
84 | ob_start(); |
||
85 | |||
86 | // Make variables accessible directly in view |
||
87 | 2 | extract($this->data); |
|
88 | |||
89 | // Render view from source |
||
90 | 2 | if (!empty($this->source)) { |
|
91 | eval(' ?>' . $this->source . '<?php '); |
||
92 | } else { // Render view from file |
||
93 | 2 | include($this->file); |
|
94 | 2 | } |
|
95 | 2 | ||
96 | // Store buffer output |
||
97 | $this->output = ob_get_contents(); |
||
98 | 2 | ||
99 | // Clear buffer |
||
100 | ob_end_clean(); |
||
101 | |||
102 | // Remove variables from context to free memory |
||
103 | foreach ($this->data as $key => $value) { |
||
104 | unset($key); |
||
105 | } |
||
106 | |||
107 | // Returned rendered view |
||
108 | return $this->output; |
||
109 | 5 | } |
|
110 | |||
111 | 5 | /** |
|
112 | 5 | * Magic method for getting view variables. |
|
113 | * |
||
114 | * @param string $name Variable key |
||
115 | 1 | * |
|
116 | * @return mixed Value |
||
117 | * @throws VariableKeyNotFound |
||
118 | */ |
||
119 | public function __get($name) |
||
120 | { |
||
121 | if (array_key_exists($name, $this->data)) { |
||
122 | return $this->data[$name]; |
||
123 | } |
||
124 | |||
125 | throw new VariableKeyNotFound($name); |
||
126 | } |
||
127 | 1 | ||
128 | /** |
||
129 | 1 | * Set current view parent rendering view and block. |
|
130 | 1 | * |
|
131 | 1 | * @param string $parent Fully qualified parent view name |
|
132 | * @param string $block View block for rendering in parent view |
||
133 | 1 | * |
|
134 | * @throws ViewClassNotFound |
||
135 | */ |
||
136 | public function extend($parent, $block) |
||
137 | { |
||
138 | //$parent = new $parent(); |
||
139 | /$parent->output(); |
||
|
|||
140 | |||
141 | //throw new ViewClassNotFound($parent); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Set current view nested block rendering view. |
||
146 | * |
||
147 | * @param string $blockName Nested view block container |
||
148 | 6 | */ |
|
149 | public function block($blockName) |
||
150 | { |
||
151 | 6 | $this->blocks[] = $blockName; |
|
152 | 3 | } |
|
153 | 6 | ||
154 | 3 | /** |
|
155 | 3 | * Magic method for setting view variables. |
|
156 | * |
||
157 | * @param string $name Variable key |
||
158 | 6 | * @param array $arguments Variable value |
|
159 | * |
||
160 | 6 | * @return $this Chaining |
|
161 | * @throws VariableKeyNotFound |
||
162 | */ |
||
163 | public function __call($name, $arguments) |
||
164 | { |
||
165 | if (count($arguments)) { |
||
166 | $this->set($arguments[0], $name); |
||
167 | } |
||
168 | |||
169 | 3 | return $this; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | 3 | * Set view variable. |
|
174 | 3 | * |
|
175 | 3 | * Passing an array as $value will add array key => values into current |
|
176 | 3 | * view data collection. If $key is passed then an array variable with this |
|
177 | 3 | * key will be added to view data collection beside adding array key => values. |
|
178 | * |
||
179 | * @param mixed $value Variable value |
||
180 | * @param string|null $key Variable key\prefix for objects and arrays |
||
181 | * |
||
182 | * @return $this Chaining |
||
183 | */ |
||
184 | public function set($value, $key = null) |
||
185 | { |
||
186 | // RenderInterface implementation |
||
187 | if (is_object($value) && is_a($value, RenderInterface::class)) { |
||
188 | $this->setRenderableObject($value, $key); |
||
189 | } elseif (is_array($value)) { // Merge array into view data |
||
190 | $this->data = array_merge($this->data, $value); |
||
191 | } |
||
192 | |||
193 | // Store key value |
||
194 | $this->data[$key] = $value; |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Set renderable object as view variable. |
||
201 | * |
||
202 | * @param mixed $object Object instance for rendering |
||
203 | * @param string|null $key Variable key\prefix for objects and arrays |
||
204 | */ |
||
205 | protected function setRenderableObject($object, $key) |
||
206 | { |
||
207 | /** @var RenderInterface $object */ |
||
208 | // Generate objects view array data and merge it with view data |
||
209 | $this->data = array_merge( |
||
210 | $this->data, |
||
211 | $object->toView(null !== $key ? $key : get_class($object)) |
||
212 | ); |
||
213 | } |
||
214 | } |
||
215 |