| 1 |
<?php |
| 2 |
|
| 3 |
namespace Http\Discovery; |
| 4 |
|
| 5 |
use Http\Discovery\Exception\DiscoveryFailedException; |
| 6 |
use Http\Discovery\Exception\NotFoundException as RealNotFoundException; |
| 7 |
use Psr\Http\Message\RequestFactoryInterface; |
| 8 |
use Psr\Http\Message\ResponseFactoryInterface; |
| 9 |
use Psr\Http\Message\ServerRequestFactoryInterface; |
| 10 |
use Psr\Http\Message\StreamFactoryInterface; |
| 11 |
use Psr\Http\Message\UploadedFileFactoryInterface; |
| 12 |
use Psr\Http\Message\UriFactoryInterface; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class Psr17FactoryDiscovery extends ClassDiscovery |
| 20 |
{ |
| 21 |
private static function createException($type, Exception $e) |
| 22 |
{ |
| 23 |
return new RealNotFoundException( |
| 24 |
'No PSR-17 '.$type.' found. Install a package from this list: https://packagist.org/providers/psr/http-factory-implementation', |
| 25 |
0, |
| 26 |
$e |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static function findRequestFactory() |
| 36 |
{ |
| 37 |
try { |
| 38 |
$messageFactory = static::findOneByType(RequestFactoryInterface::class); |
| 39 |
} catch (DiscoveryFailedException $e) { |
| 40 |
throw self::createException('request factory', $e); |
| 41 |
} |
| 42 |
|
| 43 |
return static::instantiateClass($messageFactory); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static function findResponseFactory() |
| 52 |
{ |
| 53 |
try { |
| 54 |
$messageFactory = static::findOneByType(ResponseFactoryInterface::class); |
| 55 |
} catch (DiscoveryFailedException $e) { |
| 56 |
throw self::createException('response factory', $e); |
| 57 |
} |
| 58 |
|
| 59 |
return static::instantiateClass($messageFactory); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function findServerRequestFactory() |
| 68 |
{ |
| 69 |
try { |
| 70 |
$messageFactory = static::findOneByType(ServerRequestFactoryInterface::class); |
| 71 |
} catch (DiscoveryFailedException $e) { |
| 72 |
throw self::createException('server request factory', $e); |
| 73 |
} |
| 74 |
|
| 75 |
return static::instantiateClass($messageFactory); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function findStreamFactory() |
| 84 |
{ |
| 85 |
try { |
| 86 |
$messageFactory = static::findOneByType(StreamFactoryInterface::class); |
| 87 |
} catch (DiscoveryFailedException $e) { |
| 88 |
throw self::createException('stream factory', $e); |
| 89 |
} |
| 90 |
|
| 91 |
return static::instantiateClass($messageFactory); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static function findUploadedFileFactory() |
| 100 |
{ |
| 101 |
try { |
| 102 |
$messageFactory = static::findOneByType(UploadedFileFactoryInterface::class); |
| 103 |
} catch (DiscoveryFailedException $e) { |
| 104 |
throw self::createException('uploaded file factory', $e); |
| 105 |
} |
| 106 |
|
| 107 |
return static::instantiateClass($messageFactory); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static function findUriFactory() |
| 116 |
{ |
| 117 |
try { |
| 118 |
$messageFactory = static::findOneByType(UriFactoryInterface::class); |
| 119 |
} catch (DiscoveryFailedException $e) { |
| 120 |
throw self::createException('url factory', $e); |
| 121 |
} |
| 122 |
|
| 123 |
return static::instantiateClass($messageFactory); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public static function findUrlFactory() |
| 134 |
{ |
| 135 |
return static::findUriFactory(); |
| 136 |
} |
| 137 |
} |