Skip to content

Commit a3d3cb5

Browse files
alexander-schranzfabpot
authored andcommitted
[Routing] Add possibility to create a request context with parameters directly
1 parent 5add177 commit a3d3cb5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RequestContext
3333
private string $queryString;
3434
private array $parameters = [];
3535

36-
public function __construct(string $baseUrl = '', string $method = 'GET', string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443, string $path = '/', string $queryString = '')
36+
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 = null)
3737
{
3838
$this->setBaseUrl($baseUrl);
3939
$this->setMethod($method);
@@ -43,6 +43,7 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string
4343
$this->setHttpsPort($httpsPort);
4444
$this->setPathInfo($path);
4545
$this->setQueryString($queryString);
46+
$this->parameters = $parameters ?? $this->parameters;
4647
}
4748

4849
public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self

src/Symfony/Component/Routing/Tests/RequestContextTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public function testConstruct()
2727
8080,
2828
444,
2929
'/baz',
30-
'bar=foobar'
30+
'bar=foobar',
31+
[
32+
'foo' => 'bar',
33+
]
3134
);
3235

3336
$this->assertEquals('foo', $requestContext->getBaseUrl());
@@ -38,6 +41,20 @@ public function testConstruct()
3841
$this->assertSame(444, $requestContext->getHttpsPort());
3942
$this->assertEquals('/baz', $requestContext->getPathInfo());
4043
$this->assertEquals('bar=foobar', $requestContext->getQueryString());
44+
$this->assertSame(['foo' => 'bar'], $requestContext->getParameters());
45+
}
46+
47+
public function testConstructParametersBcLayer()
48+
{
49+
$requestContext = new class() extends RequestContext {
50+
public function __construct()
51+
{
52+
$this->setParameters(['foo' => 'bar']);
53+
parent::__construct();
54+
}
55+
};
56+
57+
$this->assertSame(['foo' => 'bar'], $requestContext->getParameters());
4158
}
4259

4360
public function testFromUriWithBaseUrl()

0 commit comments

Comments
 (0)