1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 18.02.16 at 14:15 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\view; |
7
|
|
|
|
8
|
|
|
use samsonframework\core\RenderInterface; |
9
|
|
|
use samsonframework\core\ViewInterface; |
10
|
|
|
use samsonframework\view\exception\VariableKeyNotFound; |
11
|
|
|
use samsonframework\view\exception\ViewClassNotFound; |
12
|
|
|
use samsonframework\view\exception\ViewFileNotFound; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* View class for rendering. |
16
|
|
|
* @package samsonframework\view |
17
|
|
|
*/ |
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
|
|
|
* 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
|
|
|
* data set before to new view data pointer. This guarantees backward compatibility and gives |
57
|
|
|
* opportunity not to set the view path before setting view data to it. |
58
|
|
|
* |
59
|
|
|
* @param string $pathPattern Path pattern for view searching |
60
|
|
|
* |
61
|
|
|
* @return $this Chaining |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
2 |
|
public function view($pathPattern) |
65
|
|
|
{ |
66
|
2 |
|
if (file_exists($pathPattern)) { |
67
|
2 |
|
$this->file = $pathPattern; |
68
|
2 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
throw new ViewFileNotFound($pathPattern); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Render current view. |
76
|
|
|
* Method uses current view context and outputs rendering |
77
|
|
|
* result. |
78
|
|
|
* |
79
|
|
|
* @return string Rendered view |
80
|
|
|
*/ |
81
|
3 |
|
public function output() |
82
|
|
|
{ |
83
|
|
|
// Start buffering |
84
|
3 |
|
ob_start(); |
85
|
|
|
|
86
|
|
|
// Make variables accessible directly in view |
87
|
3 |
|
extract($this->data); |
88
|
|
|
|
89
|
|
|
// Render view from source |
90
|
3 |
|
if (!empty($this->source)) { |
91
|
2 |
|
eval(' ?>' . $this->source . '<?php '); |
92
|
2 |
|
} else { // Render view from file |
93
|
1 |
|
include($this->file); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Store buffer output |
97
|
3 |
|
$this->output = ob_get_contents(); |
98
|
|
|
|
99
|
|
|
// Clear buffer |
100
|
3 |
|
ob_end_clean(); |
101
|
|
|
|
102
|
|
|
// Remove variables from context to free memory |
103
|
3 |
|
foreach ($this->data as $key => $value) { |
104
|
3 |
|
unset($key); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
// Returned rendered view |
108
|
3 |
|
return $this->output; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Magic method for getting view variables. |
113
|
|
|
* |
114
|
|
|
* @param string $name Variable key |
115
|
|
|
* |
116
|
|
|
* @return mixed Value |
117
|
|
|
* @throws VariableKeyNotFound |
118
|
|
|
*/ |
119
|
5 |
|
public function __get($name) |
120
|
|
|
{ |
121
|
5 |
|
if (array_key_exists($name, $this->data)) { |
122
|
5 |
|
return $this->data[$name]; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
throw new VariableKeyNotFound($name); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set current view parent rendering view and block. |
130
|
|
|
* |
131
|
|
|
* @param string $parent Fully qualified parent view name |
132
|
|
|
* @param string $block View block for rendering in parent view |
133
|
|
|
* |
134
|
|
|
* @throws ViewClassNotFound |
135
|
|
|
*/ |
136
|
1 |
|
public function extend($parent, $block) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
//$parent = new $parent(); |
139
|
|
|
//$parent->output(); |
140
|
|
|
|
141
|
|
|
//throw new ViewClassNotFound($parent); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set current view nested block rendering view. |
146
|
|
|
* |
147
|
|
|
* @param string $blockName Nested view block container |
148
|
|
|
*/ |
149
|
|
|
public function block($blockName) |
150
|
|
|
{ |
151
|
|
|
$this->blocks[] = $blockName; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Magic method for setting view variables. |
156
|
|
|
* |
157
|
|
|
* @param string $name Variable key |
158
|
|
|
* @param array $arguments Variable value |
159
|
|
|
* |
160
|
|
|
* @return $this Chaining |
161
|
|
|
* @throws VariableKeyNotFound |
162
|
|
|
*/ |
163
|
1 |
|
public function __call($name, $arguments) |
164
|
|
|
{ |
165
|
1 |
|
if (count($arguments)) { |
166
|
1 |
|
$this->set($arguments[0], $name); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set view variable. |
174
|
|
|
* |
175
|
|
|
* Passing an array as $value will add array key => values into current |
176
|
|
|
* view data collection. If $key is passed then an array variable with this |
177
|
|
|
* 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
|
7 |
|
public function set($value, $key = null) |
185
|
|
|
{ |
186
|
|
|
// RenderInterface implementation |
187
|
7 |
|
if (is_object($value) && is_a($value, RenderInterface::class)) { |
188
|
3 |
|
$this->setRenderableObject($value, $key); |
189
|
7 |
|
} elseif (is_array($value)) { // Merge array into view data |
190
|
3 |
|
$this->data = array_merge($this->data, $value); |
191
|
3 |
|
} |
192
|
|
|
|
193
|
|
|
// Store key value |
194
|
7 |
|
$this->data[$key] = $value; |
195
|
|
|
|
196
|
7 |
|
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
|
3 |
|
protected function setRenderableObject($object, $key) |
206
|
|
|
{ |
207
|
|
|
/** @var RenderInterface $object */ |
208
|
|
|
// Generate objects view array data and merge it with view data |
209
|
3 |
|
$this->data = array_merge( |
210
|
3 |
|
$this->data, |
211
|
3 |
|
$object->toView(null !== $key ? $key : get_class($object)) |
212
|
3 |
|
); |
213
|
3 |
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.