8000 [HttpFoundation] UrlHelper is now aware of RequestContext changes · weaverryan/symfony@75eb469 · GitHub
[go: up one dir, main page]

Skip to content

Commit 75eb469

Browse files
giosh94mhznicolas-grekas
authored andcommitted
[HttpFoundation] UrlHelper is now aware of RequestContext changes
1 parent 6166fc4 commit 75eb469

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
110110
->set('url_helper', UrlHelper::class)
111111
->args([
112112
service('request_stack'),
113-
service('router.request_context')->ignoreOnInvalid(),
113+
service('router')->ignoreOnInvalid(),
114114
])
115115
->alias(UrlHelper::class, 'url_helper')
116116

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/deprecation-contracts": "^2.1|^3",
2525
"symfony/event-dispatcher": "^5.1|^6.0",
2626
"symfony/error-handler": "^4.4.1|^5.0.1|^6.0",
27-
"symfony/http-foundation": "^5.3|^6.0",
27+
"symfony/http-foundation": "^5.4.24|^6.2.11",
2828
"symfony/http-kernel": "^5.4|^6.0",
2929
"symfony/polyfill-mbstring": "~1.0",
3030
"symfony/polyfill-php80": "^1.16",

src/Symfony/Component/HttpFoundation/Tests/UrlHelperTest.php

Lines changed: 35 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
{
@@ -69,6 +70,40 @@ public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host
6970
$this->assertEquals($expected, $helper->getAbsoluteUrl($path));
7071
}
7172

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

src/Symfony/Component/HttpFoundation/UrlHelper.php

Lines changed: 27 additions & 11 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.
@@ -23,8 +24,15 @@ final class UrlHelper
2324
private $requestStack;
2425
private $requestContext;
2526

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)
2731
{
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+
2836
$this->requestStack = $requestStack;
2937
$this->requestContext = $requestContext;
3038
}
@@ -73,28 +81,36 @@ public function getRelativePath(string $path): string
7381

7482
private function getAbsoluteUrlFromContext(string $path): string
7583
{
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()) {
7793
return $path;
7894
}
7995

80-
$scheme = $this->requestContext->getScheme();
96+
$scheme = $context->getScheme();
8197
$port = '';
8298

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();
87103
}
88104

89105
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;
92108
} elseif ('?' === $path[0]) {
93-
$path = $this->requestContext->getPathInfo().$path;
109+
$path = $context->getPathInfo().$path;
94110
}
95111

96112
if ('/' !== $path[0]) {
97-
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
113+
$path = rtrim($context->getBaseUrl(), '/').'/'.$path;
98114
}
99115

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

0 commit comments

Comments
 (0)
0