/
Layout.php
317 lines (284 loc) · 8.28 KB
/
Layout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace Colibri\View;
use Colibri\Pattern\Helper;
use Colibri\Util\Html;
/**
* Class Layout.
*/
class Layout extends Helper
{
/**
* @var string name of layout template to use
*/
protected static $filename = null;
// Variables that injected into layout:
/** @var string */
protected static $description = '';
/** @var string */
protected static $keywords = '';
/** @var string */
protected static $title = '';
/** @var array */
protected static $openGraph = [];
/** @var array */
protected static $css = [];
/** @var array */
protected static $js = [];
/** @var array */
protected static $jsText = [];
/** @var string */
protected static $jsTextOnReady = '';
/** @var array */
protected static $jsMgr = [];
/** @var array */
protected static $userVars = [];
/**
* Cleans up all layout variables & resets filename if specified.
*
* @param string $filename
*/
public static function clean($filename = null)
{
static::filename($filename);
static::$description =
static::$keywords =
static::$title =
static::$jsTextOnReady = '';
static::$css =
static::$js =
static::$jsText =
static::$jsMgr = [];
}
/**
* Sets or gets filename of layout.
*
* @param string $value
*
* @return string
*/
public static function filename($value = null)
{
return $value !== null ? static::$filename = $value : static::$filename;
}
/**
* @param string $name
* @param string $value
*/
public static function set(string $name, string $value)
{
static::$userVars[$name] = $value;
}
/**
* Adds included css file from specified uri-$path.
*
* @param string $cssFilename
* @param string $path
*/
public static function addCss($cssFilename, $path = RES_CSS)
{
static::$css[] = $path . $cssFilename;
}
/**
* Adds included js file from specified uri-$path.
*
* @param string $jsFilename
* @param string $path
*/
public static function addJs($jsFilename, $path = RES_JS)
{
static::$js[] = $path . $jsFilename;
}
/**
* Adds specified $jsText as injected js code.
*
* @param string $jsText
*/
public static function addJsText($jsText)
{
static::$jsText[] = $jsText;
}
/**
* Adds specified $jsText as js code, that will be called on document.ready.
* (that will be wrapped with $(document).ready(function(){ ...js-code... });).
*
* @param string $jsText
*/
public static function addJsTextOnReady($jsText)
{
static::$jsTextOnReady .= $jsText . "\n";
}
/**
* Adds included js page manager class & its call on document.ready.
*
* @param string $jsManagerName
* @param string $path
*/
public static function addJsMgr($jsManagerName, $path = RES_JS)
{
static::addJs($jsManagerName . '_mgr.js', $path);
static::$jsMgr[] = $jsManagerName;
}
/**
* Sets or gets layout page keywords to be injected into meta tag.
*
* @param string $value
*
* @return string
*/
public static function keywords($value = null)
{
return $value !== null ? static::$keywords = $value : static::$keywords;
}
/**
* Sets or gets layout page title.
*
* @param string $value
*
* @return string
*/
public static function title($value = null)
{
return $value !== null ? static::$title = $value : static::$title;
}
/**
* Sets or gets layout page description.
*
* @param string $value
*
* @return string
*/
public static function description($value = null)
{
return $value !== null ? static::$description = $value : static::$description;
}
/**
* Sets or gets layout OpenGraph property.
*
* @param string $property
* @param string $value
*
* @return string
*/
public static function og(string $property, string $value = null)
{
return $value !== null ? static::$openGraph[$property] = $value : (static::$openGraph[$property] ?? null);
}
/**
* Deletes specified css filename.
*
* @param string $cssFilename
* @param string $path
*/
public static function delCss($cssFilename, $path = RES_CSS)
{
static::$css = array_diff(static::$css, [$path . $cssFilename]);
}
/**
* Compiles the layout. Inject layout variables & given $content.
*
* @param string $content
*
* @return string
*
* @throws \Exception
*/
public static function compile($content)
{
$layoutTplVars = static::assembleTemplateVars($content);
return static::compileWith($layoutTplVars);
}
/**
* Goes through $texts array, wraps items with $template & concatenates into single string.
*
* @param array $texts
* @param string $template sprintf-like
*
* @return string
*/
protected static function concatWrapped(array $texts, $template)
{
return array_reduce($texts, function ($concatenated, $textItem) use ($template) {
return $concatenated . sprintf($template, $textItem);
}, '');
}
/**
* If $value is not Empty, escapes html & wraps with $template.
*
* @param string $value
* @param string $template sprintf-like
*
* @return string
*/
protected static function eWrap($value, $template)
{
return ! empty($value)
? sprintf($template, Html::e($value))
: '';
}
/**
* Prepare and assemble all variables for Layout PhpTemplate.
*
* @param string $content
*
* @return array
*/
protected static function assembleTemplateVars($content)
{
$layoutTplVars = [
'content' => $content,
'keywords' => static::eWrap(static::$keywords, "<meta name='keywords' content='%s' />\n"),
'title' => static::eWrap(static::$title, "<title>%s</title>\n"),
'description' => static::eWrap(static::$description, "<meta name='description' content='%s'/>\n"),
'opengraph' => static::assembleOpenGraph(),
'css' => static::concatWrapped(static::$css,
'<link type="text/css" rel="stylesheet" href="%s"/>' . "\n\t"),
'javascript' => static::concatWrapped(static::$js,
'<script type="text/javascript" src="%s"></script>' . "\n\t"),
];
// make js init code for all js managers
if (count(static::$jsMgr)) {
static::addJsTextOnReady(static::concatWrapped(static::$jsMgr, " new %s_mgr();\n"));
}
if (static::$jsTextOnReady != '') {
static::addJsText("$(document).ready(function(){\n" . static::$jsTextOnReady . '});');
}
$layoutTplVars['javascript'] .=
static::concatWrapped(static::$jsText, "<script type=\"text/javascript\">%s</script>\n");
return $layoutTplVars + static::$userVars;
}
/**
* Assemble OpenGraph properties into html meta tags.
*
* @return string
*/
protected static function assembleOpenGraph()
{
$meta = [];
foreach (static::$openGraph as $key => $value) {
$meta[] = "<meta property='og:$key' content='" . Html::e($value) . "' />";
}
return implode("\n\t", $meta) . "\n";
}
/**
* Compiles layout template with given variables.
*
* @param array $layoutTplVars
*
* @return string
*
* @throws \Exception
*/
protected static function compileWith(array $layoutTplVars)
{
if (static::$filename === null) {
throw new \Exception('Layout template file name does not set: use Layout::setFilename().');
}
$tpl = new PhpTemplate(VIEWS . static::$filename);
$tpl->vars = $layoutTplVars;
$compiledHtml = $tpl->compile();
foreach ($layoutTplVars as $key => $value) {
$compiledHtml = str_replace('{' . $key . '}', $value, $compiledHtml);
}
return $compiledHtml;
}
}