1441 |
ariadna |
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\Psr17;
|
|
|
12 |
|
|
|
13 |
use Psr\Http\Message\ResponseFactoryInterface;
|
|
|
14 |
use Psr\Http\Message\StreamFactoryInterface;
|
|
|
15 |
use RuntimeException;
|
|
|
16 |
use Slim\Interfaces\Psr17FactoryInterface;
|
|
|
17 |
use Slim\Interfaces\ServerRequestCreatorInterface;
|
|
|
18 |
|
|
|
19 |
use function class_exists;
|
|
|
20 |
use function get_called_class;
|
|
|
21 |
|
|
|
22 |
abstract class Psr17Factory implements Psr17FactoryInterface
|
|
|
23 |
{
|
|
|
24 |
protected static string $responseFactoryClass;
|
|
|
25 |
|
|
|
26 |
protected static string $streamFactoryClass;
|
|
|
27 |
|
|
|
28 |
protected static string $serverRequestCreatorClass;
|
|
|
29 |
|
|
|
30 |
protected static string $serverRequestCreatorMethod;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* {@inheritdoc}
|
|
|
34 |
*/
|
|
|
35 |
public static function getResponseFactory(): ResponseFactoryInterface
|
|
|
36 |
{
|
|
|
37 |
if (
|
|
|
38 |
!static::isResponseFactoryAvailable()
|
|
|
39 |
|| !(($responseFactory = new static::$responseFactoryClass()) instanceof ResponseFactoryInterface)
|
|
|
40 |
) {
|
|
|
41 |
throw new RuntimeException(get_called_class() . ' could not instantiate a response factory.');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return $responseFactory;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* {@inheritdoc}
|
|
|
49 |
*/
|
|
|
50 |
public static function getStreamFactory(): StreamFactoryInterface
|
|
|
51 |
{
|
|
|
52 |
if (
|
|
|
53 |
!static::isStreamFactoryAvailable()
|
|
|
54 |
|| !(($streamFactory = new static::$streamFactoryClass()) instanceof StreamFactoryInterface)
|
|
|
55 |
) {
|
|
|
56 |
throw new RuntimeException(get_called_class() . ' could not instantiate a stream factory.');
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
return $streamFactory;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* {@inheritdoc}
|
|
|
64 |
*/
|
|
|
65 |
public static function getServerRequestCreator(): ServerRequestCreatorInterface
|
|
|
66 |
{
|
|
|
67 |
if (!static::isServerRequestCreatorAvailable()) {
|
|
|
68 |
throw new RuntimeException(get_called_class() . ' could not instantiate a server request creator.');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
return new ServerRequestCreator(static::$serverRequestCreatorClass, static::$serverRequestCreatorMethod);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* {@inheritdoc}
|
|
|
76 |
*/
|
|
|
77 |
public static function isResponseFactoryAvailable(): bool
|
|
|
78 |
{
|
|
|
79 |
return static::$responseFactoryClass && class_exists(static::$responseFactoryClass);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* {@inheritdoc}
|
|
|
84 |
*/
|
|
|
85 |
public static function isStreamFactoryAvailable(): bool
|
|
|
86 |
{
|
|
|
87 |
return static::$streamFactoryClass && class_exists(static::$streamFactoryClass);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* {@inheritdoc}
|
|
|
92 |
*/
|
|
|
93 |
public static function isServerRequestCreatorAvailable(): bool
|
|
|
94 |
{
|
|
|
95 |
return (
|
|
|
96 |
static::$serverRequestCreatorClass
|
|
|
97 |
&& static::$serverRequestCreatorMethod
|
|
|
98 |
&& class_exists(static::$serverRequestCreatorClass)
|
|
|
99 |
);
|
|
|
100 |
}
|
|
|
101 |
}
|