| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
use Symfony\Component\Routing\RequestContext; |
| 15 |
use Symfony\Component\Routing\RequestContextAwareInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class UrlHelper |
| 23 |
{ |
| 24 |
public function __construct( |
| 25 |
private RequestStack $requestStack, |
| 26 |
private RequestContextAwareInterface|RequestContext|null $requestContext = null, |
| 27 |
) { |
| 28 |
} |
| 29 |
|
| 30 |
public function getAbsoluteUrl(string $path): string |
| 31 |
{ |
| 32 |
if (str_contains($path, '://') || str_starts_with($path, '//')) { |
| 33 |
return $path; |
| 34 |
} |
| 35 |
|
| 36 |
if (null === $request = $this->requestStack->getMainRequest()) { |
| 37 |
return $this->getAbsoluteUrlFromContext($path); |
| 38 |
} |
| 39 |
|
| 40 |
if ('#' === $path[0]) { |
| 41 |
$path = $request->getRequestUri().$path; |
| 42 |
} elseif ('?' === $path[0]) { |
| 43 |
$path = $request->getPathInfo().$path; |
| 44 |
} |
| 45 |
|
| 46 |
if (!$path || '/' !== $path[0]) { |
| 47 |
$prefix = $request->getPathInfo(); |
| 48 |
$last = \strlen($prefix) - 1; |
| 49 |
if ($last !== $pos = strrpos($prefix, '/')) { |
| 50 |
$prefix = substr($prefix, 0, $pos).'/'; |
| 51 |
} |
| 52 |
|
| 53 |
return $request->getUriForPath($prefix.$path); |
| 54 |
} |
| 55 |
|
| 56 |
return $request->getSchemeAndHttpHost().$path; |
| 57 |
} |
| 58 |
|
| 59 |
public function getRelativePath(string $path): string |
| 60 |
{ |
| 61 |
if (str_contains($path, '://') || str_starts_with($path, '//')) { |
| 62 |
return $path; |
| 63 |
} |
| 64 |
|
| 65 |
if (null === $request = $this->requestStack->getMainRequest()) { |
| 66 |
return $path; |
| 67 |
} |
| 68 |
|
| 69 |
return $request->getRelativeUriForPath($path); |
| 70 |
} |
| 71 |
|
| 72 |
private function getAbsoluteUrlFromContext(string $path): string |
| 73 |
{ |
| 74 |
if (null === $context = $this->requestContext) { |
| 75 |
return $path; |
| 76 |
} |
| 77 |
|
| 78 |
if ($context instanceof RequestContextAwareInterface) { |
| 79 |
$context = $context->getContext(); |
| 80 |
} |
| 81 |
|
| 82 |
if ('' === $host = $context->getHost()) { |
| 83 |
return $path; |
| 84 |
} |
| 85 |
|
| 86 |
$scheme = $context->getScheme(); |
| 87 |
$port = ''; |
| 88 |
|
| 89 |
if ('http' === $scheme && 80 !== $context->getHttpPort()) { |
| 90 |
$port = ':'.$context->getHttpPort(); |
| 91 |
} elseif ('https' === $scheme && 443 !== $context->getHttpsPort()) { |
| 92 |
$port = ':'.$context->getHttpsPort(); |
| 93 |
} |
| 94 |
|
| 95 |
if ('#' === $path[0]) { |
| 96 |
$queryString = $context->getQueryString(); |
| 97 |
$path = $context->getPathInfo().($queryString ? '?'.$queryString : '').$path; |
| 98 |
} elseif ('?' === $path[0]) { |
| 99 |
$path = $context->getPathInfo().$path; |
| 100 |
} |
| 101 |
|
| 102 |
if ('/' !== $path[0]) { |
| 103 |
$path = rtrim($context->getBaseUrl(), '/').'/'.$path; |
| 104 |
} |
| 105 |
|
| 106 |
return $scheme.'://'.$host.$port.$path; |
| 107 |
} |
| 108 |
} |