Skip to content

[HttpKernel] allow ignoring kernel.reset methods that don't exist #43983

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
Nov 9, 2021
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 @@ -30,6 +30,7 @@
])
->call('setLogger', [service('logger')->ignoreOnInvalid()])
->tag('monolog.logger', ['channel' => 'http_client'])
->tag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
->tag('http_client.client')

->alias(HttpClientInterface::class, 'http_client')
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
* Deprecate the `fileLinkFormat` parameter of `DebugHandlersListener`
* Add support for configuring log level, and status code by exception class
* Allow ignoring "kernel.reset" methods that don't exist with "on_invalid" attribute

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function process(ContainerBuilder $container)
$methods[$id] = [];
}

if ('ignore' === ($attributes['on_invalid'] ?? null)) {
$attributes['method'] = '?'.$attributes['method'];
}

$methods[$id][] = $attributes['method'];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function reset()
{
foreach ($this->resettableServices as $id => $service) {
foreach ((array) $this->resetMethods[$id] as $resetMethod) {
if ('?' === $resetMethod[0] && !method_exists($service, $resetMethod = substr($resetMethod, 1))) {
continue;
}

$service->$resetMethod();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ public function testMissingMethod()
$container->compile();
}

public function testIgnoreInvalidMethod()
{
$container = new ContainerBuilder();
$container->register(ResettableService::class)
->setPublic(true)
->addTag('kernel.reset', ['method' => 'missingMethod', 'on_invalid' => 'ignore']);
$container->register('services_resetter', ServicesResetter::class)
->setPublic(true)
->setArguments([null, []]);
$container->addCompilerPass(new ResettableServicePass());

$container->compile();

$this->assertSame([ResettableService::class => ['?missingMethod']], $container->getDefinition('services_resetter')->getArgument(1));

$resettable = $container->get(ResettableService::class);
$resetter = $container->get('services_resetter');
$resetter->reset();
}

public function testCompilerPassWithoutResetters()
{
$container = new ContainerBuilder();
Expand Down