diff --git a/src/Bridge/Psr7/RequestFactory.php b/src/Bridge/Psr7/RequestFactory.php index 53e324c3a..003116be0 100644 --- a/src/Bridge/Psr7/RequestFactory.php +++ b/src/Bridge/Psr7/RequestFactory.php @@ -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. @@ -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; }