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\Client\RequestHandlerClient; |
17
|
|
|
use CoiSA\Http\Handler\MiddlewareHandler; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
23
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Dispatcher |
27
|
|
|
* |
28
|
|
|
* @package CoiSA\Http |
29
|
|
|
*/ |
30
|
|
|
class Dispatcher implements DispatcherInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var MiddlewareHandler |
34
|
|
|
*/ |
35
|
|
|
private $handler; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RequestHandlerClient |
39
|
|
|
*/ |
40
|
|
|
private $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Dispatcher constructor. |
44
|
|
|
* |
45
|
|
|
* @param RequestHandlerInterface $handler |
46
|
|
|
* @param MiddlewareInterface $middleware |
47
|
|
|
* @param null|ServerRequestFactoryInterface $serverRequestFactory |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
RequestHandlerInterface $handler, |
51
|
|
|
MiddlewareInterface $middleware, |
52
|
|
|
ServerRequestFactoryInterface $serverRequestFactory = null |
53
|
|
|
) { |
54
|
|
|
$this->handler = new MiddlewareHandler( |
55
|
|
|
$middleware, |
56
|
|
|
$handler |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->client = new RequestHandlerClient( |
60
|
|
|
$this->handler, |
61
|
|
|
$serverRequestFactory |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function sendRequest(RequestInterface $request): ResponseInterface |
69
|
|
|
{ |
70
|
|
|
return $this->client->sendRequest($request); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
77
|
|
|
{ |
78
|
|
|
return $this->handler->handle($request); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
85
|
|
|
{ |
86
|
|
|
return $this->handler->process($request, $handler); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|