-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHeadTag.php
195 lines (166 loc) · 4.56 KB
/
HeadTag.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
<?php
declare(strict_types=1);
/**
* This file is part of the EaseHtml package
*
* https://github.com/VitexSoftware/php-ease-html
*
* (c) Vítězslav Dvořák <http://vitexsoftware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ease\Html;
/**
* @author Vítězslav Dvořák <info@vitexsoftware.cz>, Jana Viktorie Borbina <jana@borbina.com>
*
* HTML webPage head class.
*/
class HeadTag extends PairTag
{
/**
* Javascripts to render in page.
*/
public array $javaScripts = [];
/**
* Css definitions.
*
* @var array of strings
*/
public array $cascadeStyles = [];
/**
* Content Charset.
*/
public string $charSet = 'utf-8';
/**
* Html HEAD tag with basic contents and skin support.
*
* @param mixed $content inserted content
*/
public function __construct($content = null)
{
parent::__construct('head', null, $content);
$this->addItem('<meta http-equiv="Content-Type" content="text/html; charset='.$this->charSet.'" />');
}
/**
* Change name directly to head.
*
* @param string $objectName object name
*
* @return string final object name
*/
public function setObjectName($objectName = null)
{
return parent::setObjectName('head');
}
/**
* Render a script block.
*
* @param string $javaScript inserted script
*
* @return string
*/
public static function jsEnclosure($javaScript)
{
return <<<'EOD'
<script>
// <![CDATA[
EOD.$javaScript.<<<'EOD'
// ]]>
</script>
EOD;
}
/**
* Handle page title.
*/
public function finalize(): void
{
$this->addItem('<title>'.\Ease\WebPage::singleton()->getPageTitle().'</title>');
parent::finalize();
}
/**
* @param string $divider use '' for optimized output
*
* @return string
*/
public static function getScriptsRendered(
array $scriptsArray,
$divider = "\n"
) {
$scriptsRendered = '';
ksort($scriptsArray, \SORT_NUMERIC);
$scriptsInline = [];
$scriptsIncluded = [];
$ODRStack = [];
foreach ($scriptsArray as $script) {
$scriptType = $script[0];
$scriptBody = substr($script, 1);
switch ($scriptType) {
case '#':
$scriptsIncluded[] = '<script src="'.$scriptBody.'"></script>';
break;
case '@':
$scriptsInline[] = $scriptBody;
break;
case '$':
$ODRStack[] = $scriptBody;
break;
}
}
if (!empty($scriptsIncluded)) {
$scriptsRendered .= $divider.implode($divider, $scriptsIncluded);
}
if (!empty($scriptsInline)) {
$scriptsRendered .= $divider.self::jsEnclosure(implode(
$divider,
$scriptsInline,
));
}
if (!empty($ODRStack)) {
$scriptsRendered .= $divider.
self::jsEnclosure(
'$(document).ready(function () { '.implode(
$divider,
$ODRStack,
).' });',
);
}
return $scriptsRendered;
}
/**
* Get included and inline Syles Fragment rendered.
*
* @param string $media
* @param string $divider use '' for optimized output
*
* @return string
*/
public static function getStylesRendered(
array $stylesArray,
$media = 'screen',
$divider = "\n"
) {
$cascadeStyles = [];
$cascadeStylesIncludes = [];
foreach ($stylesArray as $styleRes => $style) {
if ($styleRes === $style) {
$cascadeStylesIncludes[] = '<link href="'.$style.'" rel="stylesheet" type="text/css" media="'.$media.'" />';
} else {
$cascadeStyles[] = $style;
}
}
return empty($stylesArray) ? '' : implode($divider, $cascadeStylesIncludes).$divider.'<style>'.implode(
$divider,
$cascadeStyles,
).'</style>';
}
/**
* Renders the header of the HTML page.
*/
public function draw(): void
{
$this->addItem(self::getStylesRendered(\Ease\WebPage::singleton()->cascadeStyles));
parent::draw();
$this->drawStatus = true;
}
}