diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index cfa717b0..9d964103 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -53,6 +53,7 @@ public function load(array $configs, ContainerBuilder $container) } } + // Set main aliases foreach ($config['main_alias'] as $type => $id) { $container->setAlias(sprintf('httplug.%s', $type), $id); } @@ -69,9 +70,12 @@ public function load(array $configs, ContainerBuilder $container) */ private function configureClients(ContainerBuilder $container, array $config) { + // If we have a client named 'default' $first = isset($config['clients']['default']) ? 'default' : null; + foreach ($config['clients'] as $name => $arguments) { if ($first === null) { + // Save the name of the first configurated client. $first = $name; } diff --git a/Discovery/ConfiguredClientsStrategy.php b/Discovery/ConfiguredClientsStrategy.php new file mode 100644 index 00000000..d3a71eb1 --- /dev/null +++ b/Discovery/ConfiguredClientsStrategy.php @@ -0,0 +1,70 @@ + + */ +class ConfiguredClientsStrategy implements DiscoveryStrategy, EventSubscriberInterface +{ + /** + * @var HttpClient + */ + private static $client; + + /** + * @param HttpClient $httpClient + */ + public function __construct(HttpClient $httpClient) + { + static::$client = $httpClient; + } + + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + if (static::$client !== null && $type == HttpClient::class) { + return [['class' => function () { + return static::$client; + }]]; + } + + return []; + } + + /** + * Make sure to use our custom strategy. + * + * @param Event $e + */ + public function onEvent(Event $e) + { + HttpClientDiscovery::prependStrategy(self::class); + } + + /** + * Whenever these events occur we make sure to add our strategy to the discovery. + * + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return [ + KernelEvents::REQUEST => ['onEvent', 1024], + ConsoleEvents::COMMAND => ['onEvent', 1024], + ]; + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 77e78b6e..78648a20 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,6 +4,10 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + + diff --git a/composer.json b/composer.json index a2503d9d..a0f600ba 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "php-http/logger-plugin": "^1.0", "php-http/stopwatch-plugin": "^1.0", "symfony/options-resolver": "^2.7|^3.0", + "symfony/event-dispatcher": "^2.7|^3.0", "symfony/framework-bundle": "^2.7|^3.0", "php-http/discovery": "^0.9" },