diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index c75ac97..5754305 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -36,7 +36,7 @@ abstract class ClassDiscovery * * @param string $type * - * @return string + * @return string|\Closure * * @throws DiscoveryFailedException */ diff --git a/src/HttpAsyncClientDiscovery.php b/src/HttpAsyncClientDiscovery.php index f058ef5..45d8c5b 100644 --- a/src/HttpAsyncClientDiscovery.php +++ b/src/HttpAsyncClientDiscovery.php @@ -25,7 +25,12 @@ public static function find() try { $asyncClient = static::findOneByType(HttpAsyncClient::class); - return new $asyncClient(); + // Something like this + if (is_string($asyncClient)) { + return new $asyncClient(); + } + + return $asyncClient(); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".', diff --git a/src/Strategy/EmulateAsyncClientStrategy.php b/src/Strategy/EmulateAsyncClientStrategy.php new file mode 100644 index 0000000..2da999d --- /dev/null +++ b/src/Strategy/EmulateAsyncClientStrategy.php @@ -0,0 +1,38 @@ + + */ +final class EmulateAsyncClientStrategy implements DiscoveryStrategy +{ + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + if ($type === 'Http\Client\HttpAsyncClient') { + return [ + // ['class' => Guzzle6::class, 'condition' => Guzzle6::class], + // ['class' => Curl::class, 'condition' => Curl::class], + // ['class' => React::class, 'condition' => React::class], + ['class' => function() { return new EmulatedHttpAsyncClient(new Guzzle5()); }, 'condition' => Guzzle5::class], + ['class' => function() { return new EmulatedHttpAsyncClient(new Socket()); }, 'condition' => Socket::class], + ['class' => function() { return new EmulatedHttpAsyncClient(new Buzz()); }, 'condition' => Buzz::class], + ]; + } + + return []; + } +}