Skip to content

Commit 0b4ed9f

Browse files
authored
Merge pull request #439 from clue-labs/response-types
Add factory methods for common HTML/JSON/plaintext/XML response types
2 parents 6acf1a7 + a49404c commit 0b4ed9f

17 files changed

+566
-270
lines changed

README.md

Lines changed: 220 additions & 101 deletions
Large diffs are not rendered by default.

examples/51-server-hello-world.php

Lines changed: 3 additions & 10 deletions
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

Lines changed: 2 additions & 9 deletions
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

Lines changed: 3 additions & 10 deletions
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

Lines changed: 2 additions & 9 deletions
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

Lines changed: 6 additions & 18 deletions
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');

examples/56-server-sleep.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
43
use React\EventLoop\Loop;
5-
use React\Http\Message\Response;
6-
use React\Promise\Promise;
74

85
require __DIR__ . '/../vendor/autoload.php';
96

10-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
11-
return new Promise(function ($resolve, $reject) {
7+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
8+
$promise = new React\Promise\Promise(function ($resolve, $reject) {
129
Loop::addTimer(1.5, function() use ($resolve) {
13-
$response = new Response(
14-
Response::STATUS_OK,
15-
array(
16-
'Content-Type' => 'text/plain'
17-
),
18-
"Hello world"
19-
);
20-
$resolve($response);
10+
$resolve();
2111
});
2212
});
13+
14+
return $promise->then(function () {
15+
return React\Http\Message\Response::plaintext(
16+
"Hello world!\n"
17+
);
18+
});
2319
});
2420

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

examples/57-server-error-handling.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
4-
use React\Http\Message\Response;
5-
use React\Promise\Promise;
6-
73
require __DIR__ . '/../vendor/autoload.php';
84

95
$count = 0;
10-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$count) {
11-
return new Promise(function ($resolve, $reject) use (&$count) {
12-
$count++;
13-
14-
if ($count%2 === 0) {
15-
throw new Exception('Second call');
16-
}
6+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) use (&$count) {
7+
$count++;
178

18-
$response = new Response(
19-
Response::STATUS_OK,
20-
array(
21-
'Content-Type' => 'text/plain'
22-
),
23-
"Hello World!\n"
24-
);
9+
if ($count % 2 === 0) {
10+
throw new Exception('Second call');
11+
}
2512

26-
$resolve($response);
27-
});
13+
return React\Http\Message\Response::plaintext(
14+
"Hello World!\n"
15+
);
2816
});
2917

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

examples/58-server-stream-response.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22

3-
use Psr\Http\Message\ServerRequestInterface;
43
use React\EventLoop\Loop;
54
use React\Http\Message\Response;
65
use React\Stream\ThroughStream;
76

87
require __DIR__ . '/../vendor/autoload.php';
98

10-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
9+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1110
if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') {
1211
return new Response(Response::STATUS_NOT_FOUND);
1312
}

examples/59-server-json-api.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,32 @@
66
// $ php examples/59-server-json-api.php 8080
77
// $ curl -v http://localhost:8080/ -H 'Content-Type: application/json' -d '{"name":"Alice"}'
88

9-
use Psr\Http\Message\ServerRequestInterface;
109
use React\Http\Message\Response;
1110

1211
require __DIR__ . '/../vendor/autoload.php';
1312

14-
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
13+
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1514
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
16-
return new Response(
17-
Response::STATUS_UNSUPPORTED_MEDIA_TYPE,
18-
array(
19-
'Content-Type' => 'application/json'
20-
),
21-
json_encode(array('error' => 'Only supports application/json')) . "\n"
22-
);
15+
return Response::json(
16+
array('error' => 'Only supports application/json')
17+
)->withStatus(Response::STATUS_UNSUPPORTED_MEDIA_TYPE);
2318
}
2419

2520
$input = json_decode($request->getBody()->getContents());
2621
if (json_last_error() !== JSON_ERROR_NONE) {
27-
return new Response(
28-
Response::STATUS_BAD_REQUEST,
29-
array(
30-
'Content-Type' => 'application/json'
31-
),
32-
json_encode(array('error' => 'Invalid JSON data given')) . "\n"
33-
);
22+
return Response::json(
23+
array('error' => 'Invalid JSON data given')
24+
)->withStatus(Response::STATUS_BAD_REQUEST);
3425
}
3526

3627
if (!isset($input->name) || !is_string($input->name)) {
37-
return new Response(
38-
Response::STATUS_UNPROCESSABLE_ENTITY,
39-
array(
40-
'Content-Type' => 'application/json'
41-
),
42-
json_encode(array('error' => 'JSON data does not contain a string "name" property')) . "\n"
43-
);
28+
return Response::json(
29+
array('error' => 'JSON data does not contain a string "name" property')
30+
)->withStatus(Response::STATUS_UNPROCESSABLE_ENTITY);
4431
}
4532

46-
return new Response(
47-
Response::STATUS_OK,
48-
array(
49-
'Content-Type' => 'application/json'
50-
),
51-
json_encode(array('message' => 'Hello ' . $input->name)) . "\n"
33+
return Response::json(
34+
array('message' => 'Hello ' . $input->name)
5235
);
5336
});
5437

0 commit comments

Comments
 (0)