-
Notifications
You must be signed in to change notification settings - Fork 990
/
Router.php
201 lines (163 loc) · 6.12 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php declare(strict_types=1);
namespace Shopware\Storefront\Framework\Routing;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\PlatformRequest;
use Symfony\Bundle\FrameworkBundle\Routing\Router as SymfonyRouter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
#[Package('storefront')]
class Router implements RouterInterface, RequestMatcherInterface, WarmableInterface, ServiceSubscriberInterface
{
/**
* @var int Used to indicate the router that we only need the path info without the sales channel prefix
*/
final public const PATH_INFO = 10;
/**
* @internal
*/
public function __construct(
private readonly SymfonyRouter $decorated,
private readonly RequestStack $requestStack
) {
}
/**
* {@inheritDoc}
*/
public static function getSubscribedServices(): array
{
return SymfonyRouter::getSubscribedServices();
}
/**
* @return array<string>
*/
public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
return $this->decorated->warmUp($cacheDir, $buildDir);
}
/**
* @return array<mixed>
*/
public function matchRequest(Request $request): array
{
if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID)) {
return $this->decorated->matchRequest($request);
}
$server = array_merge(
$request->server->all(),
['REQUEST_URI' => $request->attributes->get(RequestTransformer::SALES_CHANNEL_RESOLVED_URI)]
);
$localClone = $request->duplicate(null, null, null, null, null, $server);
return $this->decorated->matchRequest($localClone);
}
public function setContext(RequestContext $context): void
{
$this->decorated->setContext($context);
}
public function getContext(): RequestContext
{
return $this->decorated->getContext();
}
public function getRouteCollection(): RouteCollection
{
return $this->decorated->getRouteCollection();
}
/**
* @param array<string, mixed> $parameters
*/
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
{
$basePath = $this->getBasePath();
if ($referenceType === self::PATH_INFO) {
$route = $this->decorated->generate($name, $parameters);
return $this->removePrefix($route, $basePath);
}
if (!$this->isStorefrontRoute($name)) {
return $this->decorated->generate($name, $parameters, $referenceType);
}
$salesChannelBaseUrl = $this->getSalesChannelBaseUrl();
$schema = '';
// we need to insert the sales channel base url between the baseUrl and the infoPath
switch ($referenceType) {
case self::ABSOLUTE_URL:
$schema = $this->getContext()->getScheme() . ':';
// intentional no break
// no break
case self::NETWORK_PATH:
$schemaAuthority = $schema . '//' . $this->getContext()->getHost();
if ($this->getContext()->getHttpPort() !== 80) {
$schemaAuthority .= ':' . $this->getContext()->getHttpPort();
} elseif ($this->getContext()->getHttpsPort() !== 443) {
$schemaAuthority .= ':' . $this->getContext()->getHttpsPort();
}
$generated = $this->decorated->generate($name, $parameters);
$pathInfo = $this->removePrefix($generated, $basePath);
$rewrite = $schemaAuthority . rtrim($basePath, '/') . rtrim($salesChannelBaseUrl, '/') . $pathInfo;
break;
case self::RELATIVE_PATH:
// remove base path from generated url (/shopware/public or /)
$generated = $this->removePrefix(
$this->decorated->generate($name, $parameters, self::RELATIVE_PATH),
$basePath
);
// url contains the base path and the base url
// base url /shopware/public/de
$rewrite = ltrim($salesChannelBaseUrl, '/') . $generated;
break;
case self::ABSOLUTE_PATH:
default:
$generated = $this->removePrefix(
$this->decorated->generate($name, $parameters),
$basePath
);
$rewrite = $basePath . rtrim($salesChannelBaseUrl, '/') . $generated;
break;
}
return $rewrite;
}
/**
* @return array<mixed>
*/
public function match(string $pathinfo): array
{
return $this->decorated->match($pathinfo);
}
private function removePrefix(string $subject, string $prefix): string
{
if (!$prefix || mb_strpos($subject, $prefix) !== 0) {
return $subject;
}
return mb_substr($subject, mb_strlen($prefix));
}
private function getSalesChannelBaseUrl(): string
{
$request = $this->requestStack->getMainRequest();
if (!$request) {
return '';
}
$url = (string) $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
if (empty($url)) {
return $url;
}
return '/' . trim($url, '/') . '/';
}
private function getBasePath(): string
{
$request = $this->requestStack->getMainRequest();
if (!$request) {
return '';
}
return $request->getBasePath();
}
private function isStorefrontRoute(string $name): bool
{
return str_starts_with($name, 'frontend.')
|| str_starts_with($name, 'widgets.')
|| str_starts_with($name, 'payment.');
}
}