|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/http package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Http; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
14
|
|
|
use Slick\Http\PhpEnvironment; |
|
15
|
|
|
use Slick\Http\PhpEnvironment\MiddlewareRunnerInterface; |
|
16
|
|
|
use Slick\Http\Server\MiddlewareCollection; |
|
17
|
|
|
use Slick\Http\Server\MiddlewareCollectionInterface; |
|
18
|
|
|
use Slick\Http\Server\MiddlewareInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* HTTP Server handler and middleware runner |
|
22
|
|
|
* |
|
23
|
|
|
* @package Slick\Http |
|
24
|
|
|
*/ |
|
25
|
|
|
class Server implements MiddlewareRunnerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ServerRequestInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $request; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ResponseInterface|PhpEnvironment\Response |
|
35
|
|
|
*/ |
|
36
|
|
|
private $response; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var MiddlewareCollectionInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $middlewareCollection; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Server request handler and middleware runner. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ServerRequestInterface|null $req |
|
47
|
|
|
* @param ResponseInterface|null $res |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function __construct( |
|
50
|
|
|
ServerRequestInterface $req = null, ResponseInterface $res = null |
|
51
|
|
|
) { |
|
52
|
6 |
|
$this->request = $req; |
|
53
|
6 |
|
$this->response = $res; |
|
54
|
6 |
|
$this->middlewareCollection = new MiddlewareCollection(); |
|
55
|
6 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set HTTP request message |
|
59
|
|
|
* |
|
60
|
|
|
* @param ServerRequestInterface $request |
|
61
|
|
|
* |
|
62
|
|
|
* @return Server |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setRequest(ServerRequestInterface $request) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->request = $request; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Adds a middleware object to the stack |
|
72
|
|
|
* |
|
73
|
|
|
* @param MiddlewareInterface $middleware |
|
74
|
|
|
* |
|
75
|
|
|
* @return self|$this|\Slick\Http\Server\MiddlewareRunnerInterface |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function add(MiddlewareInterface $middleware) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$this->middlewareCollection->append($middleware); |
|
80
|
2 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets a full list of middleware objects tho this runner |
|
85
|
|
|
* |
|
86
|
|
|
* @param MiddlewareCollectionInterface $objects |
|
87
|
|
|
* |
|
88
|
|
|
* @return self|$this|\Slick\Http\Server\MiddlewareRunnerInterface |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function setMiddlewareCollection( |
|
91
|
|
|
MiddlewareCollectionInterface $objects |
|
92
|
|
|
) |
|
93
|
|
|
{ |
|
94
|
4 |
|
$this->middlewareCollection = $objects; |
|
95
|
4 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Gets the middleware collection object |
|
100
|
|
|
* |
|
101
|
|
|
* @return MiddlewareCollection|MiddlewareCollectionInterface |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function getMiddlewareCollection() |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->middlewareCollection; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Runs all the stack and return the response |
|
110
|
|
|
* |
|
111
|
|
|
* @return ResponseInterface|Response |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function run() |
|
114
|
|
|
{ |
|
115
|
2 |
|
$middleware = $this->setObjectDependency(); |
|
116
|
2 |
|
return $middleware->handle($this->request, $this->getResponse()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Initializes and reuses the server response |
|
121
|
|
|
* |
|
122
|
|
|
* @return ResponseInterface|PhpEnvironment\Response |
|
123
|
|
|
*/ |
|
124
|
2 |
|
protected function getResponse() |
|
125
|
|
|
{ |
|
126
|
2 |
|
if (null === $this->response) { |
|
127
|
2 |
|
$this->response = new PhpEnvironment\Response(); |
|
128
|
2 |
|
} |
|
129
|
2 |
|
return $this->response; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets the middleware objects dependency |
|
134
|
|
|
* |
|
135
|
|
|
* This method returns the middleware that starts the chain |
|
136
|
|
|
* |
|
137
|
|
|
* @return null|MiddlewareInterface |
|
138
|
|
|
*/ |
|
139
|
2 |
|
protected function setObjectDependency() |
|
140
|
|
|
{ |
|
141
|
2 |
|
$reverse = array_reverse($this->middlewareCollection->asArray()); |
|
142
|
2 |
|
$last = null; |
|
143
|
|
|
/** @var MiddlewareInterface $object */ |
|
144
|
2 |
|
foreach ($reverse as $object) { |
|
145
|
2 |
|
$object->setNext($last); |
|
146
|
2 |
|
$last = $object; |
|
147
|
2 |
|
} |
|
148
|
2 |
|
return $last; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|