Psr17FactoryDiscovery.php 3.5 KB

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