UrlHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\Routing\RequestContext;
  12. /**
  13. * A helper service for manipulating URLs within and outside the request scope.
  14. *
  15. * @author Valentin Udaltsov <udaltsov.valentin@gmail.com>
  16. */
  17. final class UrlHelper
  18. {
  19. private $requestStack;
  20. private $requestContext;
  21. public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
  22. {
  23. $this->requestStack = $requestStack;
  24. $this->requestContext = $requestContext;
  25. }
  26. public function getAbsoluteUrl(string $path): string
  27. {
  28. if (str_contains($path, '://') || '//' === substr($path, 0, 2)) {
  29. return $path;
  30. }
  31. if (null === $request = $this->requestStack->getMasterRequest()) {
  32. return $this->getAbsoluteUrlFromContext($path);
  33. }
  34. if ('#' === $path[0]) {
  35. $path = $request->getRequestUri().$path;
  36. } elseif ('?' === $path[0]) {
  37. $path = $request->getPathInfo().$path;
  38. }
  39. if (!$path || '/' !== $path[0]) {
  40. $prefix = $request->getPathInfo();
  41. $last = \strlen($prefix) - 1;
  42. if ($last !== $pos = strrpos($prefix, '/')) {
  43. $prefix = substr($prefix, 0, $pos).'/';
  44. }
  45. return $request->getUriForPath($prefix.$path);
  46. }
  47. return $request->getSchemeAndHttpHost().$path;
  48. }
  49. public function getRelativePath(string $path): string
  50. {
  51. if (str_contains($path, '://') || '//' === substr($path, 0, 2)) {
  52. return $path;
  53. }
  54. if (null === $request = $this->requestStack->getMasterRequest()) {
  55. return $path;
  56. }
  57. return $request->getRelativeUriForPath($path);
  58. }
  59. private function getAbsoluteUrlFromContext(string $path): string
  60. {
  61. if (null === $this->requestContext || '' === $host = $this->requestContext->getHost()) {
  62. return $path;
  63. }
  64. $scheme = $this->requestContext->getScheme();
  65. $port = '';
  66. if ('http' === $scheme && 80 !== $this->requestContext->getHttpPort()) {
  67. $port = ':'.$this->requestContext->getHttpPort();
  68. } elseif ('https' === $scheme && 443 !== $this->requestContext->getHttpsPort()) {
  69. $port = ':'.$this->requestContext->getHttpsPort();
  70. }
  71. if ('#' === $path[0]) {
  72. $queryString = $this->requestContext->getQueryString();
  73. $path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
  74. } elseif ('?' === $path[0]) {
  75. $path = $this->requestContext->getPathInfo().$path;
  76. }
  77. if ('/' !== $path[0]) {
  78. $path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
  79. }
  80. return $scheme.'://'.$host.$port.$path;
  81. }
  82. }