Skip to content

Commit ad260a4

Browse files
[HttpKernel] allow ignoring kernel.reset methods that don't exist
1 parent 91ee870 commit ad260a4

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
99
* Deprecate the `fileLinkFormat` parameter of `DebugHandlersListener`
1010
* Add support for configuring log level, and status code by exception class
11+
* Allow ignoring "kernel.reset" methods that don't exist with "on_invalid" attribute
1112

1213
5.3
1314
---

DependencyInjection/ResettableServicePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function process(ContainerBuilder $container)
5757
$methods[$id] = [];
5858
}
5959

60+
if ('ignore' === ($attributes['on_invalid'] ?? null)) {
61+
$attributes['method'] = '?'.$attributes['method'];
62+
}
63+
6064
$methods[$id][] = $attributes['method'];
6165
}
6266
}

DependencyInjection/ServicesResetter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function reset()
4040
{
4141
foreach ($this->resettableServices as $id => $service) {
4242
foreach ((array) $this->resetMethods[$id] as $resetMethod) {
43+
if ('?' === $resetMethod[0] && !method_exists($service, $resetMethod = substr($resetMethod, 1))) {
44+
continue;
45+
}
46+
4347
$service->$resetMethod();
4448
}
4549
}

Tests/DependencyInjection/ResettableServicePassTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ public function testMissingMethod()
7070
$container->compile();
7171
}
7272

73+
public function testIgnoreInvalidMethod()
74+
{
75+
$container = new ContainerBuilder();
76+
$container->register(ResettableService::class)
77+
->setPublic(true)
78+
->addTag('kernel.reset', ['method' => 'missingMethod', 'on_invalid' => 'ignore']);
79+
$container->register('services_resetter', ServicesResetter::class)
80+
->setPublic(true)
81+
->setArguments([null, []]);
82+
$container->addCompilerPass(new ResettableServicePass());
83+
84+
$container->compile();
85+
86+
$this->assertSame([ResettableService::class => ['?missingMethod']], $container->getDefinition('services_resetter')->getArgument(1));
87+
88+
$resettable = $container->get(ResettableService::class);
89+
$resetter = $container->get('services_resetter');
90+
$resetter->reset();
91+
}
92+
7393
public function testCompilerPassWithoutResetters()
7494
{
7595
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)