forked from cwilson90/LAMP-1---Project-1
/
View.php
511 lines (458 loc) · 14.3 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* McGowan
*
* @version 1.0
*/
namespace McGowan
{
/**
* @ignore
*/
defined('IN_LIBRARY') or exit;
/**
* View
*/
class View extends Object
{
/**
* @var array Static member variable containing global view variables.
*/
protected static $_global_data = array();
/**
* factory
*
* Factory method for creating a new View instance.
*
* @access public
* @param string Contains the absolute file path to the view file.
* @param array Contains an array of data to initialize this view with.
* @return View Returns the newly created instance.
*/
public static function factory($file = '', array $data = array())
{
return new View($file, $data);
}
/**
* set_global
*
* Setter method for adding/setting a new global view variable.
*
* @access public
* @param mixed Contains the name of the variable usable within the view file. If an array, contains an array of
* variables to add to the global view scope.
* @param mixed Contains the value of the variable. Should be null (ignored) if var is an array.
* @return void
*/
public static function set_global($var, $value = null)
{
if( is_array($var) )
{
foreach( $var as $v => $val )
{
self::$_global_data[$v] = $val;
}
}
else
{
self::$_global_data[$var] = $value;
}
}
/**
* get_global
*
* Accessor method for a global view variable.
*
* @access public
* @param string Contains the variable to get.
* @param mixed Contains a default value to return if variable does not exist.
* @return mixed
*/
public static function get_global($var, $default = null)
{
return isset(self::$_global_data[$var]) ? self::$_global_data[$var] : $default;
}
/**
* global_exists
*
* Checks if a global variable exists.
*
* @access public
* @param string Contains the global variable name to check.
* @return bool Returns true if exists, otherwise false.
*/
public static function global_exists($var)
{
return isset(self::$_global_data[$var]);
}
/**
* remove_global
*
* Removes a global view variable.
*
* @access public
* @param string Contains the name of the global view variable to remove.
* @return bool Returns true if removed, otherwise false.
*/
public static function remove_global($var)
{
if( isset(self::$_global_data[$var]) )
{
unset(self::$_global_data[$var]);
return true;
}
return false;
}
/**
* @var string Contains the rendered contents
*/
private $_contents = null;
/**
* @var bool Is true if view has been rendered, otherwise false.
*/
private $_has_rendered = false;
/**
* @var bool Is true if view is currently being rendered, otherwise false.
*/
private $_is_rendering = false;
/**
* @var string Contains the view template file path.
*/
protected $_tmpl_path;
/**
* @var string Contains the view template file name.
*/
protected $_tmpl_file;
/**
* @var array Contains the variables for this view.
*/
protected $_data = array();
/**
* __construct
*
* Creates a new instance of the View class.
*
* @access public
* @param string Contains the absolute file path to the view template file.
* @param array Contains an array of variables to pre-populate view with.
*/
public function __construct($file = '', array $data = null)
{
if( !empty($file) )
{
$this->template_path(dirname($file));
$this->template_file(basename($file));
}
if( !empty($data) )
{
$this->set($data);
}
}
/**
* set
*
* Setter for a view instance variable.
*
* @access public
* @param string Contains the view variable name. May contain an array of view variables to assign.
* @param mixed Contains the view variable value. If $var is array, this is ignored.
* @return void
*/
public function set($var, $value = null)
{
if( is_array($var) )
{
foreach( $var as $v => $val )
{
$this->_data[$v] = $val;
}
}
else
{
$this->_data[$var] = $value;
}
}
/**
* get
*
* Accessor method for a view instance variable.
*
* @access public
* @param string Contains the view variable name to get.
* @param mixed Contains a default value to return if variable does not exist.
* @return mixed
*/
public function get($var, $default = null)
{
return isset($this->_data[$var]) ? $this->_data[$var] : $default;
}
/**
* exists
*
* Checks if a view instance variable exists.
*
* @access public
* @param string Contains the view variable name to get.
* @return bool Returns true if exists, otherwise false.
*/
public function exists($var)
{
return isset($this->_data[$var]);
}
/**
* remove
*
* Removes a view instance variable.
*
* @access public
* @param string Contains the name of the variable to remove.
* @return bool Returns true if successful, else false.
*/
public function remove($var)
{
if( isset($this->_data[$var]) )
{
unset($this->_data[$var]);
return true;
}
return false;
}
/**
* has_rendered
*
* Checks if this view instance has already been previously rendered.
*
* @access public
* @param void
* @return bool
*/
final public function has_rendered()
{
return $this->_has_rendered;
}
/**
* is_rendering
*
* Checks if this view instance is currently being rendered.
*
* @access public
* @param void
* @return bool
*/
final public function is_rendering()
{
return $this->_is_rendering;
}
/**
* template_path
*
* Accessor/setter for the template view file path.
*
* @access public
* @param string Contains the view file path to set for this instance. If null, method acts as a getter.
* @return mixed Returns the set path if acting as a getter, otherwise this instance.
*/
final public function template_path($path = null)
{
if( null === $path ) {
return $this->_tmpl_path;
}
$this->_tmpl_path = $path;
$this->_tmpl_path = rtrim($this->_tmpl_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return $this;
}
/**
* template_file
*
* Accessor/setter for the template view file name.
*
* @access public
* @param string Contains the view file name to set for this instance. If null, method acts as a getter.
* @return mixed Returns the set name if acting as a getter, otherwise this instance.
*/
final public function template_file($file = null)
{
if( null === $file )
{
return $this->_tmpl_file;
}
$this->_tmpl_file = $file;
return $this;
}
/**
* contents
*
* Accessor for the rendered contents of this view instance.
*
* @access public
* @param bool If true, will render if contents are empty.
* @return string Returns the rendered contents.
*/
final public function contents($render = false) {
if( $render && !$this->_contents )
{
$this->render(true);
}
return $this->_contents;
}
/**
* render
*
* Renders this view instance.
*
* @access public
* @param bool Used to determine if the contents should be returned. If true, contents are returned.
* @return mixed
*/
final public function render($return = true)
{
/* Has the view been rendered? If so, handle the return or print of them */
if( $this->has_rendered() )
{
if( !$return )
{
print $this->_contents;
return;
}
return $this->_contents;
}
/* Do not allow for recursive rendering of this view instance */
if( $this->is_rendering() )
{
throw new \Exception('View is currently being rendered and you are not allowed to call View::render() in a recursive manner.');
}
$this->_is_rendering = true;
$this->_data['view'] = $this;
/* Run the beforeRender hook */
$this->beforeRender();
try
{
/* Render the view file within the static render_view method */
$this->_contents = self::render_view($this->compile_path(), $this->_data);
}
catch( \Exception $exception )
{
$this->_has_rendered = true;
$this->_is_rendering = false;
if( !$return )
{
throw $exception;
}
$this->afterRender(true, $exception);
return (string) $exception;
}
/* Run the afterRender hook */
$this->afterRender();
$this->_has_rendered = true;
$this->_is_rendering = false;
if( !$return )
{
print $this->_contents;
return;
}
return $this->_contents;
}
/**
* @see Object::tostring
*/
final public function toString()
{
return $this->contents(true);
}
/**
* __set
*
* @see http://www.php.net/manual/en/language.oop5.overloading.php#object.set
*/
final public function __set($key, $value)
{
$this->set($key, $value);
}
/**
* __get
*
* @see http://www.php.net/manual/en/language.oop5.overloading.php#object.get
*/
final public function __get($key)
{
return $this->get($key);
}
/**
* __isset
*
* @see http://www.php.net/manual/en/language.oop5.overloading.php#object.isset
*/
final public function __isset($key)
{
return $this->exists($key);
}
/**
* __unset
*
* @see http://www.php.net/manual/en/language.oop5.overloading.php#object.unset
*/
final public function __unset($key)
{
return $this->remove($key);
}
/**
* beforeRender
*
* @access protected
* @return void
*/
protected function beforeRender()
{
/* void */
}
/**
* afterRender
*
* @access protected
* @param bool If true, during rendering an exception was caught.
* @param \Exception Contains an instance of the exception caught during rendering, otherwise null.
* @return void
*/
protected function afterRender($has_exception = false, \Exception $exception = null)
{
/* void */
}
/**
* compile_path
*
* @access protected
* @return string Contains the compiled path ready to use for rendering
*/
protected function compile_path()
{
return $this->_tmpl_path . $this->_tmpl_file;
}
/**
* render_view
*
* Handles the rendering of the view file.
*
* @access private
* @param string Contains the absolute view file path that should be rendered.
* @param array Contains the instance view variables for this view that should be made available.
* @return string Returns the rendered view file.
* @throws \Exception Throws any exception caught.
*/
final private static function render_view($view_filename, array $view_data)
{
extract($view_data, EXTR_SKIP);
extract(self::$_global_data, EXTR_SKIP | EXTR_REFS);
ob_start();
try
{
include $view_filename;
}
catch( \Exception $exception )
{
ob_end_clean();
throw $exception;
}
return ob_get_clean();
}
}
}