1 | <?php |
||
11 | class View |
||
12 | { |
||
13 | /** |
||
14 | * Active layout for view |
||
15 | * |
||
16 | * @var string|bool |
||
17 | */ |
||
18 | protected $layout; |
||
19 | |||
20 | private $pathToView; |
||
21 | |||
22 | private $pathToLayout; |
||
23 | |||
24 | /** @var array */ |
||
25 | protected $viewQueue = []; |
||
26 | |||
27 | /** |
||
28 | * Rendered view content |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $view = false; |
||
33 | |||
34 | /** |
||
35 | * Data object for view |
||
36 | * |
||
37 | * @var object |
||
38 | */ |
||
39 | protected $properties; |
||
40 | |||
41 | /** |
||
42 | * \Zewa\Config reference |
||
43 | * |
||
44 | * @var Config |
||
45 | */ |
||
46 | protected $configuration; |
||
47 | |||
48 | /** |
||
49 | * \Zewa\Router reference |
||
50 | * |
||
51 | * @var Router |
||
52 | */ |
||
53 | protected $router; |
||
54 | |||
55 | /** |
||
56 | * \Zewa\Router reference |
||
57 | * |
||
58 | * @var Router |
||
59 | */ |
||
60 | protected $request; |
||
61 | |||
62 | /** @var Container */ |
||
63 | protected $container; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $queuedJS = []; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $queuedCSS = []; |
||
74 | |||
75 | /** |
||
76 | * Load up some basic configuration settings. |
||
77 | */ |
||
78 | 11 | public function __construct(Config $config, Router $router, Request $request, Container $container) |
|
88 | |||
89 | /* |
||
90 | * @todo create method for returning |
||
91 | * a valid json string with header.. |
||
92 | * view shouldn't set header logic, |
||
93 | * and the framework doesn't care what returns the string |
||
94 | * ..but view should handle the json_encode... |
||
95 | * seems overkill to call header() with returning a $view->json; |
||
96 | * thoughts?*/ |
||
97 | |||
98 | /** |
||
99 | * Loads a view |
||
100 | * |
||
101 | * @access public |
||
102 | * @param string|bool $view view to load |
||
103 | * @param string|bool $layout |
||
104 | * @return string |
||
105 | */ |
||
106 | 6 | public function render($view = false, $layout = false) |
|
107 | { |
||
108 | 6 | if ($layout !== false) { |
|
109 | 2 | $this->setLayout($layout); |
|
110 | } |
||
111 | |||
112 | 5 | if ($view !== false) { |
|
113 | 3 | $this->setView($view); |
|
114 | } |
||
115 | |||
116 | 4 | return $this->bufferResponse(); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * formats and prepares view for inclusion |
||
121 | * @param $viewName |
||
122 | * @return string |
||
123 | * @throws Exception\LookupException |
||
124 | */ |
||
125 | 5 | public function setView($viewName) |
|
135 | |||
136 | 1 | public function getView($view = null) |
|
144 | |||
145 | 2 | public function setProperty($value, $primitive = null) |
|
146 | { |
||
147 | 2 | $container = $this->container->has('view_properties') ? $this->container->get('view_properties') : []; |
|
148 | |||
149 | 2 | if ($primitive === null) { |
|
150 | 1 | $container = $value; |
|
151 | } else { |
||
152 | 2 | $container[$value] = $primitive; |
|
153 | } |
||
154 | |||
155 | 2 | $this->container->set('view_properties', $container); |
|
156 | 2 | } |
|
157 | |||
158 | 1 | public function getProperty(string $key = null, $default = null) |
|
168 | |||
169 | 1 | public function unsetProperty(string $key) |
|
177 | |||
178 | 5 | public function setLayout($layout = null) |
|
179 | { |
||
180 | 5 | if ($layout === null) { |
|
181 | 1 | $this->layout = null; |
|
182 | 1 | return; |
|
183 | } |
||
184 | |||
185 | 5 | $layout = $this->pathToLayout . DIRECTORY_SEPARATOR . strtolower($layout) . '.php'; |
|
186 | |||
187 | 5 | if (!file_exists($layout)) { |
|
188 | 1 | throw new Exception\LookupException('Layout: "' . $layout . '" could not be found.'); |
|
189 | } |
||
190 | |||
191 | 4 | $this->layout = $layout; |
|
192 | |||
193 | 4 | return; |
|
194 | } |
||
195 | |||
196 | 1 | public function getLayout() |
|
200 | |||
201 | 4 | private function renderViews() : string |
|
212 | |||
213 | 4 | private function bufferResponse() : string |
|
223 | |||
224 | 4 | private function buffer(string $path) |
|
242 | |||
243 | /** |
||
244 | * Helper method for grabbing aggregated css files |
||
245 | * |
||
246 | * @access protected |
||
247 | * @return string css includes |
||
248 | */ |
||
249 | 2 | public function fetchCSS() |
|
263 | |||
264 | /** |
||
265 | * Helper method for grabbing aggregated JS files |
||
266 | * |
||
267 | * @access protected |
||
268 | * @return string JS includes |
||
269 | */ |
||
270 | 2 | public function fetchJS() |
|
284 | |||
285 | /** |
||
286 | * Helper method for adding css files for aggregation/render |
||
287 | * |
||
288 | * @access public |
||
289 | * @param $files array |
||
290 | */ |
||
291 | 1 | public function addCSS($files = []) |
|
292 | { |
||
293 | 1 | $this->queuedCSS = array_merge($files, $this->queuedCSS); |
|
294 | 1 | } |
|
295 | |||
296 | 1 | public function addJS($files = []) |
|
300 | |||
301 | /** |
||
302 | * Set 404 header, and return 404 view contents |
||
303 | * |
||
304 | * @access public |
||
305 | * @return string |
||
306 | */ |
||
307 | 1 | public function render404() |
|
313 | } |
||
314 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..