1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asmaster\EquipTwig\Extension; |
4
|
|
|
|
5
|
|
|
class RequestExtension extends \Twig_Extension |
6
|
|
|
{ |
7
|
|
|
use ServerRequestTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @return string The extension name |
11
|
|
|
*/ |
12
|
|
|
public function getName() |
13
|
|
|
{ |
14
|
|
|
return 'request'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function getFunctions() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
new \Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl']), |
21
|
|
|
new \Twig_SimpleFunction('relative_url', [$this, 'generateRelativeUrl']) |
22
|
|
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $path |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function generateAbsoluteUrl($path) |
30
|
|
|
{ |
31
|
|
|
if ($this->isNetworkPath($path)) { |
32
|
|
|
return $path; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$uri = $this->request->getUri(); |
36
|
|
|
|
37
|
|
|
$host = $uri->getHost(); |
38
|
|
|
if ('' === $host) { |
39
|
|
|
return $path; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (null !== $uri->getPort()) { |
43
|
|
|
$host .= ':' . $uri->getPort(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! $this->hasLeadingSlash($path)) { |
47
|
|
|
$path = rtrim($uri->getPath(), '/') . '/' . $path; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $uri->getScheme() . '://' . $host.$path; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $path |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function generateRelativeUrl($path) |
58
|
|
|
{ |
59
|
|
|
if ($this->isNetworkPath($path) |
60
|
|
|
|| ! $this->hasLeadingSlash($path) |
61
|
|
|
) { |
62
|
|
|
return $path; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$uri = $this->request->getUri(); |
66
|
|
|
|
67
|
|
|
$basePath = $uri->getPath(); |
68
|
|
|
if ($path === $basePath) { |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$baseParts = explode('/', $basePath, -1); |
73
|
|
|
$pathParts = explode('/', $path); |
74
|
|
|
|
75
|
|
|
foreach ($baseParts as $i => $segment) { |
76
|
|
|
if (isset($pathParts[$i]) && $segment === $pathParts[$i]) { |
77
|
|
|
unset($baseParts[$i], $pathParts[$i]); |
78
|
|
|
} else { |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$path = str_repeat('../', count($baseParts)) . implode('/', $pathParts); |
84
|
|
|
|
85
|
|
|
if (empty($path)) { |
86
|
|
|
return './'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (empty($baseParts) |
90
|
|
|
&& false !== strpos(current($pathParts), ':') |
91
|
|
|
) { |
92
|
|
|
$path = './' . $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $path; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $path |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
private function isNetworkPath($path) |
103
|
|
|
{ |
104
|
|
|
return false !== strpos($path, '://') |
105
|
|
|
|| '//' === substr($path, 0, 2); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $path |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
private function hasLeadingSlash($path) |
113
|
|
|
{ |
114
|
|
|
return isset($path[0]) && '/' === $path[0]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|