/
AppFactory.php
234 lines (201 loc) · 7.28 KB
/
AppFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Factory;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use RuntimeException;
use Slim\App;
use Slim\Factory\Psr17\Psr17Factory;
use Slim\Factory\Psr17\Psr17FactoryProvider;
use Slim\Factory\Psr17\SlimHttpPsr17Factory;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\Psr17FactoryProviderInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteResolverInterface;
class AppFactory
{
/**
* @var Psr17FactoryProviderInterface|null
*/
protected static $psr17FactoryProvider;
/**
* @var ResponseFactoryInterface|null
*/
protected static $responseFactory;
/**
* @var StreamFactoryInterface|null
*/
protected static $streamFactory;
/**
* @var ContainerInterface|null
*/
protected static $container;
/**
* @var CallableResolverInterface|null
*/
protected static $callableResolver;
/**
* @var RouteCollectorInterface|null
*/
protected static $routeCollector;
/**
* @var RouteResolverInterface|null
*/
protected static $routeResolver;
/**
* @var bool
*/
protected static $slimHttpDecoratorsAutomaticDetectionEnabled = true;
/**
* @param ResponseFactoryInterface|null $responseFactory
* @param ContainerInterface|null $container
* @param CallableResolverInterface|null $callableResolver
* @param RouteCollectorInterface|null $routeCollector
* @param RouteResolverInterface|null $routeResolver
* @return App
*/
public static function create(
?ResponseFactoryInterface $responseFactory = null,
?ContainerInterface $container = null,
?CallableResolverInterface $callableResolver = null,
?RouteCollectorInterface $routeCollector = null,
?RouteResolverInterface $routeResolver = null
): App {
static::$responseFactory = $responseFactory ?? static::$responseFactory;
return new App(
self::determineResponseFactory(),
$container ?? static::$container,
$callableResolver ?? static::$callableResolver,
$routeCollector ?? static::$routeCollector,
$routeResolver ?? static::$routeResolver
);
}
/**
* @param ContainerInterface $container
* @return App
*/
public static function createFromContainer(ContainerInterface $container): App
{
$responseFactory = $container->has(ResponseFactoryInterface::class)
? $container->get(ResponseFactoryInterface::class)
: self::determineResponseFactory();
$callableResolver = $container->has(CallableResolverInterface::class)
? $container->get(CallableResolverInterface::class)
: null;
$routeCollector = $container->has(RouteCollectorInterface::class)
? $container->get(RouteCollectorInterface::class)
: null;
$routeResolver = $container->has(RouteResolverInterface::class)
? $container->get(RouteResolverInterface::class)
: null;
return new App($responseFactory, $container, $callableResolver, $routeCollector, $routeResolver);
}
/**
* @return ResponseFactoryInterface
* @throws RuntimeException
*/
public static function determineResponseFactory(): ResponseFactoryInterface
{
if (static::$responseFactory) {
if (static::$streamFactory) {
return static::attemptResponseFactoryDecoration(static::$responseFactory, static::$streamFactory);
}
return static::$responseFactory;
}
$psr17FactoryProvider = static::$psr17FactoryProvider ?? new Psr17FactoryProvider();
/** @var Psr17Factory $psr17factory */
foreach ($psr17FactoryProvider->getFactories() as $psr17factory) {
if ($psr17factory::isResponseFactoryAvailable()) {
$responseFactory = $psr17factory::getResponseFactory();
if ($psr17factory::isStreamFactoryAvailable() || static::$streamFactory) {
$streamFactory = static::$streamFactory ?? $psr17factory::getStreamFactory();
return static::attemptResponseFactoryDecoration($responseFactory, $streamFactory);
}
return $responseFactory;
}
}
throw new RuntimeException(
"Could not detect any PSR-17 ResponseFactory implementations. " .
"Please install a supported implementation in order to use `AppFactory::create()`. " .
"See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations."
);
}
/**
* @param ResponseFactoryInterface $responseFactory
* @param StreamFactoryInterface $streamFactory
* @return ResponseFactoryInterface
*/
protected static function attemptResponseFactoryDecoration(
ResponseFactoryInterface $responseFactory,
StreamFactoryInterface $streamFactory
): ResponseFactoryInterface {
if (static::$slimHttpDecoratorsAutomaticDetectionEnabled
&& SlimHttpPsr17Factory::isResponseFactoryAvailable()
) {
return SlimHttpPsr17Factory::createDecoratedResponseFactory($responseFactory, $streamFactory);
}
return $responseFactory;
}
/**
* @param Psr17FactoryProviderInterface $psr17FactoryProvider
*/
public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void
{
static::$psr17FactoryProvider = $psr17FactoryProvider;
}
/**
* @param ResponseFactoryInterface $responseFactory
*/
public static function setResponseFactory(ResponseFactoryInterface $responseFactory): void
{
static::$responseFactory = $responseFactory;
}
/**
* @param StreamFactoryInterface $streamFactory
*/
public static function setStreamFactory(StreamFactoryInterface $streamFactory): void
{
static::$streamFactory = $streamFactory;
}
/**
* @param ContainerInterface $container
*/
public static function setContainer(ContainerInterface $container): void
{
static::$container = $container;
}
/**
* @param CallableResolverInterface $callableResolver
*/
public static function setCallableResolver(CallableResolverInterface $callableResolver): void
{
static::$callableResolver = $callableResolver;
}
/**
* @param RouteCollectorInterface $routeCollector
*/
public static function setRouteCollector(RouteCollectorInterface $routeCollector): void
{
static::$routeCollector = $routeCollector;
}
/**
* @param RouteResolverInterface $routeResolver
*/
public static function setRouteResolver(RouteResolverInterface $routeResolver): void
{
static::$routeResolver = $routeResolver;
}
/**
* @param bool $enabled
*/
public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void
{
static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled;
}
}