Skip to content

Commit 033765c

Browse files
committed
Added fallback on common classes. Puli is optional
1 parent fac1240 commit 033765c

10 files changed

+187
-3
lines changed

src/ClassDiscovery.php

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

33
namespace Http\Discovery;
44

5+
use Http\Discovery\Exception\NotFoundException;
6+
use Http\Discovery\Exception\PuliNotAvailableException;
7+
use Http\Discovery\FallbackStrategy\HttpClients;
8+
use Http\Discovery\FallbackStrategy\DiactorosFactory;
9+
use Http\Discovery\FallbackStrategy\GuzzleFactory;
510
use Puli\Discovery\Api\Discovery;
611

712
/**
@@ -29,13 +34,13 @@ public static function getPuliFactory()
2934
{
3035
if (null === self::$puliFactory) {
3136
if (!defined('PULI_FACTORY_CLASS')) {
32-
throw new \RuntimeException('Puli Factory is not available');
37+
throw new PuliNotAvailableException('Puli Factory is not available');
3338
}
3439

3540
$puliFactoryClass = PULI_FACTORY_CLASS;
3641

3742
if (!class_exists($puliFactoryClass)) {
38-
throw new \RuntimeException('Puli Factory class does not exist');
43+
throw new PuliNotAvailableException('Puli Factory class does not exist');
3944
}
4045

4146
self::$puliFactory = new $puliFactoryClass();
@@ -95,6 +100,32 @@ public static function getPuliDiscovery()
95100
* @throws NotFoundException
96101
*/
97102
public static function findOneByType($type)
103+
{
104+
try {
105+
return self::puliFindOneByType($type);
106+
} catch (PuliNotAvailableException $e) {
107+
if (false !== $class = HttpClients::findOneByType($type)) {
108+
return $class;
109+
}elseif (false !== $class = GuzzleFactory::findOneByType($type)) {
110+
return $class;
111+
} elseif (false !== $class = DiactorosFactory::findOneByType($type)) {
112+
return $class;
113+
}
114+
throw new NotFoundException('Could not find resource using Puli nor common Guzzle/Diactoros classes', 0, $e);
115+
}
116+
}
117+
118+
/**
119+
* Finds a class using Puli.
120+
*
121+
* @param $type
122+
*
123+
* @return string
124+
*
125+
* @throws NotFoundException
126+
* @throws PuliNotAvailableException
127+
*/
128+
private static function puliFindOneByType($type)
98129
{
99130
$bindings = self::getPuliDiscovery()->findBindings($type);
100131

src/NotFoundException.php renamed to src/Exception/NotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Http\Discovery;
3+
namespace Http\Discovery\Exception;
44

55
/**
66
* Thrown when a discovery does not find any matches.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Http\Discovery\Exception;
4+
5+
/**
6+
* Thrown when we can't use Puli for discovery.
7+
*
8+
* @author Tobias Nyholm <[email protected]>
9+
*/
10+
class PuliNotAvailableException extends \RuntimeException
11+
{
12+
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Http\Discovery\FallbackStrategy;
4+
5+
/**
6+
* Find Diactoros factories.
7+
*
8+
* @author Tobias Nyholm <[email protected]>
9+
*/
10+
class DiactorosFactory implements FallbackStrategy
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public static function findOneByType($type)
16+
{
17+
if (!class_exists('Zend\Diactoros\Request')) {
18+
return false;
19+
}
20+
21+
switch ($type) {
22+
case 'Http\Message\MessageFactory':
23+
if (class_exists('Http\Message\MessageFactory\DiactorosMessageFactory')) {
24+
return 'Http\Message\MessageFactory\DiactorosMessageFactory';
25+
}
26+
break;
27+
case 'Http\Message\StreamFactory':
28+
if (class_exists('Http\Message\StreamFactory\DiactorosStreamFactory')) {
29+
return 'Http\Message\StreamFactory\DiactorosStreamFactory';
30+
}
31+
break;
32+
case 'Http\Message\UriFactory':
33+
if (class_exists('Http\Message\UriFactory\DiactorosUriFactory')) {
34+
return 'Http\Message\UriFactory\DiactorosUriFactory';
35+
}
36+
break;
37+
}
38+
39+
return false;
40+
}
41+
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Http\Discovery\FallbackStrategy;
4+
5+
/**
6+
* @author Tobias Nyholm <[email protected]>
7+
*/
8+
interface FallbackStrategy
9+
{
10+
/**
11+
* @param $type
12+
*
13+
* @return string|bool class name of boolean false
14+
*/
15+
public static function findOneByType($type);
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Http\Discovery\FallbackStrategy;
4+
5+
/**
6+
* Find Guzzle factories.
7+
*
8+
* @author Tobias Nyholm <[email protected]>
9+
*/
10+
class GuzzleFactory implements FallbackStrategy
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public static function findOneByType($type)
16+
{
17+
if (!class_exists('GuzzleHttp\Psr7\Request')) {
18+
return false;
19+
}
20+
21+
switch ($type) {
22+
case 'Http\Message\MessageFactory':
23+
if (class_exists('Http\Message\MessageFactory\GuzzleMessageFactory')) {
24+
return 'Http\Message\MessageFactory\GuzzleMessageFactory';
25+
}
26+
break;
27+
case 'Http\Message\StreamFactory':
28+
if (class_exists('Http\Message\StreamFactory\GuzzleStreamFactory')) {
29+
return 'Http\Message\StreamFactory\GuzzleStreamFactory';
30+
}
31+
break;
32+
case 'Http\Message\UriFactory':
33+
if (class_exists('Http\Message\UriFactory\GuzzleUriFactory')) {
34+
return 'Http\Message\UriFactory\GuzzleUriFactory';
35+
}
36+
break;
37+
}
38+
39+
return false;
40+
}
41+
}

src/FallbackStrategy/HttpClients.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Http\Discovery\FallbackStrategy;
4+
5+
/**
6+
* Find common HTTP clients.
7+
*
8+
* @author Tobias Nyholm <[email protected]>
9+
*/
10+
class HttpClients implements FallbackStrategy
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public static function findOneByType($type)
16+
{
17+
switch ($type) {
18+
case 'Http\Client\HttpAsyncClient':
19+
$clients = ['Http\Adapter\Guzzle6\Client'];
20+
foreach ($clients as $class) {
21+
if (class_exists($class)) {
22+
return $class;
23+
}
24+
}
25+
break;
26+
case 'Http\Client\HttpClient':
27+
$clients = ['Http\Adapter\Guzzle6\Client', 'Http\Adapter\Guzzle5\Client'];
28+
foreach ($clients as $class) {
29+
if (class_exists($class)) {
30+
return $class;
31+
}
32+
}
33+
break;
34+
}
35+
36+
return false;
37+
}
38+
}

src/MessageFactoryDiscovery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Discovery;
44

5+
use Http\Discovery\Exception\NotFoundException;
56
use Http\Message\MessageFactory;
67

78
/**

src/StreamFactoryDiscovery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Discovery;
44

5+
use Http\Discovery\Exception\NotFoundException;
56
use Http\Message\StreamFactory;
67

78
/**

src/UriFactoryDiscovery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Discovery;
44

5+
use Http\Discovery\Exception\NotFoundException;
56
use Http\Message\UriFactory;
67

78
/**

0 commit comments

Comments
 (0)