From aa7164a6c1938fb26b65c87d5fb43ef44b501524 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 3 Jul 2016 21:15:57 +0200 Subject: [PATCH 1/5] Added comments --- DependencyInjection/HttplugExtension.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index cfa717b0..ab88b941 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -69,9 +69,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; } From a06b37e42cc6e9bafa18c4118c3e4db161de7a7e Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 3 Jul 2016 22:11:19 +0200 Subject: [PATCH 2/5] Add a new discovery strategy --- ConfiguredClientsStrategy.php | 43 ++++++++++++++++++++++++ DependencyInjection/HttplugExtension.php | 1 + Resources/config/services.xml | 3 ++ 3 files changed, 47 insertions(+) create mode 100644 ConfiguredClientsStrategy.php diff --git a/ConfiguredClientsStrategy.php b/ConfiguredClientsStrategy.php new file mode 100644 index 00000000..3dfaeddf --- /dev/null +++ b/ConfiguredClientsStrategy.php @@ -0,0 +1,43 @@ + + */ +class ConfiguredClientsStrategy implements DiscoveryStrategy +{ + /** + * @var HttpClient + */ + private static $client; + + /** + * @param HttpClient $httpClient + */ + public function __construct(HttpClient $httpClient) + { + static::$client = $httpClient; + + HttpClientDiscovery::prependStrategy(self::class); + } + + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + if (static::$client !== null && $type == 'Http\Client\HttpClient') { + return [['class' => function() { return static::$client; }]]; + } + + return []; + } +} diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index ab88b941..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); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 77e78b6e..f3985fbf 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,6 +4,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + From 84abb7c5cf5b132719e151ae5ce0ffa4f1c32f49 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 13 Jul 2016 18:28:50 +0200 Subject: [PATCH 3/5] Added events --- ConfiguredClientsStrategy.php | 43 ---------------- Discovery/ConfiguredClientsStrategy.php | 68 +++++++++++++++++++++++++ Resources/config/services.xml | 3 +- 3 files changed, 70 insertions(+), 44 deletions(-) delete mode 100644 ConfiguredClientsStrategy.php create mode 100644 Discovery/ConfiguredClientsStrategy.php diff --git a/ConfiguredClientsStrategy.php b/ConfiguredClientsStrategy.php deleted file mode 100644 index 3dfaeddf..00000000 --- a/ConfiguredClientsStrategy.php +++ /dev/null @@ -1,43 +0,0 @@ - - */ -class ConfiguredClientsStrategy implements DiscoveryStrategy -{ - /** - * @var HttpClient - */ - private static $client; - - /** - * @param HttpClient $httpClient - */ - public function __construct(HttpClient $httpClient) - { - static::$client = $httpClient; - - HttpClientDiscovery::prependStrategy(self::class); - } - - /** - * {@inheritdoc} - */ - public static function getCandidates($type) - { - if (static::$client !== null && $type == 'Http\Client\HttpClient') { - return [['class' => function() { return static::$client; }]]; - } - - return []; - } -} diff --git a/Discovery/ConfiguredClientsStrategy.php b/Discovery/ConfiguredClientsStrategy.php new file mode 100644 index 00000000..9314edf3 --- /dev/null +++ b/Discovery/ConfiguredClientsStrategy.php @@ -0,0 +1,68 @@ + + */ +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 f3985fbf..78648a20 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,8 +4,9 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + + From 36f9a1af8ef5f3df71dbe8390a527e25bc865c2d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 13 Jul 2016 18:29:44 +0200 Subject: [PATCH 4/5] style fix --- Discovery/ConfiguredClientsStrategy.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Discovery/ConfiguredClientsStrategy.php b/Discovery/ConfiguredClientsStrategy.php index 9314edf3..d3a71eb1 100644 --- a/Discovery/ConfiguredClientsStrategy.php +++ b/Discovery/ConfiguredClientsStrategy.php @@ -3,7 +3,6 @@ namespace Http\HttplugBundle\Discovery; use Http\Client\HttpClient; -use Http\Discovery\Exception\StrategyUnavailableException; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Strategy\DiscoveryStrategy; use Symfony\Component\Console\ConsoleEvents; @@ -38,14 +37,17 @@ public function __construct(HttpClient $httpClient) public static function getCandidates($type) { if (static::$client !== null && $type == HttpClient::class) { - return [['class' => function() { return static::$client; }]]; + return [['class' => function () { + return static::$client; + }]]; } return []; } /** - * Make sure to use our custom strategy + * Make sure to use our custom strategy. + * * @param Event $e */ public function onEvent(Event $e) From f109693fd0c7682fce91b40a1213964f35eaac07 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 13 Jul 2016 18:34:01 +0200 Subject: [PATCH 5/5] Make sure we require event-dispatcher --- composer.json | 1 + 1 file changed, 1 insertion(+) 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" },