Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
61 / 61 |
|
100.00% |
19 / 19 |
CRAP | |
100.00% |
1 / 1 |
View | |
100.00% |
61 / 61 |
|
100.00% |
19 / 19 |
32 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addScript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
scripts | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
styles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
useLayout | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
layout | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
block | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
startBlock | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
endBlock | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addFolder | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
folder | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setTheme | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
theme | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fetch | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
getRealPath | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
includeFunctions | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | namespace Dynart\Micro; |
4 | |
5 | /** |
6 | * Template processor |
7 | * |
8 | * This class is used for server side HTML rendering with the help of PHP. |
9 | * |
10 | * @package Dynart\Micro |
11 | */ |
12 | class View { |
13 | |
14 | const CONFIG_DEFAULT_FOLDER = 'view.default_folder'; |
15 | |
16 | /** @var Router */ |
17 | protected $router; |
18 | |
19 | /** @var Config */ |
20 | protected $config; |
21 | |
22 | /** |
23 | * The layout for the currently fetched template |
24 | * @var string |
25 | */ |
26 | protected $layout = ''; |
27 | |
28 | /** |
29 | * The theme path for overriding templates |
30 | * @var string |
31 | */ |
32 | protected $theme = ''; |
33 | |
34 | /** |
35 | * Holds the blocks queue for start/end block functions |
36 | * @var array |
37 | */ |
38 | protected $blockQueue = []; |
39 | |
40 | /** |
41 | * Holds the content of the blocks by name |
42 | * @var array |
43 | */ |
44 | protected $blocks = []; |
45 | |
46 | /** |
47 | * Stores the paths for the folders by namespace |
48 | * @var array |
49 | */ |
50 | protected $folders = []; |
51 | |
52 | /** |
53 | * Holds the view variables |
54 | * @var array |
55 | */ |
56 | protected $data = []; |
57 | |
58 | /** |
59 | * Holds the scripts for the view |
60 | * @var array |
61 | */ |
62 | protected $scripts = []; |
63 | |
64 | /** |
65 | * Holds the styles for the view |
66 | * @var array |
67 | */ |
68 | protected $styles = []; |
69 | |
70 | /** |
71 | * The functions were included? |
72 | * @var bool |
73 | */ |
74 | protected $functionsIncluded = false; |
75 | |
76 | public function __construct(Config $config) { |
77 | $this->config = $config; |
78 | } |
79 | |
80 | /** |
81 | * Returns with a view variable value by name |
82 | * @param string $name The name of the variable |
83 | * @param mixed|null $default The default value if the variable doesn't present |
84 | * @return mixed|null The value of the variable |
85 | */ |
86 | public function get(string $name, $default = null) { |
87 | return isset($this->data[$name]) ? $this->data[$name] : $default; |
88 | } |
89 | |
90 | /** |
91 | * Sets a view variable |
92 | * @param string $name The name of the variable |
93 | * @param mixed $value The value of the variable |
94 | */ |
95 | public function set(string $name, $value): void { |
96 | $this->data[$name] = $value; |
97 | } |
98 | |
99 | /** |
100 | * Adds a script for the view |
101 | * @param string $src |
102 | * @param array $attributes |
103 | */ |
104 | public function addScript(string $src, array $attributes=[]): void { |
105 | $this->scripts[$src] = $attributes; |
106 | } |
107 | |
108 | /** |
109 | * Returns with the scripts in [src => [attributes]] array |
110 | * @return array The scripts array |
111 | */ |
112 | public function scripts(): array { |
113 | return $this->scripts; |
114 | } |
115 | |
116 | /** |
117 | * Adds a style for the view |
118 | * @param string $src |
119 | * @param array $attributes |
120 | */ |
121 | public function addStyle(string $src, array $attributes=[]): void { |
122 | $this->styles[$src] = $attributes; |
123 | } |
124 | |
125 | /** |
126 | * Returns with the styles in [src => [attributes]] array |
127 | * @return array The styles array |
128 | */ |
129 | public function styles(): array { |
130 | return $this->styles; |
131 | } |
132 | |
133 | /** |
134 | * Sets the `$layout` so after the template was rendered it will render this layout. |
135 | * The path can contain a namespace if a folder was added with that namespace, for example: |
136 | * |
137 | * <pre> |
138 | * $view->addFolder('folder', 'views/example'); |
139 | * $view->useLayout('folder:layout'); |
140 | * </pre> |
141 | * |
142 | * will render the 'views/example/layout.phtml'. |
143 | * |
144 | * @param string $path The path for the layout, for example: 'layout' is for 'views/layout.phtml' |
145 | */ |
146 | public function useLayout(string $path): void { |
147 | $this->layout = $path; |
148 | } |
149 | |
150 | /** |
151 | * Returns with the current layout |
152 | * @return string The current layout |
153 | */ |
154 | public function layout(): string { |
155 | return $this->layout; |
156 | } |
157 | |
158 | /** |
159 | * Returns with the content of a block |
160 | * @param string $name |
161 | * @return string |
162 | */ |
163 | public function block(string $name): string { |
164 | return isset($this->blocks[$name]) ? $this->blocks[$name] : ''; |
165 | } |
166 | |
167 | /** |
168 | * Starts a block |
169 | * |
170 | * If the block has content, when the endBlock() will be called the content will be appended to the block. |
171 | * |
172 | * @param string $name The name of the block |
173 | */ |
174 | public function startBlock(string $name): void { |
175 | if (!isset($this->blocks[$name])) { |
176 | $this->blocks[$name] = ''; |
177 | } |
178 | $this->blockQueue[] = $name; |
179 | ob_start(); |
180 | } |
181 | |
182 | /** |
183 | * Ends a block |
184 | * |
185 | * If the block has content, the new content will be appended to the block. |
186 | */ |
187 | public function endBlock(): void { |
188 | $name = array_pop($this->blockQueue); |
189 | $this->blocks[$name] .= ob_get_clean(); |
190 | } |
191 | |
192 | /** |
193 | * Adds a view folder |
194 | * |
195 | * For example, if the `app.root_path` config value is '/var/www/example.com', and you have the following code: |
196 | * |
197 | * <pre> |
198 | * $view->addFolder('folder', '~/views/example'); |
199 | * $view->fetch('folder:index'); |
200 | * </pre> |
201 | * |
202 | * will fetch the '/var/www/example.com/views/example/index.phtml'. |
203 | * |
204 | * The `$path` should NOT end with a '/' |
205 | * |
206 | * @param string $namespace The namespace of the folder |
207 | * @param string $path The path to the folder. It can be an absolute or relative path as well. |
208 | */ |
209 | public function addFolder(string $namespace, string $path): void { |
210 | $this->folders[$namespace] = $path; |
211 | } |
212 | |
213 | /** |
214 | * Returns with the folder path by namespace |
215 | * @param string $namespace |
216 | * @return string The path for the folder |
217 | */ |
218 | public function folder(string $namespace): string { |
219 | return $this->folders[$namespace]; |
220 | } |
221 | |
222 | /** |
223 | * Sets the theme path for overriding templates |
224 | * @param string $path |
225 | */ |
226 | public function setTheme(string $path): void { |
227 | $this->theme = $path; |
228 | } |
229 | |
230 | /** |
231 | * Returns with the theme path |
232 | * @return string |
233 | */ |
234 | public function theme(): string { |
235 | return $this->theme; |
236 | } |
237 | |
238 | /** |
239 | * Fetches a template with variables |
240 | * @param string $__viewPath The path to the view |
241 | * @param array $__vars The variables in [name => value] format |
242 | * @return string The fetched template output in string |
243 | */ |
244 | public function fetch(string $__viewPath, array $__vars=[]): string { |
245 | $this->includeFunctions(); |
246 | $__path = $this->getRealPath($__viewPath).'.phtml'; |
247 | if (!file_exists($__path)) { |
248 | throw new MicroException("Can't find view: $__viewPath, $__path"); |
249 | } |
250 | extract($this->data); |
251 | extract($__vars); |
252 | ob_start(); |
253 | include $__path; |
254 | $result = ob_get_clean(); |
255 | $layout = $this->layout; |
256 | if ($layout) { |
257 | $this->layout = ''; |
258 | $result = $this->fetch($layout, $__vars); |
259 | } |
260 | return $result; |
261 | } |
262 | |
263 | /** |
264 | * Returns with the real path to the template file |
265 | * |
266 | * * If the path doesn't contain a namespace it will use the `view.default_folder` config value to determine the path for the folder. |
267 | * * If the path contains a namespace it will use the folder of the namespace. |
268 | * * If the view has a theme, that path will be checked first, so the theme can override every template. |
269 | * |
270 | * For example if you added a folder with namespace 'folder': |
271 | * |
272 | * <pre> |
273 | * $view->addFolder('folder', '~/views/example'); |
274 | * </pre> |
275 | * |
276 | * and the `app.root_path` config value is '/var/www/example.com' |
277 | * the result will be '/var/www/example.com/views/example/index.phtml' |
278 | * |
279 | * @throws MicroException If the view path has a namespace but a folder wasn't added for it |
280 | * @param string $path The view path |
281 | * @return string The real path to the template file |
282 | */ |
283 | protected function getRealPath(string $path): string { |
284 | $dotPos = strpos($path, ':'); |
285 | if ($dotPos !== false) { |
286 | $namespace = substr($path, 0, $dotPos); |
287 | if (!isset($this->folders[$namespace])) { |
288 | throw new MicroException("Folder wasn't added with namespace: $namespace"); |
289 | } |
290 | $folder = $this->folders[$namespace]; |
291 | $name = substr($path, $dotPos + 1); |
292 | $themePath = $this->theme.'/'.$namespace.'/'.$name; |
293 | } else { |
294 | $folder = $this->config->get(self::CONFIG_DEFAULT_FOLDER); |
295 | $name = $path; |
296 | $themePath = $this->theme.'/'.$path; |
297 | } |
298 | if ($this->theme) { |
299 | $themeFullPath = $this->config->getFullPath($themePath); |
300 | if (file_exists($themeFullPath.'.phtml')) { |
301 | return $themeFullPath; |
302 | } |
303 | } |
304 | return $this->config->getFullPath($folder.'/'.$name); |
305 | } |
306 | |
307 | /** |
308 | * Includes the functions.php of the theme, of the app and of the framework |
309 | */ |
310 | protected function includeFunctions() { |
311 | if ($this->functionsIncluded) { |
312 | return; |
313 | } |
314 | $themeFunctionsPath = $this->config->getFullPath($this->theme.'/functions.php'); |
315 | $appFunctionsPath = $this->config->getFullPath($this->config->get(self::CONFIG_DEFAULT_FOLDER).'/'.'functions.php'); |
316 | $defaultFunctionsPath = dirname(__FILE__) . '/../views/functions.php'; |
317 | if ($this->theme && file_exists($themeFunctionsPath)) { |
318 | require_once $themeFunctionsPath; |
319 | } |
320 | if (file_exists($appFunctionsPath)) { |
321 | require_once $appFunctionsPath; |
322 | } |
323 | require_once $defaultFunctionsPath; |
324 | $this->functionsIncluded = true; |
325 | } |
326 | } |