Skip to content

Commit 5ef1ccb

Browse files
committed
feature symfony#60120 [Routing] Add possibility to create a request context with parameters directly (alexander-schranz)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Routing] Add possibility to create a request context with parameters directly | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes<!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT While I worked on the route integration into Sulu. I had to write a few tests for depending on the RequestContext. To improve the DX I think the parameters should be possible also inside the construct directly so instead of: ```php $requestContext = new RequestContext(); $requestContext->setParameters(['foo' => 'bar']); $service->myMethod($requestContext); ``` I can do: ```php $service->myMethod(new RequestContext(parameters: ['foo' => 'bar'])); ``` Commits ------- a3d3cb5 [Routing] Add possibility to create a request context with parameters directly
2 parents e2e4800 + a3d3cb5 commit 5ef1ccb

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
@@ -28,7 +28,10 @@ public function testConstruct()
2828
8080,
2929
444,
3030
'/baz',
31-
'bar=foobar'
31+
'bar=foobar',
32+
[
33+
'foo' => 'bar',
34+
]
3235
);
3336

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

4461
public function testFromUriWithBaseUrl()

0 commit comments

Comments
 (0)