12
12
namespace Symfony \Component \HttpFoundation ;
13
13
14
14
use Symfony \Component \Routing \RequestContext ;
15
+ use Symfony \Component \Routing \RequestContextAwareInterface ;
15
16
16
17
/**
17
18
* A helper service for manipulating URLs within and outside the request scope.
@@ -23,8 +24,15 @@ final class UrlHelper
23
24
private $ requestStack ;
24
25
private $ requestContext ;
25
26
26
- public function __construct (RequestStack $ requestStack , RequestContext $ requestContext = null )
27
+ /**
28
+ * @param RequestContextAwareInterface|RequestContext|null $requestContext
29
+ */
30
+ public function __construct (RequestStack $ requestStack , $ requestContext = null )
27
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
+
28
36
$ this ->requestStack = $ requestStack ;
29
37
$ this ->requestContext = $ requestContext ;
30
38
}
@@ -73,28 +81,36 @@ public function getRelativePath(string $path): string
73
81
74
82
private function getAbsoluteUrlFromContext (string $ path ): string
75
83
{
76
- if (null === $ this ->requestContext || '' === $ host = $ this ->requestContext ->getHost ()) {
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 ()) {
77
93
return $ path ;
78
94
}
79
95
80
- $ scheme = $ this -> requestContext ->getScheme ();
96
+ $ scheme = $ context ->getScheme ();
81
97
$ port = '' ;
82
98
83
- if ('http ' === $ scheme && 80 !== $ this -> requestContext ->getHttpPort ()) {
84
- $ port = ': ' .$ this -> requestContext ->getHttpPort ();
85
- } elseif ('https ' === $ scheme && 443 !== $ this -> requestContext ->getHttpsPort ()) {
86
- $ port = ': ' .$ this -> requestContext ->getHttpsPort ();
99
+ if ('http ' === $ scheme && 80 !== $ context ->getHttpPort ()) {
100
+ $ port = ': ' .$ context ->getHttpPort ();
101
+ } elseif ('https ' === $ scheme && 443 !== $ context ->getHttpsPort ()) {
102
+ $ port = ': ' .$ context ->getHttpsPort ();
87
103
}
88
104
89
105
if ('# ' === $ path [0 ]) {
90
- $ queryString = $ this -> requestContext ->getQueryString ();
91
- $ path = $ this -> requestContext ->getPathInfo ().($ queryString ? '? ' .$ queryString : '' ).$ path ;
106
+ $ queryString = $ context ->getQueryString ();
107
+ $ path = $ context ->getPathInfo ().($ queryString ? '? ' .$ queryString : '' ).$ path ;
92
108
} elseif ('? ' === $ path [0 ]) {
93
- $ path = $ this -> requestContext ->getPathInfo ().$ path ;
109
+ $ path = $ context ->getPathInfo ().$ path ;
94
110
}
95
111
96
112
if ('/ ' !== $ path [0 ]) {
97
- $ path = rtrim ($ this -> requestContext ->getBaseUrl (), '/ ' ).'/ ' .$ path ;
113
+ $ path = rtrim ($ context ->getBaseUrl (), '/ ' ).'/ ' .$ path ;
98
114
}
99
115
100
116
return $ scheme .':// ' .$ host .$ port .$ path ;
0 commit comments