Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SymfonyHttpClient request method if http client is not available #68

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@norkunas could you please clarify defining the alias in the extension vs compiler?

} 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());
}
}