Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response factory #403

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ multiple concurrent HTTP requests without blocking.
* [withProtocolVersion()](#withprotocolversion)
* [withResponseBuffer()](#withresponsebuffer)
* [React\Http\Message](#reacthttpmessage)
* [ResponseFactory](#responsefactory)
* [Response](#response)
* [ServerRequest](#serverrequest)
* [ResponseException](#responseexception)
Expand Down Expand Up @@ -103,13 +104,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
$loop = React\EventLoop\Factory::create();

$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello World!\n"
);
return React\Http\Message\ResponseFactory::plain("Hello World!\n");
});

$socket = new React\Socket\Server(8080, $loop);
Expand Down Expand Up @@ -719,13 +714,7 @@ object and expects a [response](#server-response) object in return:

```php
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello World!\n"
);
return React\Http\Message\ResponseFactory::plain("Hello World!\n");
});
```

Expand Down Expand Up @@ -2374,6 +2363,19 @@ given setting applied.

### React\Http\Message

#### ResponseFactory

The `React\Http\Message\ResponseFactory` provides a few methods for well known content types. Except `json` all methods
accept both string or [`ReadableStreamInterface`](https://github.com/reactphp/stream#readablestreaminterface) as body.
`json` however, will also do the encoding to JSON for you.

```php
$htmlResponse = React\Http\Message\ResponseFactory::html('<html><body>Hello world!</body></html>');
$jsonResponse = React\Http\Message\ResponseFactory::json(array('message' => array('body' => 'Hello World!')));
$plainResponse = React\Http\Message\ResponseFactory::plain('Hello world!');
$xmlResponse = React\Http\Message\ResponseFactory::xml('<?xml version="1.0" encoding="UTF-8"?><message><body>Hello world!</body></message>');
```

#### Response

The `React\Http\Message\Response` class can be used to
Expand Down
10 changes: 2 additions & 8 deletions examples/51-server-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

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

$loop = Factory::create();

$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello world\n"
);
return ResponseFactory::plain("Hello world\n");
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
10 changes: 2 additions & 8 deletions examples/52-server-count-visitors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -11,13 +11,7 @@

$counter = 0;
$server = new Server($loop, function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Welcome number " . ++$counter . "!\n"
);
return ResponseFactory::plain("Welcome number " . ++$counter . "!\n");
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
10 changes: 2 additions & 8 deletions examples/53-server-whatsmyip.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -12,13 +12,7 @@
$server = new Server($loop, function (ServerRequestInterface $request) {
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];

return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
$body
);
return ResponseFactory::plain($body);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
10 changes: 2 additions & 8 deletions examples/54-server-query-parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -19,13 +19,7 @@
$body = 'The value of "foo" is: ' . htmlspecialchars($queryParams['foo']);
}

return new Response(
200,
array(
'Content-Type' => 'text/html'
),
$body
);
return ResponseFactory::html($body);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
19 changes: 3 additions & 16 deletions examples/55-server-cookie-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -15,23 +15,10 @@
if (isset($request->getCookieParams()[$key])) {
$body = "Your cookie value is: " . $request->getCookieParams()[$key];

return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
$body
);
return ResponseFactory::plain($body);
}

return new Response(
200,
array(
'Content-Type' => 'text/plain',
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
),
"Your cookie has been set."
);
return ResponseFactory::plain('Your cookie has been set.')->withAddedHeader('Set-Cookie', urlencode($key) . '=' . urlencode('test;more'));
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
10 changes: 2 additions & 8 deletions examples/56-server-sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;
use React\Promise\Promise;

Expand All @@ -13,13 +13,7 @@
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
return new Promise(function ($resolve, $reject) use ($loop) {
$loop->addTimer(1.5, function() use ($resolve) {
$response = new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello world"
);
$response = ResponseFactory::plain('Hello world');
$resolve($response);
});
});
Expand Down
10 changes: 2 additions & 8 deletions examples/57-server-error-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;
use React\Promise\Promise;

Expand All @@ -19,13 +19,7 @@
throw new Exception('Second call');
}

$response = new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello World!\n"
);
$response = ResponseFactory::plain("Hello World!\n");

$resolve($response);
});
Expand Down
9 changes: 2 additions & 7 deletions examples/58-server-stream-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;
use React\Stream\ThroughStream;

Expand Down Expand Up @@ -32,13 +33,7 @@
$loop->cancelTimer($timer);
});

return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
$stream
);
return ResponseFactory::plain($stream);
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
40 changes: 11 additions & 29 deletions examples/59-server-json-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -17,43 +17,25 @@

$server = new Server($loop, function (ServerRequestInterface $request) {
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
return new Response(
415, // Unsupported Media Type
array(
'Content-Type' => 'application/json'
),
json_encode(array('error' => 'Only supports application/json')) . "\n"
);
return ResponseFactory::json(
array('error' => 'Only supports application/json')
)->withStatus(415); // Unsupported Media Type
}

$input = json_decode($request->getBody()->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
return new Response(
400, // Bad Request
array(
'Content-Type' => 'application/json'
),
json_encode(array('error' => 'Invalid JSON data given')) . "\n"
);
return ResponseFactory::json(
array('error' => 'Invalid JSON data given')
)->withStatus(400); // Bad Request
}

if (!isset($input->name) || !is_string($input->name)) {
return new Response(
422, // Unprocessable Entity
array(
'Content-Type' => 'application/json'
),
json_encode(array('error' => 'JSON data does not contain a string "name" property')) . "\n"
);
return ResponseFactory::json(
array('error' => 'JSON data does not contain a string "name" property')
)->withStatus(422); // Unprocessable Entity
}

return new Response(
200,
array(
'Content-Type' => 'application/json'
),
json_encode(array('message' => 'Hello ' . $input->name)) . "\n"
);
return ResponseFactory::json(array('message' => 'Hello ' . $input->name));
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
10 changes: 2 additions & 8 deletions examples/61-server-hello-world-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Message\ResponseFactory;
use React\Http\Server;

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

$loop = Factory::create();

$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello world!\n"
);
return ResponseFactory::plain("Hello world\n");
});

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
Expand Down
17 changes: 3 additions & 14 deletions examples/63-server-streaming-request.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use React\EventLoop\Factory;
use React\Http\Message\ResponseFactory;

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

Expand All @@ -24,24 +25,12 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
});

$body->on('end', function () use ($resolve, &$bytes){
$resolve(new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Received $bytes bytes\n"
));
$resolve(ResponseFactory::plain("Received $bytes bytes\n"));
});

// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
$resolve(new React\Http\Message\Response(
400,
array(
'Content-Type' => 'text/plain'
),
"Encountered error after $bytes bytes: {$exception->getMessage()}\n"
));
$resolve(ResponseFactory::plain("Encountered error after $bytes bytes: {$exception->getMessage()}\n")->withStatus(400));
});
});
}
Expand Down
Loading