1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexMasterov\TwigExtension; |
4
|
|
|
|
5
|
|
|
use AlexMasterov\TwigExtension\AbsoluteUrlGenerator; |
6
|
|
|
use AlexMasterov\TwigExtension\RelativePathGenerator; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\UriInterface; |
9
|
|
|
use Twig_Extension; |
10
|
|
|
|
11
|
|
|
final class Psr7UriExtension extends Twig_Extension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ServerRequestInterface |
15
|
1 |
|
*/ |
16
|
|
|
private $request; |
17
|
1 |
|
|
18
|
|
|
/** |
19
|
|
|
* @param ServerRequestInterface $request |
20
|
1 |
|
*/ |
21
|
|
|
public function __construct(ServerRequestInterface $request) |
22
|
|
|
{ |
23
|
1 |
|
$this->request = $request; |
24
|
1 |
|
} |
25
|
1 |
|
|
26
|
|
|
/** |
27
|
|
|
* @return string The extension name |
28
|
|
|
*/ |
29
|
|
|
public function getName() |
30
|
|
|
{ |
31
|
|
|
return 'psr7_uri'; |
32
|
|
|
} |
33
|
10 |
|
|
34
|
|
|
public function getFunctions() |
35
|
10 |
|
{ |
36
|
1 |
|
return [ |
37
|
|
|
new \Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl']), |
38
|
|
|
new \Twig_SimpleFunction('relative_path', [$this, 'generateRelativePath']) |
39
|
9 |
|
]; |
40
|
|
|
} |
41
|
9 |
|
|
42
|
9 |
|
/** |
43
|
1 |
|
* @param string $path |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
8 |
|
*/ |
47
|
1 |
|
public function generateAbsoluteUrl($path = null) |
48
|
1 |
|
{ |
49
|
|
|
$requestUri = $this->request->getUri(); |
50
|
8 |
|
|
51
|
3 |
|
$url = $this->absoluteUrl($requestUri); |
52
|
3 |
|
if (null === $path) { |
53
|
|
|
return $url; |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
if ($this->isNetworkPath($path)) { |
57
|
|
|
return $path; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$this->hasLeadingSlash($url)) { |
61
|
|
|
$url = rtrim($url, '/') . "/{$path}"; |
62
|
16 |
|
} |
63
|
|
|
|
64
|
16 |
|
return $url; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
15 |
|
* @param string $path |
69
|
|
|
* |
70
|
15 |
|
* @return string |
71
|
15 |
|
*/ |
72
|
3 |
|
public function generateRelativePath($path) |
73
|
|
|
{ |
74
|
|
|
if ($this->isNetworkPath($path) || !$this->hasLeadingSlash($path)) { |
75
|
12 |
|
return $path; |
76
|
12 |
|
} |
77
|
|
|
|
78
|
12 |
|
$requestUri = $this->request->getUri(); |
79
|
12 |
|
$path = $this->relativePath($requestUri); |
80
|
12 |
|
|
81
|
12 |
|
return $path; |
82
|
7 |
|
} |
83
|
|
|
|
84
|
12 |
|
/** |
85
|
|
|
* @param UriInterface $uri |
86
|
12 |
|
* |
87
|
|
|
* @return string |
88
|
12 |
|
*/ |
89
|
1 |
|
private function relativePath(UriInterface $uri) |
90
|
|
|
{ |
91
|
|
|
$generator = new AbsoluteUrlGenerator($uri); |
|
|
|
|
92
|
11 |
|
|
93
|
1 |
|
return $generator(); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
11 |
|
/** |
97
|
|
|
* @param UriInterface $uri |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function absoluteUrl(UriInterface $uri) |
102
|
|
|
{ |
103
|
|
|
$generator = new RelativePathGenerator($uri); |
|
|
|
|
104
|
26 |
|
|
105
|
|
|
return $generator(); |
106
|
26 |
|
} |
107
|
26 |
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $path |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
private function isNetworkPath($path) |
114
|
|
|
{ |
115
|
23 |
|
return false !== strpos($path, '://') |
116
|
|
|
|| '//' === substr($path, 0, 2); |
117
|
23 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $path |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
private function hasLeadingSlash($path) |
125
|
|
|
{ |
126
|
|
|
return isset($path[0]) && '/' === $path[0]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.