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
|
|
|
use Twig_SimpleFunction; |
11
|
|
|
|
12
|
|
|
final class Psr7UriExtension extends Twig_Extension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
1 |
|
* @var ServerRequestInterface |
16
|
|
|
*/ |
17
|
1 |
|
private $request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
1 |
|
* @param ServerRequestInterface $request |
21
|
|
|
*/ |
22
|
|
|
public function __construct(ServerRequestInterface $request) |
23
|
1 |
|
{ |
24
|
1 |
|
$this->request = $request; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string The extension name |
29
|
|
|
*/ |
30
|
|
|
public function getName() |
31
|
|
|
{ |
32
|
|
|
return 'psr7_uri'; |
33
|
10 |
|
} |
34
|
|
|
|
35
|
10 |
|
public function getFunctions() |
36
|
1 |
|
{ |
37
|
|
|
return [ |
38
|
|
|
new Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl']), |
39
|
9 |
|
new Twig_SimpleFunction('relative_path', [$this, 'generateRelativePath']) |
40
|
|
|
]; |
41
|
9 |
|
} |
42
|
9 |
|
|
43
|
1 |
|
/** |
44
|
|
|
* @param string $path |
45
|
|
|
* |
46
|
8 |
|
* @return string |
47
|
1 |
|
*/ |
48
|
1 |
|
public function generateAbsoluteUrl($path = null) |
49
|
|
|
{ |
50
|
8 |
|
$url = $this->absoluteUrl( |
51
|
3 |
|
$this->request->getUri() |
52
|
3 |
|
); |
53
|
|
|
|
54
|
8 |
|
if (null === $path) { |
55
|
|
|
return $url; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->isNetworkPath($path)) { |
59
|
|
|
return $path; |
60
|
|
|
} |
61
|
|
|
|
62
|
16 |
|
if (!$this->hasLeadingSlash($url)) { |
63
|
|
|
$url = rtrim($url, '/') . $path; |
64
|
16 |
|
} |
65
|
1 |
|
|
66
|
|
|
return $url; |
67
|
|
|
} |
68
|
15 |
|
|
69
|
|
|
/** |
70
|
15 |
|
* @param string $path |
71
|
15 |
|
* |
72
|
3 |
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function generateRelativePath($path) |
75
|
12 |
|
{ |
76
|
12 |
|
if ($this->isNetworkPath($path) || !$this->hasLeadingSlash($path)) { |
77
|
|
|
return $path; |
78
|
12 |
|
} |
79
|
12 |
|
|
80
|
12 |
|
$path = $this->relativePath( |
81
|
12 |
|
$this->request->getUri(), |
82
|
7 |
|
$path |
83
|
|
|
); |
84
|
12 |
|
|
85
|
|
|
return $path; |
86
|
12 |
|
} |
87
|
|
|
|
88
|
12 |
|
/** |
89
|
1 |
|
* @param UriInterface $uri |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
11 |
|
*/ |
93
|
1 |
|
private function absoluteUrl(UriInterface $uri) |
94
|
1 |
|
{ |
95
|
|
|
$generator = new AbsoluteUrlGenerator(); |
96
|
11 |
|
$absoluteUrl = $generator($uri); |
97
|
|
|
|
98
|
|
|
return $absoluteUrl; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param UriInterface $uri |
103
|
|
|
* |
104
|
26 |
|
* @return string |
105
|
|
|
*/ |
106
|
26 |
|
private function relativePath(UriInterface $uri, $path) |
107
|
26 |
|
{ |
108
|
|
|
$generator = new RelativePathGenerator(); |
109
|
|
|
$relativePath = $generator($uri, $path); |
110
|
|
|
|
111
|
|
|
return $relativePath; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
23 |
|
* @param string $path |
116
|
|
|
* |
117
|
23 |
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
private function isNetworkPath($path) |
120
|
|
|
{ |
121
|
|
|
return false !== strpos($path, '://') |
122
|
|
|
|| '//' === substr($path, 0, 2); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $path |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
private function hasLeadingSlash($path) |
131
|
|
|
{ |
132
|
|
|
return isset($path[0]) && '/' === $path[0]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|