1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Generator\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Router\RequestContext; |
6
|
|
|
use Nip\Utility\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait UrlFunctionTrait |
10
|
|
|
* @package Nip\Router\Generator\Traits |
11
|
|
|
* @method RequestContext getContext() |
12
|
|
|
*/ |
13
|
|
|
trait UrlFunctionTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Get the full URL for the current request. |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function full() |
21
|
|
|
{ |
22
|
|
|
return $this->getContext()->fullUrl(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the current URL for the request. |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function current() |
31
|
|
|
{ |
32
|
|
|
return $this->to($this->getContext()->getPathInfo()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Generate an absolute URL to the given path. |
37
|
|
|
* |
38
|
|
|
* @param string $path |
39
|
|
|
* @param mixed $extra |
40
|
|
|
* @param bool|null $secure |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function to($path, $extra = [], $secure = null) |
44
|
|
|
{ |
45
|
|
|
// First we will check if the URL is already a valid URL. If it is we will not |
46
|
|
|
// try to generate a new one but will simply return the URL as is, which is |
47
|
|
|
// convenient since developers do not always have to check if it's valid. |
48
|
|
|
if ($this->isValidUrl($path)) { |
49
|
|
|
return $path; |
50
|
|
|
} |
51
|
|
|
$tail = implode('/', array_map( |
52
|
|
|
'rawurlencode', (array) $this->formatParameters($extra)) |
53
|
|
|
); |
54
|
|
|
// Once we have the scheme we will compile the "tail" by collapsing the values |
55
|
|
|
// into a single string delimited by slashes. This just makes it convenient |
56
|
|
|
// for passing the array of parameters to this URL as a list of segments. |
57
|
|
|
$root = $this->formatRoot($this->formatScheme($secure)); |
|
|
|
|
58
|
|
|
list($path, $query) = $this->extractQueryString($path); |
59
|
|
|
return $this->format( |
|
|
|
|
60
|
|
|
$root, '/' . trim($path . '/' . $tail, '/') |
61
|
|
|
) . $query; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate the URL to an application asset. |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
* @param bool|null $secure |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function asset($path, $secure = null) |
72
|
|
|
{ |
73
|
|
|
if ($this->isValidUrl($path)) { |
74
|
|
|
return $path; |
75
|
|
|
} |
76
|
|
|
// Once we get the root URL, we will check to see if it contains an index.php |
77
|
|
|
// file in the paths. If it does, we will remove it since it is not needed |
78
|
|
|
// for asset paths, but only for routes to endpoints in the application. |
79
|
|
|
$root = $this->formatRoot($this->formatScheme($secure)); |
|
|
|
|
80
|
|
|
return $this->removeIndex($root) . '/assets/' . trim($path, '/'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Determine if the given path is a valid URL. |
85
|
|
|
* |
86
|
|
|
* @param string $path |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isValidUrl($path) |
90
|
|
|
{ |
91
|
|
|
if (!Str::startsWith($path, ['#', '//', 'mailto:', 'tel:', 'http://', 'https://'])) { |
92
|
|
|
return filter_var($path, FILTER_VALIDATE_URL) !== false; |
93
|
|
|
} |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Extract the query string from the given path. |
100
|
|
|
* |
101
|
|
|
* @param string $path |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
protected function extractQueryString($path) |
105
|
|
|
{ |
106
|
|
|
if (($queryPosition = strpos($path, '?')) !== false) { |
107
|
|
|
return [ |
108
|
|
|
substr($path, 0, $queryPosition), |
109
|
|
|
substr($path, $queryPosition), |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
return [$path, '']; |
113
|
|
|
} |
114
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.