| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
use Symfony\Component\Routing\RequestContext; |
| 15 |
use Symfony\Component\Routing\RequestContextAwareInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* A helper service for manipulating URLs within and outside the request scope. |
| 19 |
* |
| 20 |
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> |
| 21 |
*/ |
| 22 |
final class UrlHelper |
| 23 |
{ |
| 24 |
private $requestStack; |
| 25 |
private $requestContext; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param RequestContextAwareInterface|RequestContext|null $requestContext |
| 29 |
*/ |
| 30 |
public function __construct(RequestStack $requestStack, $requestContext = null) |
| 31 |
{ |
| 32 |
if (null !== $requestContext && !$requestContext instanceof RequestContext && !$requestContext instanceof RequestContextAwareInterface) { |
| 33 |
throw new \TypeError(__METHOD__.': Argument #2 ($requestContext) must of type Symfony\Component\Routing\RequestContextAwareInterface|Symfony\Component\Routing\RequestContext|null, '.get_debug_type($requestContext).' given.'); |
| 34 |
} |
| 35 |
|
| 36 |
$this->requestStack = $requestStack; |
| 37 |
$this->requestContext = $requestContext; |
| 38 |
} |
| 39 |
|
| 40 |
public function getAbsoluteUrl(string $path): string |
| 41 |
{ |
| 42 |
if (str_contains($path, '://') || '//' === substr($path, 0, 2)) { |
| 43 |
return $path; |
| 44 |
} |
| 45 |
|
| 46 |
if (null === $request = $this->requestStack->getMainRequest()) { |
| 47 |
return $this->getAbsoluteUrlFromContext($path); |
| 48 |
} |
| 49 |
|
| 50 |
if ('#' === $path[0]) { |
| 51 |
$path = $request->getRequestUri().$path; |
| 52 |
} elseif ('?' === $path[0]) { |
| 53 |
$path = $request->getPathInfo().$path; |
| 54 |
} |
| 55 |
|
| 56 |
if (!$path || '/' !== $path[0]) { |
| 57 |
$prefix = $request->getPathInfo(); |
| 58 |
$last = \strlen($prefix) - 1; |
| 59 |
if ($last !== $pos = strrpos($prefix, '/')) { |
| 60 |
$prefix = substr($prefix, 0, $pos).'/'; |
| 61 |
} |
| 62 |
|
| 63 |
return $request->getUriForPath($prefix.$path); |
| 64 |
} |
| 65 |
|
| 66 |
return $request->getSchemeAndHttpHost().$path; |
| 67 |
} |
| 68 |
|
| 69 |
public function getRelativePath(string $path): string |
| 70 |
{ |
| 71 |
if (str_contains($path, '://') || '//' === substr($path, 0, 2)) { |
| 72 |
return $path; |
| 73 |
} |
| 74 |
|
| 75 |
if (null === $request = $this->requestStack->getMainRequest()) { |
| 76 |
return $path; |
| 77 |
} |
| 78 |
|
| 79 |
return $request->getRelativeUriForPath($path); |
| 80 |
} |
| 81 |
|
| 82 |
private function getAbsoluteUrlFromContext(string $path): string |
| 83 |
{ |
| 84 |
if (null === $context = $this->requestContext) { |
| 85 |
return $path; |
| 86 |
} |
| 87 |
|
| 88 |
if ($context instanceof RequestContextAwareInterface) { |
| 89 |
$context = $context->getContext(); |
| 90 |
} |
| 91 |
|
| 92 |
if ('' === $host = $context->getHost()) { |
| 93 |
return $path; |
| 94 |
} |
| 95 |
|
| 96 |
$scheme = $context->getScheme(); |
| 97 |
$port = ''; |
| 98 |
|
| 99 |
if ('http' === $scheme && 80 !== $context->getHttpPort()) { |
| 100 |
$port = ':'.$context->getHttpPort(); |
| 101 |
} elseif ('https' === $scheme && 443 !== $context->getHttpsPort()) { |
| 102 |
$port = ':'.$context->getHttpsPort(); |
| 103 |
} |
| 104 |
|
| 105 |
if ('#' === $path[0]) { |
| 106 |
$queryString = $context->getQueryString(); |
| 107 |
$path = $context->getPathInfo().($queryString ? '?'.$queryString : '').$path; |
| 108 |
} elseif ('?' === $path[0]) { |
| 109 |
$path = $context->getPathInfo().$path; |
| 110 |
} |
| 111 |
|
| 112 |
if ('/' !== $path[0]) { |
| 113 |
$path = rtrim($context->getBaseUrl(), '/').'/'.$path; |
| 114 |
} |
| 115 |
|
| 116 |
return $scheme.'://'.$host.$port.$path; |
| 117 |
} |
| 118 |
} |
| 119 |
|