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