Skip to content

Commit

Permalink
Replace using Slim's request by Zend Diactoros to minimize the number…
Browse files Browse the repository at this point in the history
… of dependencies
  • Loading branch information
mnapoli committed Dec 10, 2017
1 parent 97241c7 commit 301c138
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions src/Bridge/Psr7/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace PhpLambda\Bridge\Psr7;

use Slim\Http\Environment;
use Slim\Http\Request;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\ServerRequest;

/**
* Create a PSR-7 request from a lambda event.
Expand All @@ -15,33 +15,52 @@
*/
class RequestFactory
{
public function fromLambdaEvent(array $event) : Request
public function fromLambdaEvent(array $event) : ServerRequestInterface
{
$method = $event['httpMethod'] ?? 'GET';
$query = $event['queryStringParameters'] ?? '';
$query = $event['queryStringParameters'] ?? [];
parse_str($event['body'] ?? '', $request);
$files = [];
$uri = $event['requestContext']['path'] ?? '/';
$headers = $event['headers'] ?? [];
$protocolVersion = $event['requestContext']['protocol'] ?? '1.1';
// TODO
$cookies = [];

$server = [
'SERVER_PROTOCOL' => $event['requestContext']['protocol'] ?? null,
'SERVER_PROTOCOL' => $protocolVersion,
'REQUEST_METHOD' => $method,
'REQUEST_TIME' => $event['requestContext']['requestTimeEpoch'] ?? time(),
'QUERY_STRING' => $query ? http_build_query($query) : '',
'DOCUMENT_ROOT' => getcwd(),
'REQUEST_URI' => $event['requestContext']['path'] ?? '/',
'REQUEST_URI' => $uri,
];

// Inspired from \Symfony\Component\HttpFoundation\Request::overrideGlobals()
foreach (($event['headers'] ?? []) as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
if (in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$server[$key] = implode(', ', $value);
} else {
$server['HTTP_'.$key] = implode(', ', (array) $value);
}
}

$request = Request::createFromEnvironment(new Environment($server));
$request = $request->withParsedBody($request);
// foreach ($headers as $key => $value) {
// $key = strtoupper(str_replace('-', '_', $key));
// if (in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
// $server[$key] = implode(', ', $value);
// } else {
// $server['HTTP_'.$key] = implode(', ', (array) $value);
// }
// }

// $request = Request::createFromEnvironment(new Environment($server));
// $request = $request->withParsedBody($request);

$request = new ServerRequest(
$server,
$files,
$uri,
$method,
'file:///dev/null',
$headers,
$cookies,
$query,
$request,
$protocolVersion
);

return $request;
}
Expand Down

0 comments on commit 301c138

Please sign in to comment.