|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf; |
|
10
|
|
|
|
|
11
|
|
|
use Pimf\Contracts\Renderable; |
|
12
|
|
|
use Pimf\Util\File; |
|
13
|
|
|
use Pimf\Contracts\Arrayable; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* A simply view for sending and rendering data. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Pimf |
|
19
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class View implements Renderable |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Name of the template. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $template; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Contains the variables that are to be embedded in the template. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \ArrayObject |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $data; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Path to templates - is framework restriction. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $path; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $template |
|
44
|
|
|
* @param array $data |
|
45
|
|
|
* @param string $path Path to templates if you do not want to use PIMF framework restriction. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($template = 'default.phtml', array $data = array(), $path = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->data = new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS); |
|
50
|
|
|
$this->path = (!$path) ? BASE_PATH . 'app/' . Config::get('app.name') . '/_templates' : $path; |
|
51
|
|
|
$this->template = (string)$template; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $template |
|
56
|
|
|
* |
|
57
|
|
|
* @return View |
|
58
|
|
|
*/ |
|
59
|
|
|
public function produce($template) |
|
60
|
|
|
{ |
|
61
|
|
|
$view = clone $this; |
|
62
|
|
|
$view->template = (string)$template; |
|
63
|
|
|
|
|
64
|
|
|
return $view; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $template |
|
69
|
|
|
* @param array|Arrayable $model |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function partial($template, $model = array()) |
|
74
|
|
|
{ |
|
75
|
|
|
$model = ($model instanceof Arrayable) ? $model->toArray() : $model; |
|
76
|
|
|
|
|
77
|
|
|
return $this->produce($template)->pump($model)->render(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $template |
|
82
|
|
|
* @param array $model |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function loop($template, array $model = array()) |
|
87
|
|
|
{ |
|
88
|
|
|
$out = ''; |
|
89
|
|
|
|
|
90
|
|
|
foreach ($model as $row) { |
|
91
|
|
|
$out .= $this->partial($template, $row); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $out; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Assigns a variable to a specific key for the template. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $key The key. |
|
101
|
|
|
* @param mixed $value The Value. |
|
102
|
|
|
* |
|
103
|
|
|
* @return View |
|
104
|
|
|
*/ |
|
105
|
|
|
public function assign($key, $value) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->data[$key] = $value; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Exchange all variables. |
|
114
|
|
|
* |
|
115
|
|
|
* @param $model |
|
116
|
|
|
* |
|
117
|
|
|
* @return View |
|
118
|
|
|
*/ |
|
119
|
|
|
public function pump($model) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->data->exchangeArray((array)$model); |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $name |
|
128
|
|
|
* |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
* @throws \OutOfBoundsException If undefined property at the template. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function __get($name) |
|
133
|
|
|
{ |
|
134
|
|
|
if ($this->data->offsetExists($name)) { |
|
135
|
|
|
return $this->data->offsetGet($name); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$trace = debug_backtrace(); |
|
139
|
|
|
throw new \OutOfBoundsException( |
|
140
|
|
|
'undefined property "' . $name . '" at file ' . $trace[0]['file'] . ' line ' . $trace[0]['line'] |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function render() |
|
148
|
|
|
{ |
|
149
|
|
|
$level = ob_get_level(); |
|
|
|
|
|
|
150
|
|
|
ob_start(); |
|
151
|
|
|
|
|
152
|
|
|
try { |
|
153
|
|
|
|
|
154
|
|
|
echo $this->reunite(); |
|
155
|
|
|
|
|
156
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
while (ob_get_level() > $level) { |
|
159
|
|
|
ob_end_clean(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
trigger_error($exception->getMessage(), E_USER_NOTICE); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return ob_get_clean(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Puts the template an the variables together. |
|
170
|
|
|
*/ |
|
171
|
|
|
public function reunite() |
|
172
|
|
|
{ |
|
173
|
|
|
include new File(str_replace('/', DS, $this->path . '/' . $this->template)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Act when the view is treated like a string |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function __toString() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->render(); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.