1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PTS\SyliusReferralPlugin\Service; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
7
|
|
|
|
8
|
|
|
class ChannelUrlManager |
9
|
|
|
{ |
10
|
|
|
private $channelPaths; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* ChannelUrlManager constructor. |
14
|
|
|
* @param $channelPaths |
15
|
|
|
*/ |
16
|
|
|
public function __construct($channelPaths) |
17
|
|
|
{ |
18
|
|
|
$this->channelPaths = $channelPaths; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function isChannel($url, $channel) |
22
|
|
|
{ |
23
|
|
|
if ($channel['path'] !== '/' && strpos($url, $channel['path']) === false) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
if ($channel['domain'] !== '*' && strpos($url, $channel['domain']) === false) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
if ($channel['path'] !== '/' || $channel['domain'] !== '*') { |
30
|
|
|
return true; |
31
|
|
|
} |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/* |
36
|
|
|
* urlParts = [ |
37
|
|
|
* 'scheme' |
38
|
|
|
* 'host', |
39
|
|
|
* 'basePath', |
40
|
|
|
* 'requestUri' |
41
|
|
|
* ] |
42
|
|
|
*/ |
43
|
|
|
public function addChannelToUrl($urlParts, $channel) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
if ($channel['domain'] !== '*') { |
47
|
|
|
$urlParts['host'] = $channel['domain']; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($channel['path'] !== '/') { |
51
|
|
|
$urlParts['basePath'] = $urlParts['basePath'].$channel['path']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $urlParts; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function trimChannelFromUrl($urlParts, $channel) |
58
|
|
|
{ |
59
|
|
|
$defaultChannel = $this->getDefaultChannel(); |
60
|
|
|
if ($channel['domain'] !== '*') { |
61
|
|
|
$urlParts['host'] = $defaultChannel['domain']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($channel['path'] !== '/') { |
65
|
|
|
if (isset($urlParts['basePath'])) { |
66
|
|
|
$urlParts['basePath'] = str_replace($channel['path'], $defaultChannel['path'] === '/' ? '' : $defaultChannel['path'], $urlParts['basePath']); |
67
|
|
|
} |
68
|
|
|
$urlParts['requestUri'] = str_replace($channel['path'], $defaultChannel['path'], $urlParts['requestUri']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $urlParts; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function formUrl($urlParts, $optionalQueryParameters = []) |
75
|
|
|
{ |
76
|
|
|
//$buildUrl = $urlParts['scheme']; |
77
|
|
|
|
78
|
|
|
$buildUrl = $urlParts['host']; |
79
|
|
|
|
80
|
|
|
if (isset($urlParts['basePath'])) { |
81
|
|
|
$buildUrl = $buildUrl.'/'.$urlParts['basePath']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$buildUrl = $buildUrl.'/'.$urlParts['requestUri']; |
85
|
|
|
|
86
|
|
|
$buildUrl = $urlParts['scheme'].'://'.preg_replace('~/+~', '/', preg_replace('~http(:|%3A)//~', 'http%3A%2F%2F', $buildUrl)); |
87
|
|
|
|
88
|
|
|
if (sizeof($optionalQueryParameters)) { |
89
|
|
|
$query = http_build_query($optionalQueryParameters); |
90
|
|
|
if (strpos($buildUrl, '?') !== false) { |
91
|
|
|
$buildUrl .= '&'.$query; |
92
|
|
|
} else { |
93
|
|
|
$buildUrl .= '?'.$query; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $buildUrl; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getUrlParts(Request $request) |
101
|
|
|
{ |
102
|
|
|
$scheme = $request->getScheme(); |
103
|
|
|
$host = $request->getHost(); |
104
|
|
|
$base = $request->getBasePath(); |
105
|
|
|
$requestUri = $request->getRequestUri(); |
106
|
|
|
|
107
|
|
|
$requestUri = str_replace($base !== '/' ? $base : '', '', $requestUri); |
108
|
|
|
|
109
|
|
|
return [ |
110
|
|
|
'scheme' => $scheme, |
111
|
|
|
'host' => $host, |
112
|
|
|
'basePath' => $base, |
113
|
|
|
'requestUri' => $requestUri |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getDefaultChannel() |
118
|
|
|
{ |
119
|
|
|
$filter = array_filter($this->channelPaths, function ($channel) { |
120
|
|
|
return $channel['default']; |
121
|
|
|
}); |
122
|
|
|
if (sizeof($filter) === 1) { |
123
|
|
|
return array_pop($filter); |
124
|
|
|
} |
125
|
|
|
throw new HttpException(500, 'Default channel not found'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|