Просмотр файла vendor/slim/slim/Slim/Factory/ServerRequestCreatorFactory.php

Размер файла: 3Kb
  1. <?php
  2.  
  3. /**
  4. * Slim Framework (https://slimframework.com)
  5. *
  6. * @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
  7. */
  8.  
  9. declare(strict_types=1);
  10.  
  11. namespace Slim\Factory;
  12.  
  13. use RuntimeException;
  14. use Slim\Factory\Psr17\Psr17Factory;
  15. use Slim\Factory\Psr17\Psr17FactoryProvider;
  16. use Slim\Factory\Psr17\SlimHttpServerRequestCreator;
  17. use Slim\Interfaces\Psr17FactoryProviderInterface;
  18. use Slim\Interfaces\ServerRequestCreatorInterface;
  19.  
  20. class ServerRequestCreatorFactory
  21. {
  22. protected static ?Psr17FactoryProviderInterface $psr17FactoryProvider = null;
  23.  
  24. protected static ?ServerRequestCreatorInterface $serverRequestCreator = null;
  25.  
  26. protected static bool $slimHttpDecoratorsAutomaticDetectionEnabled = true;
  27.  
  28. public static function create(): ServerRequestCreatorInterface
  29. {
  30. return static::determineServerRequestCreator();
  31. }
  32.  
  33. /**
  34. * @throws RuntimeException
  35. */
  36. public static function determineServerRequestCreator(): ServerRequestCreatorInterface
  37. {
  38. if (static::$serverRequestCreator) {
  39. return static::attemptServerRequestCreatorDecoration(static::$serverRequestCreator);
  40. }
  41.  
  42. $psr17FactoryProvider = static::$psr17FactoryProvider ?? new Psr17FactoryProvider();
  43.  
  44. /** @var Psr17Factory $psr17Factory */
  45. foreach ($psr17FactoryProvider->getFactories() as $psr17Factory) {
  46. if ($psr17Factory::isServerRequestCreatorAvailable()) {
  47. $serverRequestCreator = $psr17Factory::getServerRequestCreator();
  48. return static::attemptServerRequestCreatorDecoration($serverRequestCreator);
  49. }
  50. }
  51.  
  52. throw new RuntimeException(
  53. "Could not detect any ServerRequest creator implementations. " .
  54. "Please install a supported implementation in order to use `App::run()` " .
  55. "without having to pass in a `ServerRequest` object. " .
  56. "See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations."
  57. );
  58. }
  59.  
  60. protected static function attemptServerRequestCreatorDecoration(
  61. ServerRequestCreatorInterface $serverRequestCreator
  62. ): ServerRequestCreatorInterface {
  63. if (
  64. static::$slimHttpDecoratorsAutomaticDetectionEnabled
  65. && SlimHttpServerRequestCreator::isServerRequestDecoratorAvailable()
  66. ) {
  67. return new SlimHttpServerRequestCreator($serverRequestCreator);
  68. }
  69.  
  70. return $serverRequestCreator;
  71. }
  72.  
  73. public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void
  74. {
  75. static::$psr17FactoryProvider = $psr17FactoryProvider;
  76. }
  77.  
  78. public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void
  79. {
  80. self::$serverRequestCreator = $serverRequestCreator;
  81. }
  82.  
  83. public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void
  84. {
  85. static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled;
  86. }
  87. }