|
12 | 12 | namespace Symfony\Component\HttpFoundation;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Routing\RequestContext;
|
| 15 | +use Symfony\Component\Routing\RequestContextAwareInterface; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * A helper service for manipulating URLs within and outside the request scope.
|
|
20 | 21 | */
|
21 | 22 | final class UrlHelper
|
22 | 23 | {
|
23 |
| - private RequestStack $requestStack; |
24 |
| - private ?RequestContext $requestContext; |
25 | 24 |
|
26 |
| - public function __construct(RequestStack $requestStack, RequestContext $requestContext = null) |
27 |
| - { |
28 |
| - $this->requestStack = $requestStack; |
29 |
| - $this->requestContext = $requestContext; |
| 25 | + public function __construct( |
| 26 | + private RequestStack $requestStack, |
| 27 | + private RequestContextAwareInterface|RequestContext|null $requestContext = null, |
| 28 | + ) { |
30 | 29 | }
|
31 | 30 |
|
32 | 31 | public function getAbsoluteUrl(string $path): string
|
@@ -73,28 +72,36 @@ public function getRelativePath(string $path): string
|
73 | 72 |
|
74 | 73 | private function getAbsoluteUrlFromContext(string $path): string
|
75 | 74 | {
|
76 |
| - if (null === $this->requestContext || '' === $host = $this->requestContext->getHost()) { |
| 75 | + if (null === $context = $this->requestContext) { |
| 76 | + return $path; |
| 77 | + } |
| 78 | + |
| 79 | + if ($context instanceof RequestContextAwareInterface) { |
| 80 | + $context = $context->getContext(); |
| 81 | + } |
| 82 | + |
| 83 | + if ('' === $host = $context->getHost()) { |
77 | 84 | return $path;
|
78 | 85 | }
|
79 | 86 |
|
80 |
| - $scheme = $this->requestContext->getScheme(); |
| 87 | + $scheme = $context->getScheme(); |
81 | 88 | $port = '';
|
82 | 89 |
|
83 |
| - if ('http' === $scheme && 80 !== $this->requestContext->getHttpPort()) { |
84 |
| - $port = ':'.$this->requestContext->getHttpPort(); |
85 |
| - } elseif ('https' === $scheme && 443 !== $this->requestContext->getHttpsPort()) { |
86 |
| - $port = ':'.$this->requestContext->getHttpsPort(); |
| 90 | + if ('http' === $scheme && 80 !== $context->getHttpPort()) { |
| 91 | + $port = ':'.$context->getHttpPort(); |
| 92 | + } elseif ('https' === $scheme && 443 !== $context->getHttpsPort()) { |
| 93 | + $port = ':'.$context->getHttpsPort(); |
87 | 94 | }
|
88 | 95 |
|
89 | 96 | if ('#' === $path[0]) {
|
90 |
| - $queryString = $this->requestContext->getQueryString(); |
91 |
| - $path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path; |
| 97 | + $queryString = $context->getQueryString(); |
| 98 | + $path = $context->getPathInfo().($queryString ? '?'.$queryString : '').$path; |
92 | 99 | } elseif ('?' === $path[0]) {
|
93 |
| - $path = $this->requestContext->getPathInfo().$path; |
| 100 | + $path = $context->getPathInfo().$path; |
94 | 101 | }
|
95 | 102 |
|
96 | 103 | if ('/' !== $path[0]) {
|
97 |
| - $path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path; |
| 104 | + $path = rtrim($context->getBaseUrl(), '/').'/'.$path; |
98 | 105 | }
|
99 | 106 |
|
100 | 107 | return $scheme.'://'.$host.$port.$path;
|
|
0 commit comments