|
4 | 4 |
|
5 | 5 | namespace PhpMyAdmin\Http\Factory; |
6 | 6 |
|
| 7 | +use Fig\Http\Message\StatusCodeInterface; |
7 | 8 | use GuzzleHttp\Psr7\HttpFactory; |
| 9 | +use HttpSoft\Message\ResponseFactory as HttpSoftResponseFactory; |
8 | 10 | use Laminas\Diactoros\ResponseFactory as LaminasResponseFactory; |
9 | 11 | use Nyholm\Psr7\Factory\Psr17Factory; |
10 | 12 | use PhpMyAdmin\Http\Response; |
11 | 13 | use Psr\Http\Message\ResponseFactoryInterface; |
| 14 | +use RuntimeException; |
12 | 15 | use Slim\Psr7\Factory\ResponseFactory as SlimResponseFactory; |
13 | 16 |
|
14 | 17 | use function class_exists; |
15 | 18 |
|
16 | | -class ResponseFactory |
| 19 | +final class ResponseFactory implements ResponseFactoryInterface |
17 | 20 | { |
18 | | - private ResponseFactoryInterface $responseFactory; |
19 | | - |
20 | | - public function __construct(ResponseFactoryInterface|null $responseFactory = null) |
| 21 | + /** @psalm-var list<class-string<ResponseFactoryInterface>> */ |
| 22 | + private static array $providers = [ |
| 23 | + SlimResponseFactory::class, |
| 24 | + LaminasResponseFactory::class, |
| 25 | + Psr17Factory::class, |
| 26 | + HttpFactory::class, |
| 27 | + HttpSoftResponseFactory::class, |
| 28 | + ]; |
| 29 | + |
| 30 | + public function __construct(private ResponseFactoryInterface $responseFactory) |
21 | 31 | { |
22 | | - $this->responseFactory = $responseFactory ?? $this->createResponseFactory(); |
23 | 32 | } |
24 | 33 |
|
25 | | - /** |
26 | | - * Create a new response. |
27 | | - * |
28 | | - * @param int $code HTTP status code; defaults to 200 |
29 | | - * @param string $reasonPhrase Reason phrase to associate with status code |
30 | | - * in generated response; if none is provided implementations MAY use |
31 | | - * the defaults as suggested in the HTTP specification. |
32 | | - */ |
33 | | - public function createResponse(int $code = 200, string $reasonPhrase = ''): Response |
| 34 | + public function createResponse(int $code = StatusCodeInterface::STATUS_OK, string $reasonPhrase = ''): Response |
34 | 35 | { |
35 | 36 | return new Response($this->responseFactory->createResponse($code, $reasonPhrase)); |
36 | 37 | } |
37 | 38 |
|
38 | | - private function createResponseFactory(): ResponseFactoryInterface |
| 39 | + /** @throws RuntimeException When no {@see ResponseFactoryInterface} implementation is found. */ |
| 40 | + public static function create(): self |
39 | 41 | { |
40 | | - if (class_exists(Psr17Factory::class)) { |
41 | | - /** @var ResponseFactoryInterface $factory */ |
42 | | - $factory = new Psr17Factory(); |
43 | | - } elseif (class_exists(HttpFactory::class)) { |
44 | | - /** @var ResponseFactoryInterface $factory */ |
45 | | - $factory = new HttpFactory(); |
46 | | - } elseif (class_exists(LaminasResponseFactory::class)) { |
47 | | - /** @var ResponseFactoryInterface $factory */ |
48 | | - $factory = new LaminasResponseFactory(); |
49 | | - } else { |
50 | | - $factory = new SlimResponseFactory(); |
| 42 | + foreach (self::$providers as $provider) { |
| 43 | + if (class_exists($provider)) { |
| 44 | + return new self(new $provider()); |
| 45 | + } |
51 | 46 | } |
52 | 47 |
|
53 | | - return $factory; |
| 48 | + throw new RuntimeException('No HTTP response factories found.'); |
54 | 49 | } |
55 | 50 | } |
0 commit comments