1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Air\View; |
4
|
|
|
|
5
|
|
|
class View implements ViewInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string $file The file to load. |
9
|
|
|
*/ |
10
|
|
|
protected $file; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array $data The data to inject. |
15
|
|
|
*/ |
16
|
|
|
protected $data = []; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var RendererInterface $renderer The rendering engine. |
21
|
|
|
*/ |
22
|
|
|
protected $renderer; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RendererInterface $renderer The rendering engine. |
27
|
|
|
* @param string $file The file to load. |
28
|
|
|
* @param array $data The data to inject. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(RendererInterface $renderer, $file, array $data = []) |
31
|
|
|
{ |
32
|
|
|
$this->renderer = $renderer; |
33
|
|
|
$this->file = $file; |
34
|
|
|
$this->data = $data; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Binds data to the view. |
40
|
|
|
* |
41
|
|
|
* @param string $key The key. |
42
|
|
|
* @param string $value The value. |
43
|
|
|
*/ |
44
|
|
|
public function __set($key, $value) |
45
|
|
|
{ |
46
|
|
|
$this->set($key, $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the view data |
52
|
|
|
* |
53
|
|
|
* @param string $key The key. |
54
|
|
|
* @return mixed the view data. |
55
|
|
|
*/ |
56
|
|
|
public function __get($key) |
57
|
|
|
{ |
58
|
|
|
return $this->get($key); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Binds data to the view. |
64
|
|
|
* |
65
|
|
|
* @param string $key The key. |
66
|
|
|
* @param mixed $value The value. |
67
|
|
|
*/ |
68
|
|
|
public function set($key, $value) |
69
|
|
|
{ |
70
|
|
|
$this->data[$key] = $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets the view data. |
76
|
|
|
* |
77
|
|
|
* @param string $key The key. |
78
|
|
|
* @return mixed the view data. |
79
|
|
|
*/ |
80
|
|
|
public function get($key) |
81
|
|
|
{ |
82
|
|
|
return $this->data[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Renders the view as a string. |
88
|
|
|
* |
89
|
|
|
* @return string The rendered view. |
90
|
|
|
*/ |
91
|
|
|
public function __toString() |
92
|
|
|
{ |
93
|
|
|
return $this->render(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Renders the view. |
99
|
|
|
* |
100
|
|
|
* @throws \Exception |
101
|
|
|
* @return string The rendered output. |
102
|
|
|
*/ |
103
|
|
|
public function render() |
104
|
|
|
{ |
105
|
|
|
return $this->renderer->render($this->file, $this->data); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|