This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathServer.php
188 lines (172 loc) · 5.06 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Diactoros;
use OutOfBoundsException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* "Serve" incoming HTTP requests
*
* Given a callback, takes an incoming request, dispatches it to the
* callback, and then sends a response.
*/
class Server
{
/**
* @var callable
*/
private $callback;
/**
* Response emitter to use; by default, uses Response\SapiEmitter.
*
* @var Response\EmitterInterface
*/
private $emitter;
/**
* @var ServerRequestInterface
*/
private $request;
/**
* @var ResponseInterface
*/
private $response;
/**
* Constructor
*
* Given a callback, a request, and a response, we can create a server.
*
* @param callable $callback
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*/
public function __construct(
callable $callback,
ServerRequestInterface $request,
ResponseInterface $response
) {
$this->callback = $callback;
$this->request = $request;
$this->response = $response;
}
/**
* Allow retrieving the request, response and callback as properties
*
* @param string $name
* @return mixed
* @throws OutOfBoundsException for invalid properties
*/
public function __get($name)
{
if (! property_exists($this, $name)) {
throw new OutOfBoundsException('Cannot retrieve arbitrary properties from server');
}
return $this->{$name};
}
/**
* Set alternate response emitter to use.
*
* @param Response\EmitterInterface $emitter
*/
public function setEmitter(Response\EmitterInterface $emitter)
{
$this->emitter = $emitter;
}
/**
* Create a Server instance
*
* Creates a server instance from the callback and the following
* PHP environmental values:
*
* - server; typically this will be the $_SERVER superglobal
* - query; typically this will be the $_GET superglobal
* - body; typically this will be the $_POST superglobal
* - cookies; typically this will be the $_COOKIE superglobal
* - files; typically this will be the $_FILES superglobal
*
* @param callable $callback
* @param array $server
* @param array $query
* @param array $body
* @param array $cookies
* @param array $files
* @return static
*/
public static function createServer(
callable $callback,
array $server,
array $query,
array $body,
array $cookies,
array $files
) {
$request = ServerRequestFactory::fromGlobals($server, $query, $body, $cookies, $files);
$response = new Response();
return new static($callback, $request, $response);
}
/**
* Create a Server instance from an existing request object
*
* Provided a callback, an existing request object, and optionally an
* existing response object, create and return the Server instance.
*
* If no Response object is provided, one will be created.
*
* @param callable $callback
* @param ServerRequestInterface $request
* @param null|ResponseInterface $response
* @return static
*/
public static function createServerFromRequest(
callable $callback,
ServerRequestInterface $request,
ResponseInterface $response = null
) {
if (! $response) {
$response = new Response();
}
return new static($callback, $request, $response);
}
/**
* "Listen" to an incoming request
*
* If provided a $finalHandler, that callable will be used for
* incomplete requests.
*
* Output buffering is enabled prior to invoking the attached
* callback; any output buffered will be sent prior to any
* response body content.
*
* @param null|callable $finalHandler
*/
public function listen(callable $finalHandler = null)
{
$callback = $this->callback;
ob_start();
$bufferLevel = ob_get_level();
$response = $callback($this->request, $this->response, $finalHandler);
if (! $response instanceof ResponseInterface) {
$response = $this->response;
}
$this->getEmitter()->emit($response, $bufferLevel);
}
/**
* Retrieve the current response emitter.
*
* If none has been registered, lazy-loads a Response\SapiEmitter.
*
* @return Response\EmitterInterface
*/
private function getEmitter()
{
if (! $this->emitter) {
$this->emitter = new Response\SapiEmitter();
}
return $this->emitter;
}
}