diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php index ca9d8894a2150..ba70b90ad654b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php @@ -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') diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 3acea2838d515..129201e3c3733 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php index 0cd8e6d7c935c..2e4cd69270da5 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/ResettableServicePass.php @@ -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']; } } diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/ServicesResetter.php b/src/Symfony/Component/HttpKernel/DependencyInjection/ServicesResetter.php index 59e681a9bb34a..0063deca361bf 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/ServicesResetter.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/ServicesResetter.php @@ -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(); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php index 4c110e3a26800..aa43e2ebb1d6b 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/ResettableServicePassTest.php @@ -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();