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\ViewFileNotFound; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* View class for rendering. |
15
|
|
|
* @package samsonframework\view |
16
|
|
|
*/ |
17
|
|
|
class View implements ViewInterface |
18
|
|
|
{ |
19
|
|
|
/** Default view file extension */ |
20
|
|
|
const DEFAULT_EXT = 'vphp'; |
21
|
|
|
|
22
|
|
|
/** @deprecated Class name for old PHP versions */ |
23
|
|
|
const CLASSNAME = __CLASS__; |
24
|
|
|
|
25
|
|
|
/** @var array Collection of $key => $value view data */ |
26
|
|
|
protected $data = array(); |
27
|
|
|
|
28
|
|
|
/** @var string Full path to view file */ |
29
|
|
|
protected $file; |
30
|
|
|
|
31
|
|
|
/** @var string Rendered view contents */ |
32
|
|
|
protected $output; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set current view for rendering. |
36
|
|
|
* Method searches for the shortest matching view path by $pathPattern, |
37
|
|
|
* from loaded views. |
38
|
|
|
* |
39
|
|
|
* Module saves all view data that has been set to a specific view in appropriate |
40
|
|
|
* view data collection entry. By default module creates vied data entry - VD_POINTER_DEF, |
41
|
|
|
* and until any call of iModule::view() or iModule::output(), all data that is iModule::set(), |
42
|
|
|
* is stored to that location. |
43
|
|
|
* |
44
|
|
|
* On the first call of iModule::view() or iModule::output(), this method changes the view data |
45
|
|
|
* pointer to actual relative view path, and copies(actually just sets view data pointer) all view |
46
|
|
|
* data set before to new view data pointer. This guarantees backward compatibility and gives |
47
|
|
|
* opportunity not to set the view path before setting view data to it. |
48
|
|
|
* |
49
|
|
|
* @param string $pathPattern Path pattern for view searching |
50
|
|
|
* |
51
|
|
|
* @return $this Chaining |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
2 |
|
public function view($pathPattern) |
55
|
|
|
{ |
56
|
2 |
|
if (file_exists($pathPattern)) { |
57
|
2 |
|
$this->file = $pathPattern; |
58
|
2 |
|
} else { |
59
|
1 |
|
throw new ViewFileNotFound($pathPattern); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Render current view. |
67
|
|
|
* Method uses current view context and outputs rendering |
68
|
|
|
* result. |
69
|
|
|
* |
70
|
|
|
* @return string Rendered view |
71
|
|
|
*/ |
72
|
1 |
|
public function output() |
73
|
|
|
{ |
74
|
|
|
// Start buffering |
75
|
1 |
|
ob_start(); |
76
|
|
|
|
77
|
|
|
// Make variables accessible directly in view |
78
|
1 |
|
extract($this->data); |
79
|
|
|
|
80
|
|
|
// Render view file |
81
|
1 |
|
include($this->file); |
82
|
|
|
|
83
|
|
|
// Store buffer output |
84
|
1 |
|
$this->output = ob_get_contents(); |
85
|
|
|
|
86
|
|
|
// Clear buffer |
87
|
1 |
|
ob_end_clean(); |
88
|
|
|
|
89
|
|
|
// Returned rendered view |
90
|
1 |
|
return $this->output; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Magic method for getting view variables. |
95
|
|
|
* |
96
|
|
|
* @param string $name Variable key |
97
|
|
|
* |
98
|
|
|
* @return mixed Value |
99
|
|
|
* @throws VariableKeyNotFound |
100
|
|
|
*/ |
101
|
4 |
|
public function __get($name) |
102
|
|
|
{ |
103
|
4 |
|
if (array_key_exists($name, $this->data)) { |
104
|
4 |
|
return $this->data[$name]; |
105
|
|
|
} else { |
106
|
1 |
|
throw new VariableKeyNotFound($name); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Magic method for setting view variables. |
112
|
|
|
* |
113
|
|
|
* @param string $name Variable key |
114
|
|
|
* @param array $arguments Variable value |
115
|
|
|
* |
116
|
|
|
* @return $this Chaining |
117
|
|
|
* @throws VariableKeyNotFound |
118
|
|
|
*/ |
119
|
1 |
|
public function __call($name, $arguments) |
120
|
|
|
{ |
121
|
1 |
|
if (count($arguments)) { |
122
|
1 |
|
$this->set($arguments[0], $name); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set view variable. |
130
|
|
|
* |
131
|
|
|
* Passing an array as $value will add array key => values into current |
132
|
|
|
* view data collection. If $key is passed then an array variable with this |
133
|
|
|
* key will be added to view data collection beside adding array key => values. |
134
|
|
|
* |
135
|
|
|
* @param mixed $value Variable value |
136
|
|
|
* @param string|null $key Variable key\prefix for objects and arrays |
137
|
|
|
* |
138
|
|
|
* @return $this Chaining |
139
|
|
|
*/ |
140
|
5 |
|
public function set($value, $key = null) |
141
|
|
|
{ |
142
|
|
|
// RenderInterface implementation |
143
|
5 |
|
if (is_object($value) && is_a($value, 'samsonframework\core\RenderInterface')) { |
144
|
3 |
|
$this->setRenderableObject($value, $key); |
145
|
5 |
|
} elseif (is_array($value)) { // Merge array into view data |
146
|
2 |
|
$this->data = array_merge($this->data, $value); |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
// Store key value |
150
|
5 |
|
$this->data[$key] = $value; |
151
|
|
|
|
152
|
5 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set renderable object as view variable. |
157
|
|
|
* |
158
|
|
|
* @param mixed $object Object instance for rendering |
159
|
|
|
* @param string|null $key Variable key\prefix for objects and arrays |
160
|
|
|
*/ |
161
|
3 |
|
protected function setRenderableObject($object, $key) |
162
|
|
|
{ |
163
|
|
|
/** @var RenderInterface $object */ |
164
|
|
|
// Generate objects view array data and merge it with view data |
165
|
3 |
|
$this->data = array_merge( |
166
|
3 |
|
$this->data, |
167
|
3 |
|
$object->toView(null !== $key ? $key : get_class($object)) |
168
|
3 |
|
); |
169
|
3 |
|
} |
170
|
|
|
} |
171
|
|
|
|