|
1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Loads and displays Kohana view files. Can also handle output of some binary |
|
4
|
|
|
* files, such as image, Javascript, and CSS files. |
|
5
|
|
|
* |
|
6
|
|
|
* $Id: View.php 4072 2009-03-13 17:20:38Z jheathco $ |
|
7
|
|
|
* |
|
8
|
|
|
* @package Core |
|
9
|
|
|
* @author Kohana Team |
|
10
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
|
11
|
|
|
* @license http://kohanaphp.com/license.html |
|
12
|
|
|
*/ |
|
13
|
|
|
class View_Core |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
// The view file name and type |
|
17
|
|
|
protected $kohana_filename = false; |
|
18
|
|
|
protected $kohana_filetype = false; |
|
19
|
|
|
|
|
20
|
|
|
// View variable storage |
|
21
|
|
|
protected $kohana_local_data = array(); |
|
22
|
|
|
protected static $kohana_global_data = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Creates a new View using the given parameters. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string view name |
|
28
|
|
|
* @param array pre-load data |
|
29
|
|
|
* @param string type of file: html, css, js, etc. |
|
30
|
|
|
* @param string $name |
|
|
|
|
|
|
31
|
|
|
* @return View |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function factory($name = null, $data = null, $type = null) |
|
34
|
|
|
{ |
|
35
|
|
|
return new View($name, $data, $type); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Attempts to load a view and pre-load view data. |
|
40
|
|
|
* |
|
41
|
|
|
* @throws Kohana_Exception if the requested view cannot be found |
|
42
|
|
|
* @param string view name |
|
43
|
|
|
* @param array pre-load data |
|
44
|
|
|
* @param string type of file: html, css, js, etc. |
|
45
|
|
|
* @return void |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($name = null, $data = null, $type = null) |
|
48
|
|
|
{ |
|
49
|
|
|
if (is_string($name) and $name !== '') { |
|
50
|
|
|
// Set the filename |
|
51
|
|
|
$this->set_filename($name, $type); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (is_array($data) and ! empty($data)) { |
|
55
|
|
|
// Preload data using array_merge, to allow user extensions |
|
56
|
|
|
$this->kohana_local_data = array_merge($this->kohana_local_data, $data); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Magic method access to test for view property |
|
62
|
|
|
* |
|
63
|
|
|
* @param string View property to test for |
|
64
|
|
|
* @return boolean |
|
|
|
|
|
|
65
|
|
|
*/ |
|
66
|
|
|
public function __isset($key = null) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->is_set($key); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets the view filename. |
|
73
|
|
|
* |
|
74
|
|
|
* @chainable |
|
75
|
|
|
* @param string view filename |
|
76
|
|
|
* @param string view file type |
|
77
|
|
|
* @param string $name |
|
78
|
|
|
* @return View_Core |
|
79
|
|
|
*/ |
|
80
|
|
|
public function set_filename($name, $type = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($type == null) { |
|
83
|
|
|
// Load the filename and set the content type |
|
84
|
|
|
$this->kohana_filename = Kohana::find_file('views', $name, true); |
|
|
|
|
|
|
85
|
|
|
$this->kohana_filetype = EXT; |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
// Check if the filetype is allowed by the configuration |
|
88
|
|
|
if (! in_array($type, Kohana::config('view.allowed_filetypes'))) { |
|
89
|
|
|
throw new Kohana_Exception('core.invalid_filetype', $type); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Load the filename and set the content type |
|
93
|
|
|
$this->kohana_filename = Kohana::find_file('views', $name, true, $type); |
|
|
|
|
|
|
94
|
|
|
$this->kohana_filetype = Kohana::config('mimes.'.$type); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
if ($this->kohana_filetype == null) { |
|
97
|
|
|
// Use the specified type |
|
98
|
|
|
$this->kohana_filetype = $type; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Sets a view variable. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string|array name of variable or an array of variables |
|
109
|
|
|
* @param mixed value when using a named variable |
|
110
|
|
|
* @return View_Core |
|
111
|
|
|
*/ |
|
112
|
|
|
public function set($name, $value = null) |
|
113
|
|
|
{ |
|
114
|
|
|
if (is_array($name)) { |
|
115
|
|
|
foreach ($name as $key => $value) { |
|
116
|
|
|
$this->__set($key, $value); |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
|
|
$this->__set($name, $value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Checks for a property existence in the view locally or globally. Unlike the built in __isset(), |
|
127
|
|
|
* this method can take an array of properties to test simultaneously. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $key property name to test for |
|
|
|
|
|
|
130
|
|
|
* @param array $key array of property names to test for |
|
131
|
|
|
* @return boolean property test result |
|
132
|
|
|
* @return array associative array of keys and boolean test result |
|
|
|
|
|
|
133
|
|
|
*/ |
|
134
|
|
|
public function is_set($key = false) |
|
135
|
|
|
{ |
|
136
|
|
|
// Setup result; |
|
137
|
|
|
$result = false; |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
// If key is an array |
|
140
|
|
|
if (is_array($key)) { |
|
141
|
|
|
// Set the result to an array |
|
142
|
|
|
$result = array(); |
|
143
|
|
|
|
|
144
|
|
|
// Foreach key |
|
145
|
|
|
foreach ($key as $property) { |
|
146
|
|
|
// Set the result to an associative array |
|
147
|
|
|
$result[$property] = (array_key_exists($property, $this->kohana_local_data) or array_key_exists($property, View::$kohana_global_data)) ? true : false; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
// Otherwise just check one property |
|
151
|
|
|
$result = (array_key_exists($key, $this->kohana_local_data) or array_key_exists($key, View::$kohana_global_data)) ? true : false; |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// Return the result |
|
155
|
|
|
return $result; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Sets a bound variable by reference. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string name of variable |
|
162
|
|
|
* @param mixed variable to assign by reference |
|
163
|
|
|
* @param string $name |
|
164
|
|
|
* @return View_Core |
|
165
|
|
|
*/ |
|
166
|
|
|
public function bind($name, & $var) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->kohana_local_data[$name] =& $var; |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Sets a view global variable. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string|array name of variable or an array of variables |
|
177
|
|
|
* @param mixed value when using a named variable |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function set_global($name, $value = null) |
|
181
|
|
|
{ |
|
182
|
|
|
if (is_array($name)) { |
|
183
|
|
|
foreach ($name as $key => $value) { |
|
184
|
|
|
View::$kohana_global_data[$key] = $value; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
} else { |
|
187
|
|
|
View::$kohana_global_data[$name] = $value; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Magically sets a view variable. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string variable key |
|
195
|
|
|
* @param string variable value |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
public function __set($key, $value) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->kohana_local_data[$key] = $value; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Magically gets a view variable. |
|
205
|
|
|
* |
|
206
|
|
|
* @param string variable key |
|
207
|
|
|
* @return mixed variable value if the key is found |
|
208
|
|
|
* @return void if the key is not found |
|
209
|
|
|
*/ |
|
210
|
|
|
public function &__get($key) |
|
211
|
|
|
{ |
|
212
|
|
|
if (isset($this->kohana_local_data[$key])) { |
|
213
|
|
|
return $this->kohana_local_data[$key]; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if (isset(View::$kohana_global_data[$key])) { |
|
|
|
|
|
|
217
|
|
|
return View::$kohana_global_data[$key]; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (isset($this->$key)) { |
|
221
|
|
|
return $this->$key; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Magically converts view object to string. |
|
227
|
|
|
* |
|
228
|
|
|
* @return string |
|
229
|
|
|
*/ |
|
230
|
|
|
public function __toString() |
|
231
|
|
|
{ |
|
232
|
|
|
try { |
|
233
|
|
|
return $this->render(); |
|
234
|
|
|
} catch (Exception $e) { |
|
235
|
|
|
// Display the exception using its internal __toString method |
|
236
|
|
|
return (string) $e; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Renders a view. |
|
242
|
|
|
* |
|
243
|
|
|
* @param boolean set to TRUE to echo the output instead of returning it |
|
244
|
|
|
* @param callback special renderer to pass the output through |
|
245
|
|
|
* @return string if print is FALSE |
|
246
|
|
|
* @return void if print is TRUE |
|
247
|
|
|
*/ |
|
248
|
|
|
public function render($print = false, $renderer = false) |
|
249
|
|
|
{ |
|
250
|
|
|
if (empty($this->kohana_filename)) { |
|
251
|
|
|
throw new Kohana_Exception('core.view_set_filename'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
if (is_string($this->kohana_filetype)) { |
|
255
|
|
|
// Merge global and local data, local overrides global with the same name |
|
256
|
|
|
$data = array_merge(View::$kohana_global_data, $this->kohana_local_data); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
// Load the view in the controller for access to $this |
|
259
|
|
|
$output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data); |
|
260
|
|
|
|
|
261
|
|
|
if ($renderer !== false and is_callable($renderer, true)) { |
|
262
|
|
|
// Pass the output through the user defined renderer |
|
263
|
|
|
$output = call_user_func($renderer, $output); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
if ($print === true) { |
|
267
|
|
|
// Display the output |
|
268
|
|
|
echo $output; |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
} else { |
|
272
|
|
|
// Set the content type and size |
|
273
|
|
|
header('Content-Type: '.$this->kohana_filetype[0]); |
|
274
|
|
|
|
|
275
|
|
|
if ($print === true) { |
|
276
|
|
|
if ($file = fopen($this->kohana_filename, 'rb')) { |
|
277
|
|
|
// Display the output |
|
278
|
|
|
fpassthru($file); |
|
279
|
|
|
fclose($file); |
|
280
|
|
|
} |
|
281
|
|
|
return; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
// Fetch the file contents |
|
285
|
|
|
$output = file_get_contents($this->kohana_filename); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
return $output; |
|
289
|
|
|
} |
|
290
|
|
|
} // End View |
|
291
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.