1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Application; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
10
|
|
|
use Psr\Http\Message\UploadedFileFactoryInterface; |
11
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
12
|
|
|
use Simply\Application\Handler\ErrorHandler; |
13
|
|
|
use Simply\Application\Handler\MiddlewareHandler; |
14
|
|
|
use Simply\Application\Handler\NotFoundHandler; |
15
|
|
|
use Simply\Application\HttpFactory\DiactorosHttpFactory; |
16
|
|
|
use Simply\Application\HttpFactory\HttpFactoryInterface; |
17
|
|
|
use Simply\Application\Middleware\ErrorHandlerMiddleware; |
18
|
|
|
use Simply\Application\Middleware\RouterMiddleware; |
19
|
|
|
use Simply\Container\AbstractEntryProvider; |
20
|
|
|
use Simply\Router\Router; |
21
|
|
|
use Zend\Diactoros\RequestFactory; |
22
|
|
|
use Zend\Diactoros\ResponseFactory; |
23
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
24
|
|
|
use Zend\Diactoros\StreamFactory; |
25
|
|
|
use Zend\Diactoros\UploadedFileFactory; |
26
|
|
|
use Zend\Diactoros\UriFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Provides the default dependencies required by the application. |
30
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
31
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
32
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
33
|
|
|
*/ |
34
|
|
|
class ApplicationProvider extends AbstractEntryProvider |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Returns the configured web application. |
38
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
39
|
|
|
* @return Application The configured web application |
40
|
|
|
*/ |
41
|
12 |
|
public function getApplication(ContainerInterface $container): Application |
42
|
|
|
{ |
43
|
12 |
|
return new Application( |
44
|
12 |
|
$container->get(HttpFactoryInterface::class), |
45
|
12 |
|
$container->get(MiddlewareHandler::class), |
46
|
12 |
|
$container->get(HttpClient::class) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the default middleware stack that is run for every request. |
52
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
53
|
|
|
* @return MiddlewareHandler The default middleware stack that is run for every request |
54
|
|
|
*/ |
55
|
12 |
|
public function getMiddlewareHandler(ContainerInterface $container): MiddlewareHandler |
56
|
|
|
{ |
57
|
12 |
|
$stack = new MiddlewareHandler($container->get(NotFoundHandler::class)); |
58
|
|
|
|
59
|
12 |
|
$stack->push($container->get(ErrorHandlerMiddleware::class)); |
60
|
12 |
|
$stack->push($container->get(RouterMiddleware::class)); |
61
|
|
|
|
62
|
12 |
|
return $stack; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the request handler used for 404 responses. |
67
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
68
|
|
|
* @return NotFoundHandler Rhe request handler used for 404 responses |
69
|
|
|
*/ |
70
|
12 |
|
public function getNotFoundHandler(ContainerInterface $container): NotFoundHandler |
71
|
|
|
{ |
72
|
12 |
|
return new NotFoundHandler( |
73
|
12 |
|
$container->get(ResponseFactoryInterface::class), |
74
|
12 |
|
$container->get(StreamFactoryInterface::class) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the middleware used to route exceptions to the error handler. |
80
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
81
|
|
|
* @return ErrorHandlerMiddleware The middleware used to route exceptions to the error handler |
82
|
|
|
*/ |
83
|
12 |
|
public function getErrorHandlerMiddleware(ContainerInterface $container): ErrorHandlerMiddleware |
84
|
|
|
{ |
85
|
12 |
|
return new ErrorHandlerMiddleware( |
86
|
12 |
|
$container->get(ErrorHandler::class) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the request handler for handling exception responses. |
92
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
93
|
|
|
* @return ErrorHandler The request handler for handling exception responses |
94
|
|
|
*/ |
95
|
12 |
|
public function getErrorHandler(ContainerInterface $container): ErrorHandler |
96
|
|
|
{ |
97
|
12 |
|
return new ErrorHandler( |
98
|
12 |
|
$container->get(ResponseFactoryInterface::class), |
99
|
12 |
|
$container->get(StreamFactoryInterface::class) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the router middleware used to route requests to appropriate handlers. |
105
|
|
|
* @param ContainerInterface $container The container used to resolve dependencies |
106
|
|
|
* @return RouterMiddleware The router middleware used to route requests to appropriate handlers |
107
|
|
|
*/ |
108
|
12 |
|
public function getRouterMiddleware(ContainerInterface $container): RouterMiddleware |
109
|
|
|
{ |
110
|
12 |
|
return new RouterMiddleware( |
111
|
12 |
|
$container->get(Router::class), |
112
|
12 |
|
$container, |
113
|
12 |
|
$container->get(ResponseFactoryInterface::class), |
114
|
12 |
|
$container->get(StreamFactoryInterface::class) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the http client used to communicate to the browser. |
120
|
|
|
* @return HttpClient The http client used to communicate to the browser |
121
|
|
|
*/ |
122
|
1 |
|
public function getHttpClient(): HttpClient |
123
|
|
|
{ |
124
|
1 |
|
return new HttpClient(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the factory used create requests and responses. |
129
|
|
|
* @return HttpFactoryInterface The factory for creating requests and responses |
130
|
|
|
*/ |
131
|
1 |
|
public function getHttpFactory(): HttpFactoryInterface |
132
|
|
|
{ |
133
|
1 |
|
return new DiactorosHttpFactory(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns the standard Request factory. |
138
|
|
|
* @return RequestFactoryInterface The standard Request factory |
139
|
|
|
*/ |
140
|
1 |
|
public function getRequestFactory(): RequestFactoryInterface |
141
|
|
|
{ |
142
|
1 |
|
return new RequestFactory(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the standard Response Factory. |
147
|
|
|
* @return ResponseFactoryInterface The standard Response Factory |
148
|
|
|
*/ |
149
|
12 |
|
public function getResponseFactory(): ResponseFactoryInterface |
150
|
|
|
{ |
151
|
12 |
|
return new ResponseFactory(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the standard ServerRequest Factory. |
156
|
|
|
* @return ServerRequestFactoryInterface The standard ServerRequest Factory |
157
|
|
|
*/ |
158
|
1 |
|
public function getServerRequestFactory(): ServerRequestFactoryInterface |
159
|
|
|
{ |
160
|
1 |
|
return new ServerRequestFactory(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the standard Stream Factory. |
165
|
|
|
* @return StreamFactoryInterface The standard Stream Factory |
166
|
|
|
*/ |
167
|
12 |
|
public function getStreamFactory(): StreamFactoryInterface |
168
|
|
|
{ |
169
|
12 |
|
return new StreamFactory(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the standard UploadedFile Factory. |
174
|
|
|
* @return UploadedFileFactoryInterface The standard UploadedFile Factory |
175
|
|
|
*/ |
176
|
1 |
|
public function getUploadedFileFactory(): UploadedFileFactoryInterface |
177
|
|
|
{ |
178
|
1 |
|
return new UploadedFileFactory(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the standard Uri Factory. |
183
|
|
|
* @return UriFactoryInterface The standard Uri Factory |
184
|
|
|
*/ |
185
|
1 |
|
public function getUriFactory(): UriFactoryInterface |
186
|
|
|
{ |
187
|
1 |
|
return new UriFactory(); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|