Skip to content

Commit 723f215

Browse files
norkunaskarser
authored andcommitted
Remove SymfonyHttpClient request method if http client is not available
1 parent 52b4d88 commit 723f215

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Karser\Recaptcha3Bundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
final class UseRequestMethodPass implements CompilerPassInterface
9+
{
10+
public function process(ContainerBuilder $container): void
11+
{
12+
if ($container->hasDefinition('http_client')) {
13+
$container->setAlias('karser_recaptcha3.google.request_method', 'karser_recaptcha3.request_method.symfony_http_client');
14+
} else {
15+
$container->removeDefinition('karser_recaptcha3.request_method.symfony_http_client');
16+
}
17+
}
18+
}

DependencyInjection/KarserRecaptcha3Extension.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
88
use Symfony\Component\DependencyInjection\Loader;
99
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
10-
use Symfony\Contracts\HttpClient\HttpClientInterface;
1110

1211
class KarserRecaptcha3Extension extends ConfigurableExtension implements PrependExtensionInterface
1312
{
@@ -18,12 +17,6 @@ public function loadInternal(array $configs, ContainerBuilder $container): void
1817
foreach ($configs as $key => $value) {
1918
$container->setParameter('karser_recaptcha3.'.$key, $value);
2019
}
21-
22-
if (interface_exists(HttpClientInterface::class)) {
23-
$container->setAlias('karser_recaptcha3.google.request_method', 'karser_recaptcha3.request_method.symfony_http_client');
24-
} else {
25-
$container->removeDefinition('karser_recaptcha3.request_method.symfony_http_client');
26-
}
2720
}
2821

2922
public function prepend(ContainerBuilder $container): void

KarserRecaptcha3Bundle.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
namespace Karser\Recaptcha3Bundle;
44

5+
use Karser\Recaptcha3Bundle\DependencyInjection\Compiler\UseRequestMethodPass;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
57
use Symfony\Component\HttpKernel\Bundle\Bundle;
68

79
class KarserRecaptcha3Bundle extends Bundle
810
{
11+
public function build(ContainerBuilder $container): void
12+
{
13+
parent::build($container);
914

15+
$container->addCompilerPass(new UseRequestMethodPass());
16+
}
1017
}

0 commit comments

Comments
 (0)