1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of coisa/http. |
5
|
|
|
* |
6
|
|
|
* (c) Felipe Sayão Lobato Abreu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace CoiSA\Http; |
15
|
|
|
|
16
|
|
|
use CoiSA\Http\Handler\MiddlewareHandler; |
17
|
|
|
use CoiSA\Http\Middleware\EchoBodyMiddleware; |
18
|
|
|
use CoiSA\Http\Middleware\ErrorHandlerMiddleware; |
19
|
|
|
use CoiSA\Http\Middleware\MiddlewareAggregator; |
20
|
|
|
use CoiSA\Http\Middleware\SendHeadersMiddleware; |
21
|
|
|
use Phly\EventDispatcher\EventDispatcher; |
22
|
|
|
use Psr\Http\Message\RequestInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
25
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Application |
29
|
|
|
* |
30
|
|
|
* @package CoiSA\Http |
31
|
|
|
*/ |
32
|
|
|
class Application implements ApplicationInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var MiddlewareHandler |
36
|
|
|
*/ |
37
|
|
|
private $middleware; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Application constructor. |
41
|
|
|
* |
42
|
|
|
* @param DispatcherInterface $dispatcher |
43
|
|
|
* @param RequestHandlerInterface $errorHandler |
44
|
|
|
* @param EventDispatcher $eventDispatcher |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
DispatcherInterface $dispatcher, |
48
|
|
|
RequestHandlerInterface $errorHandler, |
49
|
|
|
EventDispatcher $eventDispatcher |
50
|
|
|
) { |
51
|
|
|
$middleware = new MiddlewareAggregator( |
52
|
|
|
new SendHeadersMiddleware(), |
53
|
|
|
new EchoBodyMiddleware(), |
54
|
|
|
new ErrorHandlerMiddleware( |
55
|
|
|
$errorHandler, |
56
|
|
|
$eventDispatcher |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$this->middleware = new MiddlewareHandler( |
61
|
|
|
$middleware, |
62
|
|
|
$dispatcher |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function sendRequest(RequestInterface $request): ResponseInterface |
70
|
|
|
{ |
71
|
|
|
// @FIXME convert RequestInterface to ServerRequestInterface |
72
|
|
|
return $this->handle($request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
79
|
|
|
{ |
80
|
|
|
return $this->middleware->handle($request); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
87
|
|
|
{ |
88
|
|
|
return $this->middleware->process($request, $handler); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|