1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Url; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A helper to create urls. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CUrl |
10
|
|
|
{ |
11
|
|
|
use \Anax\TConfigure; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Properties |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
const URL_CLEAN = 'clean'; // controller/action/param1/param2 |
20
|
|
|
const URL_APPEND = 'append'; // index.php/controller/action/param1/param2 |
21
|
|
|
|
22
|
|
|
private $urlType = self::URL_APPEND; // What type of urls to generate |
23
|
|
|
|
24
|
|
|
private $siteUrl = null; // Siteurl to prepend to all absolute urls created |
25
|
|
|
private $baseUrl = null; // Baseurl to prepend to all relative urls created |
26
|
|
|
private $scriptName = null; // Name of the frontcontroller script |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets |
30
|
|
|
private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set default values from configuration. |
36
|
|
|
* |
37
|
|
|
* @return this. |
|
|
|
|
38
|
|
|
*/ |
39
|
17 |
|
public function setDefaultsFromConfiguration() |
40
|
|
|
{ |
41
|
17 |
|
$set = [ |
42
|
|
|
"urlType", |
43
|
2 |
|
"siteUrl", |
44
|
2 |
|
"baseUrl", |
45
|
2 |
|
"staticSiteUrl", |
46
|
2 |
|
"staticBaseUrl", |
47
|
|
|
"scriptName", |
48
|
15 |
|
]; |
49
|
|
|
|
50
|
3 |
|
foreach ($set as $item) { |
51
|
|
|
if (!isset($this->config[$item])) { |
52
|
12 |
|
continue; |
53
|
|
|
} |
54
|
4 |
|
|
55
|
|
|
$this->$item = $this->config[$item]; |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
return $this; |
59
|
8 |
|
} |
60
|
4 |
|
|
61
|
|
|
|
62
|
4 |
|
|
63
|
|
|
/** |
64
|
|
|
* Create an url and prepending the baseUrl. |
65
|
|
|
* |
66
|
|
|
* @param string $uri part of uri to use when creating an url. |
67
|
|
|
* "" or null means baseurl to current |
68
|
|
|
* frontcontroller. |
69
|
|
|
* @param string $baseuri optional base to prepend uri. |
70
|
|
|
* |
71
|
|
|
* @return string as resulting url. |
72
|
|
|
*/ |
73
|
|
|
public function create($uri = null, $baseuri = null) |
74
|
|
|
{ |
75
|
|
|
if (empty($uri) && empty($baseuri)) { |
76
|
|
|
// Empty uri means baseurl |
77
|
|
|
return $this->baseUrl |
78
|
|
|
. (($this->urlType == self::URL_APPEND) |
79
|
|
|
? "/$this->scriptName" |
80
|
|
|
: null); |
81
|
|
|
} elseif (empty($uri)) { |
82
|
|
|
// Empty uri means baseurl with appended $baseuri |
83
|
|
|
; |
84
|
|
|
} elseif (substr($uri, 0, 7) == "http://" |
85
|
|
|
|| substr($uri, 0, 8) == "https://" |
86
|
|
|
|| substr($uri, 0, 2) == "//" |
87
|
|
|
) { |
88
|
|
|
// Fully qualified, just leave as is. |
89
|
|
|
return rtrim($uri, "/"); |
90
|
|
|
} elseif ($uri[0] == "/") { |
91
|
|
|
// Absolute url, prepend with siteUrl |
92
|
|
|
return rtrim($this->siteUrl . rtrim($uri, '/'), '/'); |
93
|
|
|
} elseif ($uri[0] == "#" |
94
|
|
|
|| $uri[0] == "?" |
95
|
|
|
) { |
96
|
|
|
// Hashtag url to local page, or query part, leave as is. |
97
|
|
|
return $uri; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Prepend uri with baseuri |
101
|
|
|
$uri = rtrim($uri, "/"); |
102
|
|
|
if (!empty($baseuri)) { |
103
|
|
|
$uri = rtrim($baseuri, "/") . "/$uri"; |
104
|
|
|
} |
105
|
8 |
|
|
106
|
|
|
// Remove the trailing index part of the url |
107
|
8 |
|
if (basename($uri) == "index") { |
108
|
|
|
$uri = dirname($uri); |
109
|
|
|
} |
110
|
8 |
|
|
111
|
|
|
if ($this->urlType == self::URL_CLEAN) { |
112
|
4 |
|
return rtrim($this->baseUrl . "/" . $uri, "/"); |
113
|
|
|
} else { |
114
|
4 |
|
return rtrim($this->baseUrl . "/" . $this->scriptName . "/" . $uri, "/"); |
115
|
|
|
} |
116
|
2 |
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
2 |
|
/** |
121
|
|
|
* Create an url and prepend the baseUrl to the directory of the frontcontroller. |
122
|
2 |
|
* |
123
|
2 |
|
* @param string $uri part of uri to use when creating an url. "" or null means baseurl to |
124
|
2 |
|
* directory of the current frontcontroller. |
125
|
|
|
* |
126
|
|
|
* @return string as resulting url. |
127
|
|
|
*/ |
128
|
|
|
public function createRelative($uri = null) |
129
|
|
|
{ |
130
|
|
View Code Duplication |
if (empty($uri)) { |
|
|
|
|
131
|
|
|
// Empty uri means baseurl |
132
|
|
|
return $this->baseUrl; |
133
|
|
|
} elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") { |
134
|
|
|
// Fully qualified, just leave as is. |
135
|
|
|
return rtrim($uri, '/'); |
136
|
17 |
|
} elseif ($uri[0] == '/') { |
137
|
|
|
// Absolute url, prepend with siteUrl |
138
|
17 |
|
return rtrim($this->siteUrl . rtrim($uri, '/'), '/'); |
139
|
17 |
|
} |
140
|
|
|
|
141
|
|
|
$uri = rtrim($uri, '/'); |
142
|
|
|
return $this->baseUrl . '/' . $uri; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create an url for a static asset. |
149
|
|
|
* |
150
|
|
|
* @param string $uri part of uri to use when creating an url. |
151
|
18 |
|
* |
152
|
|
|
* @return string as resulting url. |
153
|
18 |
|
*/ |
154
|
18 |
|
public function asset($uri = null) |
155
|
|
|
{ |
156
|
|
View Code Duplication |
if (empty($uri)) { |
|
|
|
|
157
|
|
|
// Allow empty |
158
|
|
|
} elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") { |
159
|
|
|
// Fully qualified, just leave as is. |
160
|
|
|
return rtrim($uri, '/'); |
161
|
|
|
} elseif ($uri[0] == '/') { |
162
|
|
|
// Absolute url, prepend with staticSiteUrl |
163
|
|
|
return rtrim($this->staticSiteUrl . rtrim($uri, '/'), '/'); |
164
|
|
|
} |
165
|
|
|
|
166
|
4 |
|
$baseUrl = isset($this->staticBaseUrl) ? $this->staticBaseUrl : $this->baseUrl; |
167
|
|
|
|
168
|
4 |
|
return empty($uri) |
169
|
4 |
|
? $baseUrl |
170
|
|
|
: $baseUrl . '/' . $uri; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the siteUrl to prepend all absolute urls created. |
177
|
|
|
* |
178
|
|
|
* @param string $url part of url to use when creating an url. |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
4 |
|
*/ |
182
|
|
|
public function setSiteUrl($url) |
183
|
4 |
|
{ |
184
|
4 |
|
$this->siteUrl = rtrim($url, '/'); |
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set the baseUrl to prepend all relative urls created. |
192
|
|
|
* |
193
|
|
|
* @param string $url part of url to use when creating an url. |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
7 |
|
*/ |
197
|
|
|
public function setBaseUrl($url) |
198
|
7 |
|
{ |
199
|
7 |
|
$this->baseUrl = rtrim($url, '/'); |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Set the siteUrl to prepend absolute urls for assets. |
207
|
|
|
* |
208
|
|
|
* @param string $url part of url to use when creating an url. |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
15 |
|
*/ |
212
|
|
|
public function setStaticSiteUrl($url) |
213
|
15 |
|
{ |
214
|
1 |
|
$this->staticSiteUrl = rtrim($url, '/'); |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
14 |
|
|
218
|
14 |
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the baseUrl to prepend relative urls for assets. |
222
|
|
|
* |
223
|
|
|
* @param string $url part of url to use when creating an url. |
224
|
|
|
* |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setStaticBaseUrl($url) |
228
|
|
|
{ |
229
|
|
|
$this->staticBaseUrl = rtrim($url, '/'); |
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set the scriptname to use when creating URL_APPEND urls. |
237
|
|
|
* |
238
|
|
|
* @param string $name as the scriptname, for example index.php. |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function setScriptName($name) |
243
|
|
|
{ |
244
|
|
|
$this->scriptName = $name; |
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set the type of urls to be generated, URL_CLEAN, URL_APPEND. |
252
|
|
|
* |
253
|
|
|
* @param string $type what type of urls to create. |
254
|
|
|
* |
255
|
|
|
* @return $this |
256
|
|
|
*/ |
257
|
|
|
public function setUrlType($type) |
258
|
|
|
{ |
259
|
|
|
if (!in_array($type, [self::URL_APPEND, self::URL_CLEAN])) { |
260
|
|
|
throw new \Exception("Unsupported Url type."); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$this->urlType = $type; |
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Create a slug of a string, to be used as url. |
271
|
|
|
* |
272
|
|
|
* @param string $str the string to format as slug. |
273
|
|
|
* |
274
|
|
|
* @return str the formatted slug. |
275
|
|
|
*/ |
276
|
|
|
public function slugify($str) |
277
|
|
|
{ |
278
|
|
|
$str = mb_strtolower(trim($str)); |
279
|
|
|
$str = str_replace(array('å','ä','ö'), array('a','a','o'), $str); |
280
|
|
|
$str = preg_replace('/[^a-z0-9-]/', '-', $str); |
281
|
|
|
$str = trim(preg_replace('/-+/', '-', $str), '-'); |
282
|
|
|
return $str; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.