-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EasyHttpClient] Add Symfony config to decorate default http client (#…
…628)
- Loading branch information
Showing
5 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...yHttpClient/src/Bridge/Symfony/DependencyInjection/Compiler/DecorateDefaultClientPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters