From 820648fb04cab9525b1ca9bc4a344da211bc207f Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 1 Dec 2023 11:54:05 +0200 Subject: [PATCH] Remove SymfonyHttpClient request method if http client is not available --- .../Compiler/UseRequestMethodPass.php | 18 ++++++++++++++++++ .../KarserRecaptcha3Extension.php | 7 ------- KarserRecaptcha3Bundle.php | 7 +++++++ 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 DependencyInjection/Compiler/UseRequestMethodPass.php diff --git a/DependencyInjection/Compiler/UseRequestMethodPass.php b/DependencyInjection/Compiler/UseRequestMethodPass.php new file mode 100644 index 0000000..35bcade --- /dev/null +++ b/DependencyInjection/Compiler/UseRequestMethodPass.php @@ -0,0 +1,18 @@ +hasDefinition('http_client')) { + $container->setAlias('karser_recaptcha3.google.request_method', 'karser_recaptcha3.request_method.symfony_http_client'); + } else { + $container->removeDefinition('karser_recaptcha3.request_method.symfony_http_client'); + } + } +} diff --git a/DependencyInjection/KarserRecaptcha3Extension.php b/DependencyInjection/KarserRecaptcha3Extension.php index 703fa7d..c0002cf 100644 --- a/DependencyInjection/KarserRecaptcha3Extension.php +++ b/DependencyInjection/KarserRecaptcha3Extension.php @@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; -use Symfony\Contracts\HttpClient\HttpClientInterface; class KarserRecaptcha3Extension extends ConfigurableExtension implements PrependExtensionInterface { @@ -18,12 +17,6 @@ public function loadInternal(array $configs, ContainerBuilder $container): void foreach ($configs as $key => $value) { $container->setParameter('karser_recaptcha3.'.$key, $value); } - - if (interface_exists(HttpClientInterface::class)) { - $container->setAlias('karser_recaptcha3.google.request_method', 'karser_recaptcha3.request_method.symfony_http_client'); - } else { - $container->removeDefinition('karser_recaptcha3.request_method.symfony_http_client'); - } } public function prepend(ContainerBuilder $container): void diff --git a/KarserRecaptcha3Bundle.php b/KarserRecaptcha3Bundle.php index 61c0277..1d1d192 100644 --- a/KarserRecaptcha3Bundle.php +++ b/KarserRecaptcha3Bundle.php @@ -2,9 +2,16 @@ namespace Karser\Recaptcha3Bundle; +use Karser\Recaptcha3Bundle\DependencyInjection\Compiler\UseRequestMethodPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class KarserRecaptcha3Bundle extends Bundle { + public function build(ContainerBuilder $container): void + { + parent::build($container); + $container->addCompilerPass(new UseRequestMethodPass()); + } }