-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrl.php
More file actions
378 lines (337 loc) · 10 KB
/
Copy pathUrl.php
File metadata and controls
378 lines (337 loc) · 10 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Http;
/**
* Url helper
*/
class Url
{
const RELATIVE_APP_URL = BASE_PATH . '/arikaim';
const BASE_URL = DOMAIN . BASE_PATH;
const VIEW_URL = Self::RELATIVE_APP_URL . '/view';
const EXTENSIONS_URL = Self::RELATIVE_APP_URL . '/extensions';
const LIBRARY_URL = Self::VIEW_URL . '/library';
const TEMPLATES_URL = Self::VIEW_URL . '/templates';
const COMPONENTS_URL = Self::VIEW_URL . '/components';
/**
* Replace url scheme
* @param string $url
* @param string $new
* @return string
*/
public static function replaceScheme(string $url, string $new = 'https'): string
{
if (Self::isValid($url) == false) {
return $url;
}
$scheme = \parse_url($url,PHP_URL_SCHEME);
return (empty($scheme) == true || $scheme === false) ? $url :
\str_replace($scheme,$new,$url);
}
/**
* Replace url query vars with params array
*
* @param string $url
* @param array $params
* @return string|null
*/
public static function replaceUrlQueryParams(string $url, array $params): ?string
{
$urlParts = \parse_url($url);
if ($urlParts === false) {
return null;
}
$port = $urlParts['port'] ?? '';
$path = $urlParts['path'] ?? '';
$query = $urlParts['query'] ?? '';
$fragment = $urlParts['fragment'] ?? '';
$result = $urlParts['scheme'] . '://' . $urlParts['host'];
$result = (empty($port) == false) ? $result . ':' . $port : $result;
$result .= $path;
if (empty($query) == false) {
// replce query vars values
\parse_str($query,$queryVars);
foreach ($queryVars as $key => $value) {
$queryVars[$key] = $params[$key] ?? $value;
}
$result .= '?' . \http_build_query($queryVars);
}
$result = (empty($fragment) == false) ? $result . '#' . $fragment : $result;
return $result;
}
/**
* Remove url schema
*
* @param string $url
* @return string
*/
public static function removeSchema(string $url): string
{
$schema = \parse_url($url,PHP_URL_SCHEME);
$url = \str_replace($schema,'',$url);
$url = \str_replace("://",'',$url);
return \str_replace($schema,'',$url);
}
/**
* Get url file name
*
* @param string $url
* @return string|null
*/
public static function getUrlFileName(string $url): ?string
{
$path = \parse_url($url,PHP_URL_PATH);
if (empty($path) == true) {
return null;
}
$tokens = \explode('/',$path);
return end($tokens);
}
/**
* Return url link with current language code
*
* @param string $path
* @param boolean $relative
* @param string|null $language
* @param string|null $defaultLanguage
* @return string
*/
public static function getUrl(?string $path = '', bool $relative = false, ?string $language = null, ?string $defaultLanguage = null): string
{
$defaultLanguage = $defaultLanguage ?? $language;
$path = (\substr($path,0,1) == '/') ? \substr($path,1) : $path;
$url = ($relative == false) ? Url::BASE_URL : BASE_PATH;
$url = ($url == '/') ? $url : $url . '/';
$url .= $path;
if (empty($language) == true) {
return $url;
}
return ($defaultLanguage != $language) ? Self::getLanguagePath($url,$language) : $url;
}
/**
* Get language path
*
* @param string $path
* @param string $language
* @return string
*/
public static function getLanguagePath(string $path, string $language): string
{
return (\substr($path,-1) == '/') ? $path . $language . '/' : $path . '/' . $language . '/';
}
/**
* Retrun true if url is relative
*
* @param string $url
* @return boolean
*/
public static function isRelative(?string $url): bool
{
return (\strpos($url,DOMAIN) === false);
}
/**
* Get url query param value
*
* @param string|null $url
* @param string $paramName
* @return string|null
*/
public static function getUrlParam(?string $url, string $paramName): ?string
{
$parts = \parse_url($url);
\parse_str($parts['query'],$query);
return $query[$paramName] ?? null;
}
/**
* Return true if url is remote server
*
* @param string $url
* @return boolean
*/
public static function isRemote(string $url): bool
{
if (Self::isValid($url) == false) {
return false;
}
$info = \parse_url($url);
$host = $info['host'] ?? '';
return ($host != DOMAIN);
}
/**
* Init domain and base path constants
*
* @param string $domain
* @param string $basePath
* @return void
*/
public static function init(string $domain, ?string $basePath): void
{
if (\defined('DOMAIN') == false) {
\define('DOMAIN',$domain);
}
if (\defined('BASE_PATH') == false) {
\define('BASE_PATH',$basePath);
}
}
/**
* Set app url
*
* @param string $path
* @return void
*/
public static function setAppUrl(string $path): void
{
if (\defined('APP_URL') == false) {
\define('APP_URL',Self::BASE_URL . $path);
}
}
/**
* Get theme file url
*
* @param string $template
* @param string $theme
* @param string|null $themeFile
* @return string|null
*/
public static function getThemeFileUrl(string $template, string $theme, ?string $themeFile): ?string
{
return (empty($themeFile) == true) ? null : Self::getTemplateThemeUrl($template,$theme) . $themeFile;
}
/**
* Get template theme url
*
* @param string $template
* @param string $theme
* @return string
*/
public static function getTemplateThemeUrl(string $template, string $theme): string
{
return Self::getTemplateThemesUrl($template) . '/' . $theme . '/';
}
/**
* Get template url
*
* @param string $template
* @param string $path
* @param bool $relative
* @return string
*/
public static function getTemplateUrl(string $template, string $path = '', bool $relative = true): string
{
$url = Self::TEMPLATES_URL . '/' . $template . $path;
return ($relative == false) ? DOMAIN . $url : $url;
}
/**
* Get components library url
*
* @param string $name
* @param string $path
* @param bool $relative
* @return string
*/
public static function getComponentsLibraryUrl(string $name, string $path = '', bool $relative = true): string
{
$url = Self::COMPONENTS_URL . '/' . $name . $path;
return ($relative == false) ? DOMAIN . $url : $url;
}
/**
* Get template themes url
*
* @param string $template
* @return string
*/
public static function getTemplateThemesUrl(string $template): string
{
return Self::getTemplateUrl($template) . '/themes';
}
/**
* Get UI library theme url
*
* @param string $library
* @param string $theme
* @return string
*/
public static function getLibraryThemeUrl(string $library, string $theme): string
{
return Self::getLibraryUrl($library) . '/themes/' . $theme . '/';
}
/**
* Get UI library theme file url
*
* @param string $library
* @param string $file
* @param string $theme
* @return string
*/
public static function getLibraryThemeFileUrl(string $library, string $file, string $theme): string
{
return Self::getLibraryThemeUrl($library,$theme) . $file;
}
/**
* Get UI library url
*
* @param string $library
* @param bool $relative
* @return string
*/
public static function getLibraryUrl(string $library, bool $relative = true): string
{
$url = Self::LIBRARY_URL . '/' . $library;
return ($relative == false) ? DOMAIN . $url : $url;
}
/**
* Get UI library file url
*
* @param string $library
* @param string|null $fileName
* @param array|null $params
* @return string
*/
public static function getLibraryFileUrl(string $library, string $fileName = '', ?array $params = null): string
{
$paramsText = (empty($params) == false) ? '?' . \http_build_query($params) : '';
return Self::getLibraryUrl($library) . '/' . $fileName . $paramsText;
}
/**
* Get extension view url
*
* @param string $extension
* @param string $path
* @param bool $relative
* @return string
*/
public static function getExtensionViewUrl(string $extension, string $path = '', bool $relative = true): string
{
$url = Self::EXTENSIONS_URL . '/' . $extension . '/view' . $path;
return ($relative == false) ? Self::BASE_URL . $url : $url;
}
/**
* Return true if url is valid
*
* @param string $url
* @return boolean
*/
public static function isValid(string $url): bool
{
return !(\filter_var($url,FILTER_VALIDATE_URL) === false);
}
/**
* Add url schema
*
* @param string $url
* @param string $schema
* @return string
*/
public static function addSchema(string $url, string $schema = 'http://'): string
{
$schema = \parse_url($url,PHP_URL_SCHEME);
return (empty($schema) == true || $schema === false) ? $schema . $url : $url;
}
}