Skip to content

Commit 71872b2

Browse files
Merge branch '5.4' into 6.2
* 5.4: [FrameworkBundle] Fix Workflow without a marking store definition uses marking store definition of previously defined workflow [HttpFoundation] UrlHelper is now aware of RequestContext changes UrlHelper is now aware of RequestContext changes [Process] Stop the process correctly even if underlying input stream is not closed: [Notifier] Update AmazonSns url in doc from de to en [PropertyInfo] Fix `PhpStanExtractor` when constructor has no docblock
2 parents 8eef8ec + 3c59f97 commit 71872b2

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Tests/UrlHelperTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpFoundation\UrlHelper;
1818
use Symfony\Component\Routing\RequestContext;
19+
use Symfony\Component\Routing\RequestContextAwareInterface;
1920

2021
class UrlHelperTest extends TestCase
2122
{
@@ -64,11 +65,46 @@ public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host
6465
}
6566

6667
$requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
68+
6769
$helper = new UrlHelper(new RequestStack(), $requestContext);
6870

6971
$this->assertEquals($expected, $helper->getAbsoluteUrl($path));
7072
}
7173

74+
/**
75+
* @dataProvider getGenerateAbsoluteUrlRequestContextData
76+
*/
77+
public function testGenerateAbsoluteUrlWithRequestContextAwareInterface($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
78+
{
79+
if (!class_exists(RequestContext::class)) {
80+
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
81+
}
82+
83+
$requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
84+
$contextAware = new class($requestContext) implements RequestContextAwareInterface {
85+
private $requestContext;
86+
87+
public function __construct($requestContext)
88+
{
89+
$this->requestContext = $requestContext;
90+
}
91+
92+
public function setContext(RequestContext $context)
93+
{
94+
$this->requestContext = $context;
95+
}
96+
97+
public function getContext()
98+
{
99+
return $this->requestContext;
100+
}
101+
};
102+
103+
$helper = new UrlHelper(new RequestStack(), $contextAware);
104+
105+
$this->assertEquals($expected, $helper->getAbsoluteUrl($path));
106+
}
107+
72108
/**
73109
* @dataProvider getGenerateAbsoluteUrlRequestContextData
74110
*/

UrlHelper.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation;
1313

1414
use Symfony\Component\Routing\RequestContext;
15+
use Symfony\Component\Routing\RequestContextAwareInterface;
1516

1617
/**
1718
* A helper service for manipulating URLs within and outside the request scope.
@@ -20,13 +21,11 @@
2021
*/
2122
final class UrlHelper
2223
{
23-
private RequestStack $requestStack;
24-
private ?RequestContext $requestContext;
2524

26-
public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
27-
{
28-
$this->requestStack = $requestStack;
29-
$this->requestContext = $requestContext;
25+
public function __construct(
26+
private RequestStack $requestStack,
27+
private RequestContextAwareInterface|RequestContext|null $requestContext = null,
28+
) {
3029
}
3130

3231
public function getAbsoluteUrl(string $path): string
@@ -73,28 +72,36 @@ public function getRelativePath(string $path): string
7372

7473
private function getAbsoluteUrlFromContext(string $path): string
7574
{
76-
if (null === $this->requestContext || '' === $host = $this->requestContext->getHost()) {
75+
if (null === $context = $this->requestContext) {
76+
return $path;
77+
}
78+
79+
if ($context instanceof RequestContextAwareInterface) {
80+
$context = $context->getContext();
81+
}
82+
83+
if ('' === $host = $context->getHost()) {
7784
return $path;
7885
}
7986

80-
$scheme = $this->requestContext->getScheme();
87+
$scheme = $context->getScheme();
8188
$port = '';
8289

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();
90+
if ('http' === $scheme && 80 !== $context->getHttpPort()) {
91+
$port = ':'.$context->getHttpPort();
92+
} elseif ('https' === $scheme && 443 !== $context->getHttpsPort()) {
93+
$port = ':'.$context->getHttpsPort();
8794
}
8895

8996
if ('#' === $path[0]) {
90-
$queryString = $this->requestContext->getQueryString();
91-
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
97+
$queryString = $context->getQueryString();
98+
$path = $context->getPathInfo().($queryString ? '?'.$queryString : '').$path;
9299
} elseif ('?' === $path[0]) {
93-
$path = $this->requestContext->getPathInfo().$path;
100+
$path = $context->getPathInfo().$path;
94101
}
95102

96103
if ('/' !== $path[0]) {
97-
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
104+
$path = rtrim($context->getBaseUrl(), '/').'/'.$path;
98105
}
99106

100107
return $scheme.'://'.$host.$port.$path;

0 commit comments

Comments
 (0)