|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package application |
|
4
|
|
|
* @subpackage sitemaps |
|
5
|
|
|
* @author marius orcisk <[email protected]> |
|
6
|
|
|
* @date 17.01.25 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace vsc\application\sitemaps; |
|
9
|
|
|
|
|
10
|
|
|
use vsc\infrastructure\urls\UrlParserA; |
|
11
|
|
|
|
|
12
|
|
|
trait ResourceMapTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $sTitle; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $aResources = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @throws ExceptionSitemap |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract public function getModulePath(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $sTitle |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function setTitle($sTitle) { |
|
34
|
1 |
|
$this->sTitle = $sTitle; |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
20 |
|
public function getTitle() { |
|
41
|
20 |
|
return $this->sTitle; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $aResources |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function setResources($aResources) { |
|
49
|
1 |
|
$this->aResources = $aResources; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ResourceMapTrait $oMap |
|
|
|
|
|
|
54
|
|
|
*/ |
|
55
|
22 |
|
protected function mergeResources($oMap) { |
|
56
|
22 |
|
$aResources = $this->getResources(); |
|
57
|
22 |
|
if ($oMap instanceof ResourceMapInterface) { |
|
58
|
22 |
|
$aParentResources = $oMap->getResources(); |
|
59
|
|
|
// iterate over our resources and merging with matching existing parent resources |
|
60
|
22 |
|
foreach ($aResources as $resType => $resources) { |
|
61
|
2 |
|
if (!isset($aParentResources[$resType])) { continue; } |
|
62
|
|
|
|
|
63
|
2 |
|
foreach ($resources as $key => $path) { |
|
64
|
2 |
|
if (!isset($aParentResources[$resType][$key])) { continue; } |
|
65
|
2 |
|
$aResources[$resType][$key] = array_merge($aResources[$resType][$key], $aParentResources[$resType][$key]); |
|
66
|
2 |
|
unset ($aParentResources[$resType][$key]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
// adding elements that exist just in parent resources |
|
70
|
22 |
|
foreach ($aParentResources as $resType => $resources) { |
|
71
|
2 |
|
if (!isset($aParentResources[$resType])) { $aResources[$resType] = []; } |
|
72
|
|
|
|
|
73
|
2 |
|
foreach ($resources as $key => $path) { |
|
74
|
2 |
|
$aResources[$resType][$key] = $aParentResources[$resType][$key]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
22 |
|
$this->aResources = $aResources; |
|
79
|
22 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $sVar |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function removeHeader($sVar) { |
|
86
|
1 |
|
$this->aResources['headers'][$sVar] = null; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param $sVar |
|
91
|
|
|
* @param $sVal |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function addHeader($sVar, $sVal) { |
|
95
|
1 |
|
$this->aResources['headers'][$sVar] = $sVal; |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $sVar |
|
100
|
|
|
* @param $sVal |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function addSetting($sVar, $sVal) { |
|
104
|
1 |
|
$this->aResources['settings'][$sVar] = $sVal; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $sPath |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getResourcePath($sPath) { |
|
112
|
|
|
if (is_file($this->getModulePath() . $sPath)) { |
|
113
|
|
|
$sPath = $this->getModulePath() . $sPath; |
|
114
|
|
|
} |
|
115
|
|
|
$oUrl = UrlParserA::url($sPath); |
|
116
|
|
|
if ($oUrl->isLocal()) { |
|
117
|
|
|
// I had a bad habit of correcting external URL's |
|
118
|
|
|
$sPath = $oUrl->getPath(); |
|
119
|
|
|
} |
|
120
|
|
|
return $sPath; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $sPath |
|
125
|
|
|
* @param string $sMedia |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function addStyle($sPath, $sMedia = 'screen') { |
|
128
|
1 |
|
$this->aResources['styles'][$sMedia][] = $this->getResourcePath($sPath); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Adds a path for a JavaScript resource |
|
133
|
|
|
* @param string $sPath |
|
134
|
|
|
* @param bool $bInHead |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function addScript($sPath, $bInHead = false) { |
|
137
|
2 |
|
$iMainKey = (int)$bInHead; // [1] in the <head> section; [0] at the end of the *HTML document |
|
138
|
2 |
|
$this->aResources['scripts'][$iMainKey][] = $this->getResourcePath($sPath); |
|
139
|
2 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $sType The type of the link element (eg, application/rss+xml or image/png) |
|
143
|
|
|
* @param string $aData The rest of the link's attributes (href, rel, s/a) |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function addLink($sType, $aData) { |
|
147
|
1 |
|
if (array_key_exists('href', $aData)) { |
|
148
|
|
|
$aData['href'] = $this->getResourcePath($aData['href']); |
|
149
|
|
|
} |
|
150
|
1 |
|
if (array_key_exists('src', $aData)) { |
|
151
|
|
|
$aData['src'] = $this->getResourcePath($aData['src']); ; |
|
152
|
|
|
} |
|
153
|
1 |
|
$this->aResources['links'][$sType][] = $aData; |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $sName |
|
158
|
|
|
* @param string $sValue |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
2 |
|
public function addMeta($sName, $sValue) { |
|
162
|
2 |
|
$this->aResources['meta'][$sName] = $sValue; |
|
163
|
2 |
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $sType |
|
|
|
|
|
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
20 |
|
public function getResources($sType = null) { |
|
170
|
20 |
|
if (!is_null($sType)) { |
|
171
|
|
|
if (array_key_exists($sType, $this->aResources)) { |
|
172
|
|
|
$aResources = $this->aResources[$sType]; |
|
173
|
|
|
} else { |
|
174
|
|
|
$aResources = array(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $aResources; |
|
178
|
|
|
} else { |
|
179
|
20 |
|
return $this->aResources; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param null $sMedia |
|
185
|
|
|
* @return array|null |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function getStyles($sMedia = null) { |
|
188
|
1 |
|
$aStyles = $this->getResources('styles'); |
|
189
|
1 |
|
if (!is_null($sMedia)) { |
|
190
|
|
|
$aMediaStyles[$sMedia] = $aStyles[$sMedia]; |
|
|
|
|
|
|
191
|
|
|
return array_key_exists($sMedia, $aStyles) ? $aMediaStyles : null; |
|
192
|
|
|
} else { |
|
193
|
1 |
|
return $aStyles; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param null $sName |
|
199
|
|
|
* @return array|string |
|
200
|
|
|
*/ |
|
201
|
3 |
|
public function getMetas($sName = null) { |
|
202
|
3 |
|
$aMetas = $this->getResources('meta'); |
|
203
|
3 |
|
if (!is_null($sName)) { |
|
204
|
1 |
|
return array_key_exists($sName, $aMetas) ? $aMetas[$sName] : ''; |
|
205
|
|
|
} else { |
|
206
|
2 |
|
return $aMetas; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param bool $bInHead |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
1 |
|
public function getScripts($bInHead = false) { |
|
215
|
1 |
|
$aAllScripts = $this->getResources('scripts'); |
|
216
|
1 |
|
if ($bInHead && array_key_exists(1, $aAllScripts)) { |
|
217
|
|
|
return $aAllScripts[1]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
1 |
|
if (!$bInHead && array_key_exists(0, $aAllScripts)) { |
|
221
|
|
|
return $aAllScripts[0]; // [1] -> script goes in the <head> [0] - script is loaded at the end of the source |
|
222
|
|
|
} |
|
223
|
1 |
|
return []; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @return array |
|
228
|
|
|
*/ |
|
229
|
1 |
|
public function getSettings() { |
|
230
|
1 |
|
return $this->getResources('settings'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $sType |
|
|
|
|
|
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
2 |
|
public function getLinks($sType = null) { |
|
238
|
2 |
|
$aLinks = $this->getResources('links'); |
|
239
|
|
|
|
|
240
|
2 |
|
if (!is_null($sType)) { |
|
241
|
1 |
|
if (array_key_exists($sType, $aLinks)) { |
|
242
|
1 |
|
$aTLinks[$sType] = $aLinks[$sType]; |
|
|
|
|
|
|
243
|
1 |
|
$aLinks = $aTLinks; |
|
244
|
|
|
} else { |
|
245
|
|
|
$aLinks = array($sType => array()); // kinda hackish, but needed to have a uniform structure |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
2 |
|
return $aLinks; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param string $sVar |
|
253
|
|
|
* @return array|string |
|
254
|
|
|
*/ |
|
255
|
1 |
|
public function getSetting($sVar) { |
|
256
|
1 |
|
$aSettings = $this->getResources('settings'); |
|
257
|
|
|
|
|
258
|
1 |
|
if (array_key_exists($sVar, $aSettings)) { |
|
259
|
|
|
return $aSettings[$sVar]; |
|
260
|
|
|
} else { |
|
261
|
1 |
|
return ''; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @return array |
|
267
|
|
|
*/ |
|
268
|
2 |
|
public function getHeaders() { |
|
269
|
2 |
|
return $this->getResources('headers'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|