Skip to content

Commit a5b302c

Browse files
committed
Style and better exceptions
1 parent e1f85de commit a5b302c

9 files changed

+59
-24
lines changed

src/ClassDiscovery.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
use Http\Discovery\Exception\DiscoveryFailedException;
66
use Http\Discovery\Exception\StrategyUnavailableException;
7-
use Http\Discovery\Exception\NotFoundException;
8-
use Http\Discovery\Strategy\DiactorosFactory;
9-
use Http\Discovery\Strategy\GuzzleFactory;
10-
use Http\Discovery\Strategy\HttpClients;
11-
use Http\Discovery\Strategy\Puli;
127

138
/**
149
* Registry that based find results on class existence.
@@ -25,10 +20,10 @@ abstract class ClassDiscovery
2520
* @var array
2621
*/
2722
public static $strategies = [
28-
Puli::class,
29-
HttpClients::class,
30-
GuzzleFactory::class,
31-
DiactorosFactory::class,
23+
Strategy\Puli::class,
24+
Strategy\HttpClients::class,
25+
Strategy\GuzzleFactory::class,
26+
Strategy\DiactorosFactory::class,
3227
];
3328

3429
/**
@@ -41,20 +36,20 @@ abstract class ClassDiscovery
4136
/**
4237
* Finds a class.
4338
*
44-
* @param $type
39+
* @param string $type
4540
*
4641
* @return string
4742
*
48-
* @throws NotFoundException
43+
* @throws DiscoveryFailedException
4944
*/
50-
public static function findOneByType($type)
45+
protected static function findOneByType($type)
5146
{
52-
$exceptions = [];
53-
47+
// Look in the cache
5448
if (null !== $class = self::getFromCache($type)) {
5549
return $class;
5650
}
5751

52+
$exceptions = [];
5853
foreach (self::$strategies as $strategy) {
5954
try {
6055
$bindings = call_user_func($strategy.'::find', $type);

src/HttpAsyncClientDiscovery.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Discovery;
44

55
use Http\Client\HttpAsyncClient;
6+
use Http\Discovery\Exception\DiscoveryFailedException;
67

78
/**
89
* Finds an HTTP Asynchronous Client.
@@ -15,11 +16,21 @@ final class HttpAsyncClientDiscovery extends ClassDiscovery
1516
* Finds an HTTP Async Client.
1617
*
1718
* @return HttpAsyncClient
19+
*
20+
* @throws NotFoundException
1821
*/
1922
public static function find()
2023
{
21-
$asyncClient = static::findOneByType('Http\Client\HttpAsyncClient');
24+
try {
25+
$asyncClient = static::findOneByType('Http\Client\HttpAsyncClient');
2226

23-
return new $asyncClient();
27+
return new $asyncClient();
28+
} catch (DiscoveryFailedException $e) {
29+
throw new NotFoundException(
30+
'No Httplug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".',
31+
0,
32+
$e
33+
);
34+
}
2435
}
2536
}

src/HttpClientDiscovery.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Discovery;
44

55
use Http\Client\HttpClient;
6+
use Http\Discovery\Exception\DiscoveryFailedException;
67

78
/**
89
* Finds an HTTP Client.
@@ -15,11 +16,21 @@ final class HttpClientDiscovery extends ClassDiscovery
1516
* Finds an HTTP Client.
1617
*
1718
* @return HttpClient
19+
*
20+
* @throws NotFoundException
1821
*/
1922
public static function find()
2023
{
21-
$client = static::findOneByType('Http\Client\HttpClient');
24+
try {
25+
$client = static::findOneByType('Http\Client\HttpClient');
2226

23-
return new $client();
27+
return new $client();
28+
} catch (DiscoveryFailedException $e) {
29+
throw new NotFoundException(
30+
'No Httplug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".',
31+
0,
32+
$e
33+
);
34+
}
2435
}
2536
}

src/MessageFactoryDiscovery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ final class MessageFactoryDiscovery extends ClassDiscovery
1717
* Finds a Message Factory.
1818
*
1919
* @return MessageFactory
20+
*
21+
* @throws NotFoundException
2022
*/
2123
public static function find()
2224
{

src/Strategy/GuzzleFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GuzzleFactory extends AbstractStrategy
1919
*/
2020
protected static $classes = [
2121
'Http\Message\MessageFactory' => [
22-
['class' => GuzzleMessageFactory::class, 'condition' => [Request::class, DiactorosMessageFactory::class]],
22+
['class' => GuzzleMessageFactory::class, 'condition' => [Request::class, GuzzleMessageFactory::class]],
2323
],
2424
'Http\Message\StreamFactory' => [
2525
['class' => GuzzleStreamFactory::class, 'condition' => [Request::class, GuzzleStreamFactory::class]],

src/Strategy/HttpClients.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Http\Adapter\Guzzle6\Client as Guzzle6;
66
use Http\Adapter\Guzzle5\Client as Guzzle5;
7+
use Http\Client\Curl\Client as Curl;
8+
use Http\Client\Socket\Client as Socket;
9+
use Http\Adapter\React\Client as React;
10+
use Http\Adapter\Buzz\Client as Buzz;
711

812
/**
913
* Find common HTTP clients.
@@ -18,10 +22,15 @@ class HttpClients extends AbstractStrategy
1822
protected static $classes = [
1923
'Http\Client\HttpAsyncClient' => [
2024
['class' => Guzzle6::class, 'condition' => Guzzle6::class],
25+
['class' => React::class, 'condition' => React::class],
2126
],
2227
'Http\Client\HttpClient' => [
2328
['class' => Guzzle6::class, 'condition' => Guzzle6::class],
2429
['class' => Guzzle5::class, 'condition' => Guzzle5::class],
30+
['class' => Curl::class, 'condition' => Curl::class],
31+
['class' => Socket::class, 'condition' => Socket::class],
32+
['class' => Buzz::class, 'condition' => Buzz::class],
33+
['class' => React::class, 'condition' => React::class],
2534
],
2635
];
2736
}

src/Strategy/Puli.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Http\Discovery\Strategy;
44

55
use Http\Discovery\Exception\PuliUnavailableException;
6-
use Puli\Discovery\Api\Discovery;
76

87
/**
98
* @author David de Boer <[email protected]>
@@ -12,17 +11,19 @@
1211
class Puli implements DiscoveryStrategy
1312
{
1413
/**
15-
* @var GeneratedPuliFactory
14+
* @var \Puli\GeneratedPuliFactory
1615
*/
1716
private static $puliFactory;
1817

1918
/**
20-
* @var Discovery
19+
* @var \Puli\Discovery\Api\Discovery
2120
*/
2221
private static $puliDiscovery;
2322

2423
/**
25-
* @return GeneratedPuliFactory
24+
* @return \Puli\GeneratedPuliFactory
25+
*
26+
* @throws PuliUnavailableException
2627
*/
2728
public static function getPuliFactory()
2829
{
@@ -70,7 +71,9 @@ public static function resetPuliFactory()
7071
/**
7172
* Returns the Puli discovery layer.
7273
*
73-
* @return Discovery
74+
* @return \Puli\Discovery\Api\Discovery
75+
*
76+
* @throws PuliUnavailableException
7477
*/
7578
public static function getPuliDiscovery()
7679
{

src/StreamFactoryDiscovery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ final class StreamFactoryDiscovery extends ClassDiscovery
1717
* Finds a Stream Factory.
1818
*
1919
* @return StreamFactory
20+
*
21+
* @throws NotFoundException
2022
*/
2123
public static function find()
2224
{

src/UriFactoryDiscovery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ final class UriFactoryDiscovery extends ClassDiscovery
1717
* Finds a URI Factory.
1818
*
1919
* @return UriFactory
20+
*
21+
* @throws NotFoundException
2022
*/
2123
public static function find()
2224
{

0 commit comments

Comments
 (0)