Skip to content

Commit b7a424f

Browse files
authored
Alternative to Symfony Httplug Async (#161)
* Alternative fix for verify that an class can be instanciated * More tests * Fixed typo * error handling * cs * use warning * We still need interface_exists
1 parent 2e0ad94 commit b7a424f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ matrix:
6565
- install_test cant-find "Http\Discovery\HttpClientDiscovery::find();" "symfony/http-client:5.* php-http/httplug guzzlehttp/psr7:1.* http-interop/http-factory-guzzle"
6666
# We should be able to find a client when Symfony is only partly installed and we have guzzle adapter installed
6767
- install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "symfony/http-client:5.* php-http/guzzle6-adapter php-http/httplug php-http/message-factory guzzlehttp/psr7:1.*"
68-
68+
# Test that we find a client with Symfony and Guzzle
69+
- install_test will-find "Http\Discovery\HttpClientDiscovery::find();" "php-http/client-common:2.* php-http/message:1.8.* symfony/http-client:4.* php-http/guzzle6-adapter"
70+
# Test that we find an async client with Symfony and Guzzle
71+
- install_test will-find "Http\Discovery\HttpAsyncClientDiscovery::find();" "php-http/client-common:2.* php-http/message:1.8.* symfony/http-client:4.* php-http/guzzle6-adapter"
6972

7073
before_install:
7174
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi

src/Strategy/CommonClassesStrategy.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Psr7\Request as GuzzleRequest;
77
use Http\Client\HttpAsyncClient;
88
use Http\Client\HttpClient;
9+
use Http\Discovery\Exception\NotFoundException;
910
use Http\Discovery\MessageFactoryDiscovery;
1011
use Http\Discovery\Psr17FactoryDiscovery;
1112
use Http\Message\RequestFactory;
@@ -68,13 +69,13 @@ final class CommonClassesStrategy implements DiscoveryStrategy
6869
['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]],
6970
],
7071
HttpAsyncClient::class => [
71-
['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, RequestFactory::class]],
72+
['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, RequestFactory::class, [self::class, 'isPsr17FactoryInstalled']]],
7273
['class' => Guzzle6::class, 'condition' => Guzzle6::class],
7374
['class' => Curl::class, 'condition' => Curl::class],
7475
['class' => React::class, 'condition' => React::class],
7576
],
7677
HttpClient::class => [
77-
['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, RequestFactory::class, Psr17RequestFactory::class]],
78+
['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, RequestFactory::class, [self::class, 'isPsr17FactoryInstalled']]],
7879
['class' => Guzzle6::class, 'condition' => Guzzle6::class],
7980
['class' => Guzzle5::class, 'condition' => Guzzle5::class],
8081
['class' => Curl::class, 'condition' => Curl::class],
@@ -135,4 +136,24 @@ public static function symfonyPsr18Instantiate()
135136
{
136137
return new SymfonyPsr18(null, Psr17FactoryDiscovery::findResponseFactory(), Psr17FactoryDiscovery::findStreamFactory());
137138
}
139+
140+
/**
141+
* Can be used as a condition.
142+
*
143+
* @return bool
144+
*/
145+
public static function isPsr17FactoryInstalled()
146+
{
147+
try {
148+
Psr17FactoryDiscovery::findResponseFactory();
149+
} catch (NotFoundException $e) {
150+
return false;
151+
} catch (\Throwable $e) {
152+
trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-17 ResponseFactory is available', get_class($e), $e->getMessage()), E_USER_WARNING);
153+
154+
return false;
155+
}
156+
157+
return true;
158+
}
138159
}

0 commit comments

Comments
 (0)