Skip to content

Commit 96a650b

Browse files
committed
style and doc
1 parent a787c8b commit 96a650b

13 files changed

+74
-38
lines changed

src/ClassDiscovery.php

Lines changed: 13 additions & 17 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,35 +20,36 @@ 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
/**
3530
* Discovery cache to make the second time we use discovery faster.
31+
*
3632
* @var array
3733
*/
3834
private static $cache = [];
3935

4036
/**
4137
* Finds a class.
4238
*
43-
* @param $type
39+
* @param string $type
4440
*
4541
* @return string
4642
*
47-
* @throws NotFoundException
43+
* @throws DiscoveryFailedException
4844
*/
49-
public static function findOneByType($type)
45+
protected static function findOneByType($type)
5046
{
51-
$exceptions = [];
52-
47+
// Look in the cache
5348
if (null !== $class = self::getFromCache($type)) {
5449
return $class;
5550
}
5651

52+
$exceptions = [];
5753
foreach (self::$strategies as $strategy) {
5854
try {
5955
$bindings = call_user_func($strategy.'::find', $type);
@@ -80,7 +76,7 @@ public static function findOneByType($type)
8076
}
8177

8278
/**
83-
* Get a value from cache
79+
* Get a value from cache.
8480
*
8581
* @param string $type
8682
*
@@ -92,11 +88,11 @@ private static function getFromCache($type)
9288
return self::$cache[$type];
9389
}
9490

95-
return null;
91+
return;
9692
}
9793

9894
/**
99-
* Store a value in cache
95+
* Store a value in cache.
10096
*
10197
* @param string $type
10298
* @param string $class

src/Exception/DiscoveryFailedException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ final class DiscoveryFailedException extends \RuntimeException
1515
private $exceptions;
1616

1717
/**
18-
*
1918
* @param $exceptions
2019
*/
2120
public function __construct($message, array $exceptions = [])

src/Exception/StrategyUnavailableException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
/**
66
* This exception is thrown when we cannot use a discovery strategy. This is *not* thrown when
7-
* the discovery fails to find a class.
7+
* the discovery fails to find a class.
88
*
99
* @author Tobias Nyholm <[email protected]>
1010
*/
1111
class StrategyUnavailableException extends \RuntimeException
1212
{
13-
14-
}
13+
}

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/AbstractStrategy.php

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

55
/**
6-
*
7-
*
86
* @author Tobias Nyholm <[email protected]>
97
*/
108
abstract class AbstractStrategy implements DiscoveryStrategy
@@ -22,4 +20,4 @@ public static function find($type)
2220

2321
return [];
2422
}
25-
}
23+
}

src/Strategy/DiscoveryStrategy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
namespace Http\Discovery\Strategy;
44

5+
use Http\Discovery\Exception\StrategyUnavailableException;
6+
57
/**
68
* @author Tobias Nyholm <[email protected]>
79
*/
810
interface DiscoveryStrategy
911
{
1012
/**
11-
* Find a resource of a specific type.
12-
*
13+
* Find a resource of a specific type.
14+
*
1315
* @param string $type
1416
*
1517
* @return array The return value is always an array with zero or more elements. Each
1618
* element is an array with two keys ['class' => string, 'condition' => mixed].
19+
*
20+
* @throws StrategyUnavailableException if we cannot use this strategy.
1721
*/
1822
public static function find($type);
1923
}

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: 10 additions & 7 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
{
@@ -97,9 +100,9 @@ public static function find($type)
97100
if ($binding->hasParameterValue('depends')) {
98101
$condition = $binding->getParameterValue('depends');
99102
}
100-
$returnData[] = ['class'=>$binding->getClassName(), 'condition'=> $condition];
103+
$returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition];
101104
}
102105

103106
return $returnData;
104107
}
105-
}
108+
}

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)