|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Mvc; |
|
12
|
|
|
|
|
13
|
|
|
use Drone\Mvc\AbstractionController; |
|
14
|
|
|
use Drone\Mvc\Exception; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Layout class |
|
18
|
|
|
* |
|
19
|
|
|
* This class manages templates from views |
|
20
|
|
|
*/ |
|
21
|
|
|
class Layout |
|
22
|
|
|
{ |
|
23
|
|
|
use \Drone\Util\ParamTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Controller instance |
|
27
|
|
|
* |
|
28
|
|
|
* @var AbstractionController |
|
29
|
|
|
*/ |
|
30
|
|
|
private $controller; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* View path |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $view; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Document title |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $title; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Document description |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $description; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Document image |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $image; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Base path |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
private $basePath; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the instance of current controller |
|
69
|
|
|
* |
|
70
|
|
|
* @return AbstractionController |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getController() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->controller; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the view |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getView() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->view; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns the document title |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getTitle() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->title; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the document description |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getDescription() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->description; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the document image |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getImage() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->image; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets the document title |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $title |
|
121
|
|
|
* |
|
122
|
|
|
* @return null |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setTitle($title) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->title = $title; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sets the document description |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $description |
|
133
|
|
|
* |
|
134
|
|
|
* @return null |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setDescription($description) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->description = $description; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Sets the document image |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $image |
|
145
|
|
|
* |
|
146
|
|
|
* @return null |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setImage($image) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->image = $image; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets the view |
|
155
|
|
|
* |
|
156
|
|
|
* @param AbstractionModule $module |
|
157
|
|
|
* @param string $view |
|
158
|
|
|
* |
|
159
|
|
|
* @return null |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setView($module, $view) |
|
162
|
|
|
{ |
|
163
|
|
|
$config = $module->getConfig(); |
|
164
|
|
|
|
|
165
|
|
|
if (!array_key_exists($view, $config["view_manager"]["view_map"]) || !file_exists($config["view_manager"]["view_map"][$view])) |
|
166
|
|
|
throw new Exception\ViewNotFoundException("The 'view' template " . $view . " does not exists"); |
|
167
|
|
|
|
|
168
|
|
|
$this->view = $config["view_manager"]["view_map"][$view]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Sets the base path |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $path |
|
175
|
|
|
* |
|
176
|
|
|
* @return null |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setBasePath($path) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->basePath = $path; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Constructor |
|
185
|
|
|
* |
|
186
|
|
|
* All modifiable attributes (i.e. with setter method) can be passed as key |
|
187
|
|
|
* |
|
188
|
|
|
* @param array $params |
|
189
|
|
|
* |
|
190
|
|
|
* @throws Exception\PageNotFoundException |
|
191
|
|
|
*/ |
|
192
|
|
|
public function __construct(Array $params = []) |
|
193
|
|
|
{ |
|
194
|
|
|
foreach ($params as $param => $value) |
|
195
|
|
|
{ |
|
196
|
|
|
if (property_exists(__CLASS__, strtolower($param)) && method_exists($this, 'set'.$param)) |
|
197
|
|
|
$this->{'set'.$param}($value); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Loads a view from a controller |
|
203
|
|
|
* |
|
204
|
|
|
* @throws Exception\PageNotFoundException |
|
205
|
|
|
* |
|
206
|
|
|
* @param AbstractionController |
|
207
|
|
|
*/ |
|
208
|
|
|
public function fromController(AbstractionController $controller) |
|
209
|
|
|
{ |
|
210
|
|
|
// str_replace() is needed in linux systems |
|
211
|
|
|
$this->setParams($controller->getParams()); |
|
212
|
|
|
$this->basePath = $controller->getBasePath(); |
|
213
|
|
|
$this->controller = $controller; |
|
214
|
|
|
|
|
215
|
|
|
if ($controller->getShowView()) |
|
216
|
|
|
{ |
|
217
|
|
|
$view = 'module/' . $controller->getModule()->getModuleName() . |
|
218
|
|
|
'/source/view/'. basename(str_replace('\\','/',get_class($controller))) . |
|
219
|
|
|
'/' . $controller->getMethod() . '.phtml'; |
|
220
|
|
|
|
|
221
|
|
|
$this->view = $view; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
if ($controller->getTerminal()) |
|
225
|
|
|
{ |
|
226
|
|
|
if (file_exists($view)) |
|
|
|
|
|
|
227
|
|
|
include $view; |
|
228
|
|
|
} |
|
229
|
|
|
else |
|
230
|
|
|
{ |
|
231
|
|
|
if (!is_null($this->view) && !file_exists($this->view)) |
|
|
|
|
|
|
232
|
|
|
throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists"); |
|
233
|
|
|
|
|
234
|
|
|
$config = $controller->getModule()->getConfig(); |
|
235
|
|
|
|
|
236
|
|
|
if (!array_key_exists($controller->getLayout(), $config["view_manager"]["template_map"])) |
|
237
|
|
|
throw new Exception\PageNotFoundException("The 'template' " . $template . " was not defined in module.config.php"); |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
$template = $config["view_manager"]["template_map"][$controller->getLayout()]; |
|
240
|
|
|
|
|
241
|
|
|
if (!file_exists($template)) |
|
242
|
|
|
throw new Exception\PageNotFoundException("The 'template' " . $template . " does not exists"); |
|
243
|
|
|
|
|
244
|
|
|
include $template; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Loads a view from a template file |
|
250
|
|
|
* |
|
251
|
|
|
* @throws Exception\PageNotFoundException |
|
252
|
|
|
* |
|
253
|
|
|
* @param AbstractionModule $module |
|
254
|
|
|
* @param string $template |
|
255
|
|
|
*/ |
|
256
|
|
|
public function fromTemplate($module, $template) |
|
257
|
|
|
{ |
|
258
|
|
|
$config = $module->getConfig(); |
|
259
|
|
|
include $config["view_manager"]["template_map"][$template]; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Includes the file view |
|
264
|
|
|
* |
|
265
|
|
|
* @return null |
|
266
|
|
|
*/ |
|
267
|
|
|
public function content() |
|
268
|
|
|
{ |
|
269
|
|
|
if (!file_exists($this->view)) |
|
270
|
|
|
throw new Exception\ViewNotFoundException("The 'view' template " . $this->view . " does not exists"); |
|
271
|
|
|
|
|
272
|
|
|
include $this->view; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Returns the base path of the application |
|
277
|
|
|
* |
|
278
|
|
|
* @return string |
|
279
|
|
|
*/ |
|
280
|
|
|
public function basePath() |
|
281
|
|
|
{ |
|
282
|
|
|
return $this->basePath; |
|
283
|
|
|
} |
|
284
|
|
|
} |