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
|
20 |
|
protected function mergeResources($oMap) { |
56
|
20 |
|
$aResources = $this->getResources(); |
57
|
20 |
|
if ($oMap instanceof self) { |
58
|
2 |
|
$aParentResources = $oMap->getResources(); |
59
|
2 |
|
$aResources = array_merge($aResources, $aParentResources); |
|
|
|
|
60
|
|
|
// maybe I should merge the regexes too like processor_regex . '.*' . controller_regex |
61
|
|
|
} |
62
|
20 |
|
$this->aResources = $aResources; |
63
|
20 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $sVar |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
1 |
|
public function removeHeader($sVar) { |
70
|
1 |
|
$this->aResources['headers'][$sVar] = null; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $sVar |
75
|
|
|
* @param $sVal |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
1 |
|
public function addHeader($sVar, $sVal) { |
79
|
1 |
|
$this->aResources['headers'][$sVar] = $sVal; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $sVar |
84
|
|
|
* @param $sVal |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
1 |
|
public function addSetting($sVar, $sVal) { |
88
|
1 |
|
$this->aResources['settings'][$sVar] = $sVal; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $sPath |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
private function getResourcePath($sPath) { |
96
|
|
|
if (is_file($this->getModulePath() . $sPath)) { |
97
|
|
|
$sPath = $this->getModulePath() . $sPath; |
98
|
|
|
} |
99
|
|
|
$oUrl = UrlParserA::url($sPath); |
100
|
|
|
if ($oUrl->isLocal()) { |
101
|
|
|
// I had a bad habit of correcting external URL's |
102
|
|
|
$sPath = $oUrl->getPath(); |
103
|
|
|
} |
104
|
|
|
return $sPath; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function addStyle($sPath, $sMedia = 'screen') { |
108
|
1 |
|
$this->aResources['styles'][$sMedia][] = $this->getResourcePath($sPath); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* Adds a path for a JavaScript resource |
114
|
|
|
* @param string $sPath |
115
|
|
|
* @param bool $bInHead |
116
|
|
|
*/ |
117
|
2 |
|
public function addScript($sPath, $bInHead = false) { |
118
|
2 |
|
$iMainKey = (int)$bInHead; // [1] in the <head> section; [0] at the end of the *HTML document |
|
|
|
|
119
|
2 |
|
$this->aResources['scripts'][$iMainKey][] = $this->getResourcePath($sPath); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $sType The type of the link element (eg, application/rss+xml or image/png) |
124
|
|
|
* @param string $aData The rest of the link's attributes (href, rel, s/a) |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
1 |
|
public function addLink($sType, $aData) { |
128
|
1 |
|
if (array_key_exists('href', $aData)) { |
129
|
|
|
$aData['href'] = $this->getResourcePath($aData['href']); |
130
|
|
|
} |
131
|
1 |
|
if (array_key_exists('src', $aData)) { |
132
|
|
|
$aData['src'] = $this->getResourcePath($aData['src']); ; |
|
|
|
|
133
|
|
|
} |
134
|
1 |
|
$this->aResources['links'][$sType][] = $aData; |
135
|
1 |
|
} |
136
|
|
|
|
137
|
2 |
|
public function addMeta($sName, $sValue) { |
138
|
2 |
|
$this->aResources['meta'][$sName] = $sValue; |
139
|
2 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $sType |
|
|
|
|
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
20 |
|
public function getResources($sType = null) { |
146
|
20 |
|
if (!is_null($sType)) { |
147
|
|
|
if (array_key_exists($sType, $this->aResources)) { |
148
|
|
|
$aResources = $this->aResources[$sType]; |
149
|
|
|
} else { |
150
|
|
|
$aResources = array(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $aResources; |
154
|
|
|
} else { |
155
|
20 |
|
return $this->aResources; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function getStyles($sMedia = null) { |
160
|
1 |
|
$aStyles = $this->getResources('styles'); |
161
|
1 |
|
if (!is_null($sMedia)) { |
162
|
|
|
$aMediaStyles[$sMedia] = $aStyles[$sMedia]; |
|
|
|
|
163
|
|
|
return array_key_exists($sMedia, $aStyles) ? $aMediaStyles : null; |
164
|
|
|
} else { |
165
|
1 |
|
return $aStyles; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
public function getMetas($sName = null) { |
|
|
|
|
170
|
3 |
|
$aMetas = $this->getResources('meta'); |
171
|
3 |
|
if (!is_null($sName)) { |
172
|
1 |
|
return array_key_exists($sName, $aMetas) ? $aMetas[$sName] : ''; |
173
|
|
|
} else { |
174
|
2 |
|
return $aMetas; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param bool $bInHead |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
1 |
|
public function getScripts($bInHead = false) { |
183
|
1 |
|
$aAllScripts = $this->getResources('scripts'); |
184
|
1 |
|
if ($bInHead && array_key_exists(1, $aAllScripts)) { |
185
|
|
|
return $aAllScripts[1]; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
if (!$bInHead && array_key_exists(0, $aAllScripts)) { |
189
|
|
|
return $aAllScripts[0]; // [1] -> script goes in the <head> [0] - script is loaded at the end of the source |
190
|
|
|
} |
191
|
1 |
|
return []; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
public function getSettings() { |
195
|
1 |
|
return $this->getResources('settings'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $sType |
|
|
|
|
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
2 |
|
public function getLinks($sType = null) { |
203
|
2 |
|
$aLinks = $this->getResources('links'); |
204
|
|
|
|
205
|
2 |
|
if (!is_null($sType)) { |
206
|
1 |
|
if (array_key_exists($sType, $aLinks)) { |
207
|
1 |
|
$aTLinks[$sType] = $aLinks[$sType]; |
|
|
|
|
208
|
1 |
|
$aLinks = $aTLinks; |
|
|
|
|
209
|
|
|
} else { |
210
|
|
|
$aLinks = array($sType => array()); // kinda hackish, but needed to have a uniform structure |
211
|
|
|
} |
212
|
|
|
} |
213
|
2 |
|
return $aLinks; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $sVar |
218
|
|
|
* @return array|string |
219
|
|
|
*/ |
220
|
1 |
|
public function getSetting($sVar) { |
221
|
1 |
|
$aSettings = $this->getResources('settings'); |
222
|
|
|
|
223
|
1 |
|
if (array_key_exists($sVar, $aSettings)) { |
224
|
|
|
return $aSettings[$sVar]; |
225
|
|
|
} else { |
226
|
1 |
|
return ''; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
public function getHeaders() { |
231
|
2 |
|
return $this->getResources('headers'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|