Skip to content

Commit

Permalink
Remove SymfonyHttpClient request method if http client is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas authored and karser committed Jan 9, 2024
1 parent 52b4d88 commit 723f215
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
18 changes: 18 additions & 0 deletions DependencyInjection/Compiler/UseRequestMethodPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Karser\Recaptcha3Bundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class UseRequestMethodPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->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');
}
}
}
7 changes: 0 additions & 7 deletions DependencyInjection/KarserRecaptcha3Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions KarserRecaptcha3Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 723f215

Please sign in to comment.