-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLayout.php
235 lines (190 loc) · 3.7 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
<?php
/**
Tools for working with Layouts
Add Scripts and Styles
*/
namespace Edoceo\Radix;
class Layout
{
private static $_script_head;
private static $_script_tail;
private static $_style_head;
/**
@param $src Source Link or Source Code
@param $pos Position, 'head' or null
*/
static function addScript($src, $pos='tail')
{
// Init
if (empty(self::$_script_head)) {
self::$_script_head = array();
}
if (empty(self::$_script_tail)) {
self::$_script_tail = array();
}
$k = self::_addScript_Kind($src);
switch ($pos) {
case 'head':
self::$_script_head[] = array(
'kind' => $k,
'data' => $src,
);
break;
case 'tail':
default:
self::$_script_tail[] = array(
'kind' => $k,
'data' => $src,
);
break;
}
}
/**
Determines if the source is Code, a Link or a full HTML Node
If $src starts with 'h' or '/', it's a link
If it it starts with '<' it's a Node
Else Code
@param $src The Source String
@return "code" | "link" | "node"
*/
private static function _addScript_Kind($src)
{
$src = trim($src);
$clt = null; // Code or Link or full Tag
switch (substr($src, 0, 1)) {
case 'h':
case '/':
$clt = 'link';
break;
case '<':
$clt = 'node';
break;
default:
$clt = 'code';
break;
}
return $clt;
}
/**
Return the JS to Render
*/
static function getScript($pos='tail')
{
$ret = array();
switch ($pos) {
case 'head':
if (empty(self::$_script_head)) {
return null;
}
foreach (self::$_script_head as $i => $s) {
$ret[] = self::_getScript_HTML($s);
}
break;
case 'tail':
default:
if (empty(self::$_script_tail)) {
return null;
}
foreach (self::$_script_tail as $i => $s) {
$ret[] = self::_getScript_HTML($s);
}
break;
}
return trim(implode("\n", $ret));
}
/**
@param $s a Script Descriptor
@return $s as HTML
*/
private static function _getScript_HTML($s)
{
switch ($s['kind']) {
case 'code':
return sprintf('<script>%s</script>', $s['data']);
case 'link':
return sprintf('<script src="%s"></script>', $s['data']);
case 'node':
return $s['data'];
}
}
/**
Add CSS as Link or Text
*/
static function addStyle($css)
{
// Init
if (empty(self::$_style_head)) {
self::$_style_head = array();
}
$k = self::_addStyle_Kind($css);
self::$_style_head[] = array(
'kind' => $k,
'data' => $css,
);
}
/**
Determines if the source is Code, a Link or a full HTML Node
If $src starts with 'http' or '/', it's a link
If it it starts with '<' it's a Node
Else Code
@param $src The Source String
@return "code" | "link" | "node"
*/
private static function _addStyle_Kind($src)
{
$src = trim($src);
$clt = null; // Code or Link or full Tag
$c4 = substr($src, 0, 4);
// This is too foolish
switch ($c4) {
case 'http':
return 'link' ;
case '<lin';
case '<sty':
return 'node';
}
$c2 = substr($src, 0, 2);
switch ($c2) {
case '//':
return 'link';
case '/*':
return 'code';
}
$c1 = substr($src, 0, 1);
switch ($c1) {
case '/':
return 'link';
break;
}
return 'code';
}
/**
@return the Style Sheets and Code
*/
static function getStyle()
{
$ret = array();
if (empty(self::$_style_head)) {
return null;
}
foreach (self::$_style_head as $i => $s) {
$ret[] = self::_getStyle_HTML($s);
}
return trim(implode("\n", $ret));
}
/**
@param $s a Style Descriptor
@return $s as HTML
*/
private static function _getStyle_HTML($s)
{
switch ($s['kind']) {
case 'code':
return sprintf('<style>%s</style>', $s['data']);
case 'link':
return sprintf('<link href="%s" rel="stylesheet">', $s['data']);
case 'node':
return $s['data'];
}
}
}