|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* View |
|
5
|
|
|
* |
|
6
|
|
|
* View template handling class. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class View { |
|
14
|
|
|
use Module; |
|
15
|
|
|
|
|
16
|
|
|
protected static $handler = null; |
|
17
|
|
|
protected $options = [ |
|
18
|
|
|
'template' => '', |
|
19
|
|
|
'data' => [], |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Construct a new view based on the passed template |
|
24
|
|
|
* @param mixed $template The template path or an array of them. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($template){ |
|
27
|
|
|
foreach ((array)$template as $templ){ |
|
28
|
|
|
if (static::$handler->exists($templ)) |
|
29
|
|
|
return $this->options['template'] = $templ; |
|
30
|
|
|
} |
|
31
|
|
|
throw new Exception("[Core.View] Template not found."); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Load a Template Handler |
|
36
|
|
|
* @param class $handler The template handler class instance |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function using(View\Adapter $handler){ |
|
39
|
|
|
static::$handler = $handler; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* View factory method, can optionally pass data to pre-init view |
|
44
|
|
|
* @param string $template The template path |
|
45
|
|
|
* @param array $data The key-value map of data to pass to the view |
|
46
|
|
|
* @return View |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function from($template,$data=null){ |
|
49
|
|
|
$view = new self($template); |
|
50
|
|
|
return $data ? $view->with($data) : $view; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Assigns data to the view |
|
55
|
|
|
* @param array $data The key-value map of data to pass to the view |
|
56
|
|
|
* @return View |
|
57
|
|
|
*/ |
|
58
|
|
|
public function with($data){ |
|
59
|
|
|
if ($data){ |
|
60
|
|
|
$tmp = array_merge($data, (isset($this->options['data'])?$this->options['data']:[])); |
|
61
|
|
|
$this->options['data'] = $tmp; |
|
62
|
|
|
} |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Render view when casted to a string |
|
68
|
|
|
* @return string The rendered view |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __toString(){ |
|
71
|
|
|
return Filter::with('core.view',static::$handler->render($this->options['template'],$this->options['data'])); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the handler instance |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function & handler(){ |
|
79
|
|
|
return static::$handler; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if a template exists |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function exists($templatePath){ |
|
87
|
|
|
return static::$handler->exists($templatePath); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Propagate the call to the handler |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __call($n,$p){ |
|
95
|
|
|
return call_user_func_array([static::$handler,$n],$p); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Propagate the static call to the handler |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function __callStatic($n,$p){ |
|
102
|
|
|
return forward_static_call_array([static::$handler,$n],$p); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|