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

[EasyHttpClient] Add Symfony config to decorate default http client #628

Merged
merged 1 commit into from
Jul 5, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ interface BridgeConstantsInterface
* @var string
*/
public const LOG_CHANNEL = 'http_client';

/**
* @var string
*/
public const PARAM_DECORATE_DEFAULT_CLIENT = 'easy_http_client.decorate_default_client';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace EonX\EasyHttpClient\Bridge\Symfony\DependencyInjection\Compiler;

use EonX\EasyHttpClient\Bridge\BridgeConstantsInterface;
use EonX\EasyHttpClient\Implementations\Symfony\WithEventsHttpClient;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

final class DecorateDefaultClientPass implements CompilerPassInterface
{
/**
* @var string
*/
private const DEFAULT_CLIENT_ID = 'http_client';

public function process(ContainerBuilder $container): void
{
// Apply only if default client definition exists
if ($container->has(self::DEFAULT_CLIENT_ID) === false) {
return;
}

// Apply only if configured to do so
if ($this->isEnabled($container) === false) {
return;
}

$def = (new Definition(WithEventsHttpClient::class))
->setAutowired(true)
->setAutoconfigured(true)
->setDecoratedService(self::DEFAULT_CLIENT_ID);

$container->setDefinition(WithEventsHttpClient::class, $def);
}

private function isEnabled(ContainerBuilder $container): bool
{
if ($container->hasParameter(BridgeConstantsInterface::PARAM_DECORATE_DEFAULT_CLIENT) === false) {
return false;
}

return (bool)$container->getParameter(BridgeConstantsInterface::PARAM_DECORATE_DEFAULT_CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function getConfigTreeBuilder(): TreeBuilder

$treeBuilder->getRootNode()
->children()
->booleanNode('decorate_default_client')->defaultFalse()->end()
->booleanNode('easy_bugsnag_enabled')->defaultTrue()->end()
->booleanNode('psr_logger_enabled')->defaultTrue()->end()
->end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace EonX\EasyHttpClient\Bridge\Symfony\DependencyInjection;

use Bugsnag\Client;
use EonX\EasyHttpClient\Bridge\BridgeConstantsInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -23,9 +24,14 @@ public function load(array $configs, ContainerBuilder $container): void
$config = $this->processConfiguration(new Configuration(), $configs);
$loader = new PhpFileLoader($container, new FileLocator([__DIR__ . '/../Resources/config']));

if (($config['easy_bugsnag_enabled'] ?? true) && \class_exists(Client::class)) {
$loader->load('easy_bugsnag.php');
}
$container->setParameter(
BridgeConstantsInterface::PARAM_DECORATE_DEFAULT_CLIENT,
$config['decorate_default_client'] ?? false
);

if (($config['easy_bugsnag_enabled'] ?? true) && \class_exists(Client::class)) {
$loader->load('easy_bugsnag.php');
}

if (($config['psr_logger_enabled'] ?? true) && \interface_exists(LoggerInterface::class)) {
$loader->load('psr_logger.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

namespace EonX\EasyHttpClient\Bridge\Symfony;

use EonX\EasyHttpClient\Bridge\Symfony\DependencyInjection\Compiler\DecorateDefaultClientPass;
use EonX\EasyHttpClient\Bridge\Symfony\DependencyInjection\EasyHttpClientExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class EasyHttpClientSymfonyBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new DecorateDefaultClientPass());
}

public function getContainerExtension(): ExtensionInterface
{
return new EasyHttpClientExtension();
Expand Down