PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / vendor / vendor-prefixed / symfony / http-foundation / UrlHelper.php

UrlHelper.php in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at vendor/vendor-prefixed/symfony/http-foundation/UrlHelper.php

119 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 Give\Vendors\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 <[email protected]>
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