|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mosaic\Http; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Mosaic\Http\Emitters\SapiEmitter; |
|
7
|
|
|
use Mosaic\Http\Server as ServerContract; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Relay\RelayBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class WebServer implements ServerContract |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $pipes = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Emitter |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $emitter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var callable |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $resolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Emitter $emitter |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function __construct(Emitter $emitter = null) |
|
32
|
|
|
{ |
|
33
|
4 |
|
$this->emitter = $emitter ?: new SapiEmitter; |
|
34
|
|
|
|
|
35
|
4 |
|
$this->setResolver(function ($class) { |
|
36
|
3 |
|
return new $class; |
|
37
|
4 |
|
}); |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Request $request |
|
42
|
|
|
* @param callable|null $terminate |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function serve(Request $request, callable $terminate = null) |
|
45
|
|
|
{ |
|
46
|
4 |
|
if (count($this->pipes) < 1) { |
|
47
|
1 |
|
throw new InvalidArgumentException('No pipes were given to the web server. Response could not be served.'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
ob_start(); |
|
51
|
3 |
|
$bufferLevel = ob_get_level(); |
|
52
|
|
|
|
|
53
|
3 |
|
$response = $this->relay($request); |
|
54
|
|
|
|
|
55
|
3 |
|
if (is_callable($terminate)) { |
|
56
|
1 |
|
$terminate($request->toPsr7(), $response); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
$this->getEmitter()->emit($response, $bufferLevel); |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param Request $request |
|
64
|
|
|
* @return ResponseInterface |
|
65
|
|
|
*/ |
|
66
|
3 |
|
protected function relay(Request $request) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$relay = (new RelayBuilder($this->resolver))->newInstance($this->pipes); |
|
69
|
|
|
|
|
70
|
3 |
|
$response = $relay( |
|
71
|
3 |
|
$request->toPsr7(), |
|
72
|
3 |
|
$request->prepareResponse()->toPsr7() |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
3 |
|
return $response; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array ...$pipes |
|
80
|
|
|
* @return Server |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function pipe(...$pipes) |
|
83
|
|
|
{ |
|
84
|
3 |
|
$this->pipes = $pipes; |
|
85
|
|
|
|
|
86
|
3 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return Emitter |
|
91
|
|
|
*/ |
|
92
|
3 |
|
protected function getEmitter() : Emitter |
|
93
|
|
|
{ |
|
94
|
3 |
|
return $this->emitter; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param callable $resolver |
|
99
|
|
|
* @return Server |
|
100
|
|
|
*/ |
|
101
|
4 |
|
public function setResolver(callable $resolver) |
|
102
|
|
|
{ |
|
103
|
4 |
|
$this->resolver = $resolver; |
|
104
|
|
|
|
|
105
|
4 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|