1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace pjpawel\Magis\View; |
4
|
|
|
|
5
|
|
|
use pjpawel\Magis\Exception\TemplateException; |
6
|
|
|
use pjpawel\Magis\Helper\AppContainerInterface; |
7
|
|
|
use pjpawel\Magis\Helper\Script; |
8
|
|
|
use pjpawel\Magis\Helper\Style; |
9
|
|
|
use pjpawel\Magis\Helper\Tag; |
10
|
|
|
use pjpawel\Magis\Template; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Paweł Podgórski <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class MagicView extends AbstractView |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Content of html |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private string $content = ''; |
23
|
|
|
/** |
24
|
|
|
* Registered events |
25
|
|
|
* @var array<string, callable> |
26
|
|
|
*/ |
27
|
|
|
private array $events = []; |
28
|
|
|
/** |
29
|
|
|
* @var list<Tag> |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
private array $headTags = []; |
32
|
|
|
/** |
33
|
|
|
* @var list<string> |
34
|
|
|
*/ |
35
|
|
|
private array $jsFiles = []; |
36
|
|
|
/** |
37
|
|
|
* @var list<string> |
38
|
|
|
*/ |
39
|
|
|
private array $cssFiles = []; |
40
|
|
|
/** |
41
|
|
|
* @var list<Script> |
42
|
|
|
*/ |
43
|
|
|
private array $jsBlocks = []; |
44
|
|
|
/** |
45
|
|
|
* @var list<Style> |
46
|
|
|
*/ |
47
|
|
|
private array $cssBlocks = []; |
48
|
|
|
private string $language = 'en'; |
49
|
|
|
private string $charset = 'UTF-8'; |
50
|
|
|
private string $title = ''; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function __construct(string $templateDir) |
54
|
|
|
{ |
55
|
|
|
parent::__construct($templateDir); |
56
|
|
|
$this->registerDefaultTriggers(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function registerDefaultTriggers() |
60
|
|
|
{ |
61
|
|
|
$this->registerTrigger( |
62
|
|
|
Event::BeforeRun, |
63
|
|
|
function (MagicView $view) { |
64
|
|
|
$view->content .= '<!DOCTYPE html><html lang="' . $view->loadLanguage() . '">'; |
65
|
|
|
$view->content .= '<head><title>' . $view->getTitle() . '</title><meta charset="' . $view->getCharset() . '">'; |
66
|
|
|
foreach ($view->getHeadTags() as $tag) { |
67
|
|
|
$view->content .= $tag->show() . PHP_EOL; |
68
|
|
|
} |
69
|
|
|
foreach ($view->getCssBlocks() as $css) { |
70
|
|
|
$view->content .= $css->show() . PHP_EOL; |
71
|
|
|
} |
72
|
|
|
$view->content .= '</head>'; |
73
|
|
|
$view->content .= '<body>'; |
74
|
|
|
foreach ($view->getCssFiles() as $css) { |
75
|
|
|
$view->content .= $css . PHP_EOL; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
$this->registerTrigger( |
80
|
|
|
Event::AfterRun, |
81
|
|
|
function (MagicView $view) { |
82
|
|
|
foreach ($view->getJsFiles() as $js) { |
83
|
|
|
$view->content .= $js . PHP_EOL; |
84
|
|
|
} |
85
|
|
|
foreach ($view->getJsBlocks() as $js) { |
86
|
|
|
$view->content .= $js->show() . PHP_EOL; |
87
|
|
|
} |
88
|
|
|
$view->content .= '</body>'; |
89
|
|
|
$view->content .= '</html>'; |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritDoc |
96
|
|
|
*/ |
97
|
|
|
public function render(string $template, array $params = []): string |
98
|
|
|
{ |
99
|
|
|
$template = Template::resolveTemplatePath($this->templateDir, $template); |
100
|
|
|
$this->params = array_merge_recursive($params, $this->params); |
101
|
|
|
$content = $this->renderPhpFile($template, $this->params); |
102
|
|
|
|
103
|
|
|
$this->callTrigger(Event::BeforeRun); |
104
|
|
|
$this->content .= $content; |
105
|
|
|
$this->callTrigger(Event::AfterRun); |
106
|
|
|
$this->callTrigger(Event::AfterRender); |
107
|
|
|
|
108
|
|
|
return $this->content; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function extend(string $template, array $params = []): string |
112
|
|
|
{ |
113
|
|
|
$template = Template::resolveTemplatePath($this->templateDir, $template); |
114
|
|
|
return $this->renderPhpFile($template, $params); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Event $event |
119
|
|
|
* @param callable $function |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function registerTrigger(Event $event, callable $function): void |
123
|
|
|
{ |
124
|
|
|
$this->events[$event->name] = $function; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function callTrigger(Event $event): void |
128
|
|
|
{ |
129
|
|
|
if (array_key_exists($event->name, $this->events)) { |
|
|
|
|
130
|
|
|
call_user_func($this->events[$event->name], $this); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function loadLanguage(): string |
138
|
|
|
{ |
139
|
|
|
if (isset($this->app) && is_subclass_of($this->app, AppContainerInterface::class)) { |
140
|
|
|
$this->language = $this->app->get('request')->getLocale(); |
141
|
|
|
} |
142
|
|
|
return $this->language; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return list<Tag> |
147
|
|
|
*/ |
148
|
|
|
public function getHeadTags(): array |
149
|
|
|
{ |
150
|
|
|
return $this->headTags; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param Tag $tag |
155
|
|
|
*/ |
156
|
|
|
public function addHeadTag(Tag $tag): void |
157
|
|
|
{ |
158
|
|
|
$this->headTags[] = $tag; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* This will check if title is set, |
163
|
|
|
* WARNING! This will not override title that was set! |
164
|
|
|
* |
165
|
|
|
* @param string $title |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function setTitle(string $title): void |
169
|
|
|
{ |
170
|
|
|
if (!isset($this->title)) { |
171
|
|
|
$this->title = $title; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getTitle(): string |
179
|
|
|
{ |
180
|
|
|
return $this->title; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getCharset(): string |
187
|
|
|
{ |
188
|
|
|
return $this->charset; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Clears workspace |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function clear(): void |
197
|
|
|
{ |
198
|
|
|
$this->headTags = []; |
|
|
|
|
199
|
|
|
$this->jsFiles = []; |
|
|
|
|
200
|
|
|
$this->cssFiles = []; |
|
|
|
|
201
|
|
|
$this->jsBlocks = []; |
|
|
|
|
202
|
|
|
$this->cssBlocks = []; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Should be injected to the view as PackageInterface |
207
|
|
|
* |
208
|
|
|
* @param $path |
209
|
|
|
* @return string |
210
|
|
|
* @throws TemplateException |
211
|
|
|
* @deprecated |
212
|
|
|
*/ |
213
|
|
|
public function asset($path): string |
214
|
|
|
{ |
215
|
|
|
if (!isset($this->asset)) { |
216
|
|
|
throw new TemplateException(); |
217
|
|
|
} |
218
|
|
|
return $this->asset->getUrl($path); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return list<string> |
223
|
|
|
*/ |
224
|
|
|
public function getCssFiles(): array |
225
|
|
|
{ |
226
|
|
|
return $this->cssFiles; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return list<Style> |
231
|
|
|
*/ |
232
|
|
|
public function getCssBlocks(): array |
233
|
|
|
{ |
234
|
|
|
return $this->cssBlocks; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return list<string> |
239
|
|
|
*/ |
240
|
|
|
public function getJsFiles(): array |
241
|
|
|
{ |
242
|
|
|
return $this->jsFiles; |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return list<Script> |
247
|
|
|
*/ |
248
|
|
|
public function getJsBlocks(): array |
249
|
|
|
{ |
250
|
|
|
return $this->jsBlocks; |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string $charset |
255
|
|
|
*/ |
256
|
|
|
public function setCharset(string $charset): void |
257
|
|
|
{ |
258
|
|
|
$this->charset = $charset; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $cssFile |
263
|
|
|
* @return void |
264
|
|
|
*/ |
265
|
|
|
public function addCssFile(string $cssFile): void |
266
|
|
|
{ |
267
|
|
|
$this->cssFiles[] = $cssFile; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param string $cssFile |
272
|
|
|
* @return void |
273
|
|
|
*/ |
274
|
|
|
public function addJsFile(string $cssFile): void |
275
|
|
|
{ |
276
|
|
|
$this->jsFiles[] = $cssFile; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param Style $cssBlock |
281
|
|
|
* @return void |
282
|
|
|
*/ |
283
|
|
|
public function addCssBlock(Style $cssBlock): void |
284
|
|
|
{ |
285
|
|
|
$this->cssBlocks[] = $cssBlock; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param Script $jsBlock |
290
|
|
|
* @return void |
291
|
|
|
*/ |
292
|
|
|
public function addJsBlock(Script $jsBlock): void |
293
|
|
|
{ |
294
|
|
|
$this->jsBlocks[] = $jsBlock; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths