Skip to content

[HttpFoundation] UrlHelper is now aware of RequestContext changes #50309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
->set('url_helper', UrlHelper::class)
->args([
service('request_stack'),
service('router.request_context')->ignoreOnInvalid(),
service('router')->ignoreOnInvalid(),
])
->alias(UrlHelper::class, 'url_helper')

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/event-dispatcher": "^5.1|^6.0",
"symfony/error-handler": "^4.4.1|^5.0.1|^6.0",
"symfony/http-foundation": "^5.3|^6.0",
"symfony/http-foundation": "^5.4.24|^6.2.11",
"symfony/http-kernel": "^5.4|^6.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.16",
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\UrlHelper;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;

class UrlHelperTest extends TestCase
{
Expand Down Expand Up @@ -69,6 +70,40 @@ public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host
$this->assertEquals($expected, $helper->getAbsoluteUrl($path));
}

/**
* @dataProvider getGenerateAbsoluteUrlRequestContextData
*/
public function testGenerateAbsoluteUrlWithRequestContextAwareInterface($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
{
if (!class_exists(RequestContext::class)) {
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
}

$requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
$contextAware = new class($requestContext) implements RequestContextAwareInterface {
private $requestContext;

public function __construct($requestContext)
{
$this->requestContext = $requestContext;
}

public function setContext(RequestContext $context)
{
$this->requestContext = $context;
}

public function getContext()
{
return $this->requestContext;
}
};

$helper = new UrlHelper(new RequestStack(), $contextAware);

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

/**
* @dataProvider getGenerateAbsoluteUrlRequestContextData
*/
Expand Down
38 changes: 27 additions & 11 deletions src/Symfony/Component/HttpFoundation/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation;

use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;

/**
* A helper service for manipulating URLs within and outside the request scope.
Expand All @@ -23,8 +24,15 @@ final class UrlHelper
private $requestStack;
private $requestContext;

public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
/**
* @param RequestContextAwareInterface|RequestContext|null $requestContext
*/
public function __construct(RequestStack $requestStack, $requestContext = null)
{
if (null !== $requestContext && !$requestContext instanceof RequestContext && !$requestContext instanceof RequestContextAwareInterface) {
throw new \TypeError(__METHOD__.': Argument #2 ($requestContext) must of type Symfony\Component\Routing\RequestContextAwareInterface|Symfony\Component\Routing\RequestContext|null, '.get_debug_type($requestContext).' given.');
}

$this->requestStack = $requestStack;
$this->requestContext = $requestContext;
}
Expand Down Expand Up @@ -73,28 +81,36 @@ public function getRelativePath(string $path): string

private function getAbsoluteUrlFromContext(string $path): string
{
if (null === $this->requestContext || '' === $host = $this->requestContext->getHost()) {
if (null === $context = $this->requestContext) {
return $path;
}

if ($context instanceof RequestContextAwareInterface) {
$context = $context->getContext();
}

if ('' === $host = $context->getHost()) {
return $path;
}

$scheme = $this->requestContext->getScheme();
$scheme = $context->getScheme();
$port = '';

if ('http' === $scheme && 80 !== $this->requestContext->getHttpPort()) {
$port = ':'.$this->requestContext->getHttpPort();
} elseif ('https' === $scheme && 443 !== $this->requestContext->getHttpsPort()) {
$port = ':'.$this->requestContext->getHttpsPort();
if ('http' === $scheme && 80 !== $context->getHttpPort()) {
$port = ':'.$context->getHttpPort();
} elseif ('https' === $scheme && 443 !== $context->getHttpsPort()) {
$port = ':'.$context->getHttpsPort();
}

if ('#' === $path[0]) {
$queryString = $this->requestContext->getQueryString();
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
$queryString = $context->getQueryString();
$path = $context->getPathInfo().($queryString ? '?'.$queryString : '').$path;
} elseif ('?' === $path[0]) {
$path = $this->requestContext->getPathInfo().$path;
$path = $context->getPathInfo().$path;
}

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

return $scheme.'://'.$host.$port.$path;
Expand Down