1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\RequestContext\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasUrlFunctionsTrait |
7
|
|
|
* @package Nip\Router\RequestContext\Traits |
8
|
|
|
*/ |
9
|
|
|
trait HasUrlFunctionsTrait |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get the root URL for the application. |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function root() |
18
|
|
|
{ |
19
|
|
|
return rtrim($this->getSchemeAndHttpHost() . $this->getBaseUrl(), '/'); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the full URL for the request. |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function fullUrl() |
29
|
|
|
{ |
30
|
|
|
$query = $this->getQueryString(); |
|
|
|
|
31
|
|
|
$question = $this->getBaseUrl() . $this->getPathInfo() == '/' ? '/?' : '?'; |
|
|
|
|
32
|
|
|
return $query ? $this->url() . $question . $query : $this->url(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the URL (no query string) for the request. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function url() |
41
|
|
|
{ |
42
|
|
|
return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generates a normalized URI (URL) for the Request. |
47
|
|
|
* |
48
|
|
|
* @return string A normalized URI (URL) for the Request |
49
|
|
|
* |
50
|
|
|
* @see getQueryString() |
51
|
|
|
*/ |
52
|
|
|
public function getUri() |
53
|
|
|
{ |
54
|
|
|
if (null !== $queryString = $this->getQueryString()) { |
55
|
|
|
$queryString = '?' . $queryString; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->getSchemeAndHttpHost() . $this->getBaseUrl() . $this->getPathInfo() . $queryString; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getSchemeAndHttpHost() |
65
|
|
|
{ |
66
|
|
|
return $this->getScheme() . '://' . $this->getHttpHost(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getHttpHost() |
73
|
|
|
{ |
74
|
|
|
$scheme = $this->getScheme(); |
75
|
|
|
$port = ''; |
76
|
|
|
if ('http' === $scheme && 80 != $this->getHttpPort()) { |
|
|
|
|
77
|
|
|
$port = ':' . $this->getHttpPort(); |
78
|
|
|
} elseif ('https' === $scheme && 443 != $this->getHttpsPort()) { |
|
|
|
|
79
|
|
|
$port = ':' . $this->getHttpsPort(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->getHost() . $port; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|