Skip to content

Commit a49404c

Browse files
committed
Update documentation and examples to use new response factory methods
1 parent 6ca832f commit a49404c

15 files changed

+99
-270
lines changed

README.md

+39-101
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
112112
require __DIR__ . '/vendor/autoload.php';
113113

114114
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
115-
return new React\Http\Message\Response(
116-
React\Http\Message\Response::STATUS_OK,
117-
array(
118-
'Content-Type' => 'text/plain'
119-
),
115+
return React\Http\Message\Response::plaintext(
120116
"Hello World!\n"
121117
);
122118
});
@@ -738,11 +734,7 @@ object and expects a [response](#server-response) object in return:
738734

739735
```php
740736
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
741-
return new React\Http\Message\Response(
742-
React\Http\Message\Response::STATUS_OK,
743-
array(
744-
'Content-Type' => 'text/plain'
745-
),
737+
return React\Http\Message\Response::plaintext(
746738
"Hello World!\n"
747739
);
748740
});
@@ -953,14 +945,10 @@ and will be passed to the callback function like this.
953945

954946
```php
955947
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
956-
$body = "The method of the request is: " . $request->getMethod();
957-
$body .= "The requested path is: " . $request->getUri()->getPath();
948+
$body = "The method of the request is: " . $request->getMethod() . "\n";
949+
$body .= "The requested path is: " . $request->getUri()->getPath() . "\n";
958950

959-
return new React\Http\Message\Response(
960-
React\Http\Message\Response::STATUS_OK,
961-
array(
962-
'Content-Type' => 'text/plain'
963-
),
951+
return React\Http\Message\Response::plaintext(
964952
$body
965953
);
966954
});
@@ -996,13 +984,9 @@ The following parameters are currently available:
996984

997985
```php
998986
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
999-
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
987+
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'] . "\n";
1000988

1001-
return new React\Http\Message\Response(
1002-
React\Http\Message\Response::STATUS_OK,
1003-
array(
1004-
'Content-Type' => 'text/plain'
1005-
),
989+
return React\Http\Message\Response::plaintext(
1006990
$body
1007991
);
1008992
});
@@ -1030,11 +1014,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
10301014
$body = 'The value of "foo" is: ' . htmlspecialchars($queryParams['foo']);
10311015
}
10321016

1033-
return new React\Http\Message\Response(
1034-
React\Http\Message\Response::STATUS_OK,
1035-
array(
1036-
'Content-Type' => 'text/html'
1037-
),
1017+
return React\Http\Message\Response::html(
10381018
$body
10391019
);
10401020
});
@@ -1077,9 +1057,7 @@ request headers (commonly used for `POST` requests for HTML form submission data
10771057
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
10781058
$name = $request->getParsedBody()['name'] ?? 'anonymous';
10791059

1080-
return new React\Http\Message\Response(
1081-
React\Http\Message\Response::STATUS_OK,
1082-
array(),
1060+
return React\Http\Message\Response::plaintext(
10831061
"Hello $name!\n"
10841062
);
10851063
});
@@ -1102,10 +1080,8 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
11021080
$data = json_decode((string)$request->getBody());
11031081
$name = $data->name ?? 'anonymous';
11041082

1105-
return new React\Http\Message\Response(
1106-
React\Http\Message\Response::STATUS_OK,
1107-
array('Content-Type' => 'application/json'),
1108-
json_encode(['message' => "Hello $name!"])
1083+
return React\Http\Message\Response::json(
1084+
['message' => "Hello $name!"]
11091085
);
11101086
});
11111087
```
@@ -1125,9 +1101,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
11251101
$files = $request->getUploadedFiles();
11261102
$name = isset($files['avatar']) ? $files['avatar']->getClientFilename() : 'nothing';
11271103

1128-
return new React\Http\Message\Response(
1129-
React\Http\Message\Response::STATUS_OK,
1130-
array(),
1104+
return React\Http\Message\Response::plaintext(
11311105
"Uploaded $name\n"
11321106
);
11331107
});
@@ -1208,24 +1182,16 @@ $http = new React\Http\HttpServer(
12081182
});
12091183

12101184
$body->on('end', function () use ($resolve, &$bytes){
1211-
$resolve(new React\Http\Message\Response(
1212-
React\Http\Message\Response::STATUS_OK,
1213-
array(
1214-
'Content-Type' => 'text/plain'
1215-
),
1185+
$resolve(React\Http\Message\Response::plaintext(
12161186
"Received $bytes bytes\n"
12171187
));
12181188
});
12191189

12201190
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
12211191
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
1222-
$resolve(new React\Http\Message\Response(
1223-
React\Http\Message\Response::STATUS_BAD_REQUEST,
1224-
array(
1225-
'Content-Type' => 'text/plain'
1226-
),
1192+
$resolve(React\Http\Message\Response::plaintext(
12271193
"Encountered error after $bytes bytes: {$e->getMessage()}\n"
1228-
));
1194+
)->withStatus(React\Http\Message\Response::STATUS_BAD_REQUEST));
12291195
});
12301196
});
12311197
}
@@ -1272,23 +1238,15 @@ $http = new React\Http\HttpServer(
12721238
function (Psr\Http\Message\ServerRequestInterface $request) {
12731239
$size = $request->getBody()->getSize();
12741240
if ($size === null) {
1275-
$body = 'The request does not contain an explicit length.';
1276-
$body .= 'This example does not accept chunked transfer encoding.';
1277-
1278-
return new React\Http\Message\Response(
1279-
React\Http\Message\Response::STATUS_LENGTH_REQUIRED,
1280-
array(
1281-
'Content-Type' => 'text/plain'
1282-
),
1241+
$body = "The request does not contain an explicit length. ";
1242+
$body .= "This example does not accept chunked transfer encoding.\n";
1243+
1244+
return React\Http\Message\Response::plaintext(
12831245
$body
1284-
);
1246+
)->withStatus(React\Http\Message\Response::STATUS_LENGTH_REQUIRED);
12851247
}
12861248

1287-
return new React\Http\Message\Response(
1288-
React\Http\Message\Response::STATUS_OK,
1289-
array(
1290-
'Content-Type' => 'text/plain'
1291-
),
1249+
return React\Http\Message\Response::plaintext(
12921250
"Request body size: " . $size . " bytes\n"
12931251
);
12941252
}
@@ -1344,25 +1302,16 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
13441302
$key = 'react\php';
13451303

13461304
if (isset($request->getCookieParams()[$key])) {
1347-
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
1305+
$body = "Your cookie value is: " . $request->getCookieParams()[$key] . "\n";
13481306

1349-
return new React\Http\Message\Response(
1350-
React\Http\Message\Response::STATUS_OK,
1351-
array(
1352-
'Content-Type' => 'text/plain'
1353-
),
1307+
return React\Http\Message\Response::plaintext(
13541308
$body
13551309
);
13561310
}
13571311

1358-
return new React\Http\Message\Response(
1359-
React\Http\Message\Response::STATUS_OK,
1360-
array(
1361-
'Content-Type' => 'text/plain',
1362-
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
1363-
),
1364-
"Your cookie has been set."
1365-
);
1312+
return React\Http\Message\Response::plaintext(
1313+
"Your cookie has been set.\n"
1314+
)->withHeader('Set-Cookie', urlencode($key) . '=' . urlencode('test;more'));
13661315
});
13671316
```
13681317

@@ -1413,11 +1362,7 @@ In its most simple form, you can use it like this:
14131362

14141363
```php
14151364
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1416-
return new React\Http\Message\Response(
1417-
React\Http\Message\Response::STATUS_OK,
1418-
array(
1419-
'Content-Type' => 'text/plain'
1420-
),
1365+
return React\Http\Message\Response::plaintext(
14211366
"Hello World!\n"
14221367
);
14231368
});
@@ -1441,18 +1386,17 @@ This example shows how such a long-term action could look like:
14411386

14421387
```php
14431388
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1444-
return new Promise(function ($resolve, $reject) {
1389+
$promise = new Promise(function ($resolve, $reject) {
14451390
Loop::addTimer(1.5, function() use ($resolve) {
1446-
$response = new React\Http\Message\Response(
1447-
React\Http\Message\Response::STATUS_OK,
1448-
array(
1449-
'Content-Type' => 'text/plain'
1450-
),
1451-
"Hello world"
1452-
);
1453-
$resolve($response);
1391+
$resolve();
14541392
});
14551393
});
1394+
1395+
return $promise->then(function () {
1396+
return React\Http\Message\Response::plaintext(
1397+
"Hello World!"
1398+
);
1399+
});
14561400
});
14571401
```
14581402

@@ -1571,11 +1515,7 @@ a `string` response body like this:
15711515

15721516
```php
15731517
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1574-
return new React\Http\Message\Response(
1575-
React\Http\Message\Response::STATUS_OK,
1576-
array(
1577-
'Content-Type' => 'text/plain'
1578-
),
1518+
return React\Http\Message\Response::plaintext(
15791519
"Hello World!\n"
15801520
);
15811521
});
@@ -1845,11 +1785,9 @@ $http = new React\Http\HttpServer(
18451785
$resolve($next($request));
18461786
});
18471787
return $promise->then(null, function (Exception $e) {
1848-
return new React\Http\Message\Response(
1849-
React\Http\Message\Response::STATUS_INTERNAL_SERVER_ERROR,
1850-
array(),
1851-
'Internal error: ' . $e->getMessage()
1852-
);
1788+
return React\Http\Message\Response::plaintext(
1789+
'Internal error: ' . $e->getMessage() . "\n"
1790+
)->withStatus(React\Http\Message\Response::STATUS_INTERNAL_SERVER_ERROR);
18531791
});
18541792
},
18551793
function (Psr\Http\Message\ServerRequestInterface $request) {

examples/51-server-hello-world.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

8-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
9-
return new Response(
10-
Response::STATUS_OK,
11-
array(
12-
'Content-Type' => 'text/plain'
13-
),
14-
"Hello world\n"
5+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
6+
return React\Http\Message\Response::plaintext(
7+
"Hello World!\n"
158
);
169
});
1710

examples/52-server-count-visitors.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

85
$counter = 0;
9-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) {
10-
return new Response(
11-
Response::STATUS_OK,
12-
array(
13-
'Content-Type' => 'text/plain'
14-
),
6+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) use (&$counter) {
7+
return React\Http\Message\Response::plaintext(
158
"Welcome number " . ++$counter . "!\n"
169
);
1710
});

examples/53-server-whatsmyip.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

8-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
9-
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
5+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
6+
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'] . "\n";
107

11-
return new Response(
12-
Response::STATUS_OK,
13-
array(
14-
'Content-Type' => 'text/plain'
15-
),
8+
return React\Http\Message\Response::plaintext(
169
$body
1710
);
1811
});

examples/54-server-query-parameter.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

8-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
5+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
96
$queryParams = $request->getQueryParams();
107

118
$body = 'The query parameter "foo" is not set. Click the following link ';
@@ -15,11 +12,7 @@
1512
$body = 'The value of "foo" is: ' . htmlspecialchars($queryParams['foo']);
1613
}
1714

18-
return new Response(
19-
Response::STATUS_OK,
20-
array(
21-
'Content-Type' => 'text/html'
22-
),
15+
return React\Http\Message\Response::html(
2316
$body
2417
);
2518
});

examples/55-server-cookie-handling.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
63
require __DIR__ . '/../vendor/autoload.php';
74

8-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
5+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
96
$key = 'react\php';
107

118
if (isset($request->getCookieParams()[$key])) {
12-
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
9+
$body = "Your cookie value is: " . $request->getCookieParams()[$key] . "\n";
1310

14-
return new Response(
15-
Response::STATUS_OK,
16-
array(
17-
'Content-Type' => 'text/plain'
18-
),
11+
return React\Http\Message\Response::plaintext(
1912
$body
2013
);
2114
}
2215

23-
return new Response(
24-
Response::STATUS_OK,
25-
array(
26-
'Content-Type' => 'text/plain',
27-
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
28-
),
29-
"Your cookie has been set."
30-
);
16+
return React\Http\Message\Response::plaintext(
17+
"Your cookie has been set.\n"
18+
)->withHeader('Set-Cookie', urlencode($key) . '=' . urlencode('test;more'));
3119
});
3220

3321
$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0');

0 commit comments

Comments
 (0)