Skip to content

[FrameworkBundle][HttpFoundation] Reset Request's formats using the service resetter #59403

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
Jan 9, 2025
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 @@ -284,7 +284,9 @@ private function findProperServiceName(InputInterface $input, SymfonyStyle $io,
return $matchingServices[0];
}

return $io->choice('Select one of the following services to display its information', $matchingServices);
natsort($matchingServices);

return $io->choice('Select one of the following services to display its information', array_values($matchingServices));
}

private function findProperTagName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $container, string $tagName): string
Expand All @@ -302,7 +304,9 @@ private function findProperTagName(InputInterface $input, SymfonyStyle $io, Cont
return $matchingTags[0];
}

return $io->choice('Select one of the following tags to display its information', $matchingTags);
natsort($matchingTags);

return $io->choice('Select one of the following tags to display its information', array_values($matchingTags));
}

private function findServiceIdsContaining(ContainerBuilder $container, string $name, bool $showHidden): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
->alias(HttpKernelInterface::class, 'http_kernel')

->set('request_stack', RequestStack::class)
->tag('kernel.reset', ['method' => 'resetRequestFormats', 'on_invalid' => 'ignore'])
->public()
->alias(RequestStack::class, 'request_stack')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ public function testTagsPartialSearch()
$tester->run(['command' => 'debug:container', '--tag' => 'kernel.'], ['decorated' => false]);

$this->assertStringContainsString('Select one of the following tags to display its information', $tester->getDisplay());
$this->assertStringContainsString('[0] kernel.event_subscriber', $tester->getDisplay());
$this->assertStringContainsString('[1] kernel.locale_aware', $tester->getDisplay());
$this->assertStringContainsString('[2] kernel.cache_warmer', $tester->getDisplay());
$this->assertStringContainsString('[0] kernel.cache_clearer', $tester->getDisplay());
$this->assertStringContainsString('[1] kernel.cache_warmer', $tester->getDisplay());
$this->assertStringContainsString('[2] kernel.event_subscriber', $tester->getDisplay());
$this->assertStringContainsString('[3] kernel.fragment_renderer', $tester->getDisplay());
$this->assertStringContainsString('[4] kernel.reset', $tester->getDisplay());
$this->assertStringContainsString('[5] kernel.cache_clearer', $tester->getDisplay());
$this->assertStringContainsString('Symfony Container Services Tagged with "kernel.event_subscriber" Tag', $tester->getDisplay());
$this->assertStringContainsString('[4] kernel.locale_aware', $tester->getDisplay());
$this->assertStringContainsString('[5] kernel.reset', $tester->getDisplay());
$this->assertStringContainsString('Symfony Container Services Tagged with "kernel.cache_clearer" Tag', $tester->getDisplay());
}

public function testDescribeEnvVars()
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/RequestStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@

throw new SessionNotFoundException();
}

public function resetRequestFormats(): void
{
static $resetRequestFormats;
$resetRequestFormats ??= \Closure::bind(static fn () => self::$formats = null, null, Request::class);

Check failure on line 113 in src/Symfony/Component/HttpFoundation/RequestStack.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedPropertyFetch

src/Symfony/Component/HttpFoundation/RequestStack.php:113:65: UndefinedPropertyFetch: Static property Symfony\Component\HttpFoundation\RequestStack::$formats is not defined (see https://psalm.dev/039)

Check failure on line 113 in src/Symfony/Component/HttpFoundation/RequestStack.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedPropertyAssignment

src/Symfony/Component/HttpFoundation/RequestStack.php:113:65: UndefinedPropertyAssignment: Static property Symfony\Component\HttpFoundation\RequestStack::$formats is not defined (see https://psalm.dev/038)

Check failure on line 113 in src/Symfony/Component/HttpFoundation/RequestStack.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedPropertyFetch

src/Symfony/Component/HttpFoundation/RequestStack.php:113:65: UndefinedPropertyFetch: Static property Symfony\Component\HttpFoundation\RequestStack::$formats is not defined (see https://psalm.dev/039)

Check failure on line 113 in src/Symfony/Component/HttpFoundation/RequestStack.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedPropertyAssignment

src/Symfony/Component/HttpFoundation/RequestStack.php:113:65: UndefinedPropertyAssignment: Static property Symfony\Component\HttpFoundation\RequestStack::$formats is not defined (see https://psalm.dev/038)
$resetRequestFormats();
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ public function testGetParentRequest()
$requestStack->push($secondSubRequest);
$this->assertSame($firstSubRequest, $requestStack->getParentRequest());
}

public function testResetRequestFormats()
{
$requestStack = new RequestStack();

$request = Request::create('/foo');
$request->setFormat('foo', ['application/foo']);

$this->assertSame(['application/foo'], $request->getMimeTypes('foo'));

$requestStack->resetRequestFormats();

$this->assertSame([], $request->getMimeTypes('foo'));
}
}
Loading