Skip to content

Commit a86dc99

Browse files
committed
Do not match request twice in HttpUtils
1 parent 912a75c commit a86dc99

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

HttpUtils.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public function createRequest(Request $request, string $path)
118118
public function checkRequestPath(Request $request, string $path)
119119
{
120120
if ('/' !== $path[0]) {
121+
// Shortcut if request has already been matched before
122+
if ($request->attributes->has('_route')) {
123+
return $path === $request->attributes->get('_route');
124+
}
125+
121126
try {
122127
// matching a request is more powerful than matching a URL path + context, so try that first
123128
if ($this->urlMatcher instanceof RequestMatcherInterface) {

Tests/HttpUtilsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,22 @@ public function testCheckRequestPathWithUrlMatcherLoadingException()
318318
$utils->checkRequestPath($this->getRequest(), 'foobar');
319319
}
320320

321+
public function testCheckRequestPathWithRequestAlreadyMatchedBefore()
322+
{
323+
$urlMatcher = $this->createMock(RequestMatcherInterface::class);
324+
$urlMatcher
325+
->expects($this->never())
326+
->method('matchRequest')
327+
;
328+
329+
$request = $this->getRequest();
330+
$request->attributes->set('_route', 'route_name');
331+
332+
$utils = new HttpUtils(null, $urlMatcher);
333+
$this->assertTrue($utils->checkRequestPath($request, 'route_name'));
334+
$this->assertFalse($utils->checkRequestPath($request, 'foobar'));
335+
}
336+
321337
public function testCheckPathWithoutRouteParam()
322338
{
323339
$urlMatcher = $this->createMock(UrlMatcherInterface::class);

0 commit comments

Comments
 (0)