Skip to content

[Routing] Add possibility to create a request context with parameters directly #60120

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

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/Routing/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RequestContext
private string $queryString;
private array $parameters = [];

public function __construct(string $baseUrl = '', string $method = 'GET', string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443, string $path = '/', string $queryString = '')
public function __construct(string $baseUrl = '', string $method = 'GET', string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443, string $path = '/', string $queryString = '', array $parameters = [])
{
$this->setBaseUrl($baseUrl);
$this->setMethod($method);
Expand All @@ -43,6 +43,10 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string
$this->setHttpsPort($httpsPort);
$this->setPathInfo($path);
$this->setQueryString($queryString);

if ($parameters) {
$this->parameters = $parameters;
}
}

public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
Expand Down
19 changes: 18 additions & 1 deletion src/Symfony/Component/Routing/Tests/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function testConstruct()
8080,
444,
'/baz',
'bar=foobar'
'bar=foobar',
[
'foo' => 'bar',
]
);

$this->assertEquals('foo', $requestContext->getBaseUrl());
Expand All @@ -38,6 +41,20 @@ public function testConstruct()
$this->assertSame(444, $requestContext->getHttpsPort());
$this->assertEquals('/baz', $requestContext->getPathInfo());
$this->assertEquals('bar=foobar', $requestContext->getQueryString());
$this->assertEquals(['foo' => 'bar'], $requestContext->getParameters());
}

public function testConstructParametersBcLayer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what this test case is about

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its about why the if is needed for BC reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lines. As the require for the bc case in this tests, other tests wouldn't catch that.

+        if ($parameters) {
            $this->parameters = $parameters;
+        }

First I wanted to trigger deprecation so we could just remove it with 8.0.

{
$requestContext = new class() extends RequestContext {
public function __construct()
{
$this->setParameters(['foo' => 'bar']);
parent::__construct();
}
};

$this->assertEquals(['foo' => 'bar'], $requestContext->getParameters());
}

public function testFromUriWithBaseUrl()
Expand Down
Loading