Skip to content

Commit

Permalink
[EasyHttpClient] Add Symfony config to decorate default http client (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
natepage committed Jul 5, 2021
1 parent 6488539 commit 17fed9f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
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

0 comments on commit 17fed9f

Please sign in to comment.