PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / symfony / http-foundation / UrlHelper.php

UrlHelper.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/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 <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