From ea2de24d47563040a4eee26c8142f5a9522ec1c8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 29 Jul 2018 20:28:38 -0700 Subject: [PATCH 1/5] Make sure independent from ANY PSR7 implementation --- src/Http/Request.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index ba89c62..0280dbb 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -2,16 +2,20 @@ namespace PHPFastCGI\FastCGIDaemon\Http; +use Interop\Http\Factory\ServerRequestFactoryInterface; +use Nyholm\Psr7Server\ServerRequestCreatorInterface; use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; -use function Zend\Diactoros\createUploadedFile; -use Zend\Diactoros\ServerRequest; -use Zend\Diactoros\ServerRequestFactory; /** * The default implementation of the RequestInterface. */ final class Request implements RequestInterface { + /** + * @var ServerRequestCreatorInterface|null + */ + private static $serverRequestCreator = null; + /** * @var int */ @@ -56,6 +60,11 @@ public function __construct(array $params, $stdin) rewind($this->stdin); } + public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void + { + self::$serverRequestCreator = $serverRequestCreator; + } + /** * {@inheritdoc} */ @@ -229,10 +238,11 @@ public function getStdin() */ public function getServerRequest() { - if (!class_exists(ServerRequest::class)) { - throw new \RuntimeException('You need to install zendframework/zend-diactoros^1.8 to use PSR-7 requests.'); + if (null === self::$serverRequestCreator) { + throw new \RuntimeException('You need to add an object of \Nyholm\Psr7Server\ServerRequestCreatorInterface to \PHPFastCGI\FastCGIDaemon\Http\Request::setServerRequestCreator to use PSR-7 requests. Please install and read more at https://github.com/nyholm/psr7-server'); } + $server = $this->params; $query = $this->getQuery(); $post = $this->getPost(); $cookies = $this->getCookies(); From 3591ff54dd9ed9e6918a246bff1652e15e4ce104 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 29 Jul 2018 20:39:17 -0700 Subject: [PATCH 2/5] cs --- src/Http/Request.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index 0280dbb..fb9f815 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -2,7 +2,6 @@ namespace PHPFastCGI\FastCGIDaemon\Http; -use Interop\Http\Factory\ServerRequestFactoryInterface; use Nyholm\Psr7Server\ServerRequestCreatorInterface; use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; From 9c2375ffc989acaa7e0ed754be7b18ab3f4c0765 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 29 Jul 2018 21:00:43 -0700 Subject: [PATCH 3/5] Improved testing --- composer.json | 4 +++- test/Http/RequestTest.php | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e5f199a..1f76afd 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "require-dev": { "phpunit/phpunit": "^7.2.7", "symfony/http-foundation": "^3.4 || ^4.0", - "zendframework/zend-diactoros": "^1.8" + "zendframework/zend-diactoros": "^1.8", + "nyholm/psr7-server": "^0.2", + "http-interop/http-factory-diactoros": "^1.0" }, "autoload": { "psr-4": { diff --git a/test/Http/RequestTest.php b/test/Http/RequestTest.php index 2f34eb5..7e5e98b 100644 --- a/test/Http/RequestTest.php +++ b/test/Http/RequestTest.php @@ -2,6 +2,11 @@ namespace PHPFastCGI\Test\FastCGIDaemon\Http; +use Http\Factory\Diactoros\ServerRequestFactory; +use Http\Factory\Diactoros\StreamFactory; +use Http\Factory\Diactoros\UploadedFileFactory; +use Http\Factory\Diactoros\UriFactory; +use Nyholm\Psr7Server\ServerRequestCreator; use PHPFastCGI\FastCGIDaemon\Http\Request; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -12,6 +17,17 @@ */ class RequestTest extends TestCase { + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + Request::setServerRequestCreator(new ServerRequestCreator( + new ServerRequestFactory(), + new UriFactory(), + new UploadedFileFactory(), + new StreamFactory() + )); + } + /** * Test that the request builder is correctly building the request messages. */ @@ -53,7 +69,6 @@ public function testRequest() $this->assertEquals($expectedQuery, $serverRequest->getQueryParams()); $this->assertEquals($expectedPost, $serverRequest->getParsedBody()); $this->assertEquals($expectedCookies, $serverRequest->getCookieParams()); - $this->assertEquals($content, (string) $serverRequest->getBody()); // Check the HttpFoundation request rewind($stream); From 9b5c5d7f77cf3d89779109703b0398f7a281ec11 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 29 Jul 2018 21:05:45 -0700 Subject: [PATCH 4/5] Bugfix --- test/Http/RequestTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Http/RequestTest.php b/test/Http/RequestTest.php index 7e5e98b..44f5e72 100644 --- a/test/Http/RequestTest.php +++ b/test/Http/RequestTest.php @@ -69,6 +69,7 @@ public function testRequest() $this->assertEquals($expectedQuery, $serverRequest->getQueryParams()); $this->assertEquals($expectedPost, $serverRequest->getParsedBody()); $this->assertEquals($expectedCookies, $serverRequest->getCookieParams()); + $this->assertEquals($content, (string) $serverRequest->getBody()); // Check the HttpFoundation request rewind($stream); From ae21ab3fdcb34acc560029caa4a592f675041de6 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Wed, 1 Aug 2018 09:36:54 +0200 Subject: [PATCH 5/5] Rebase fix --- src/Http/Request.php | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index fb9f815..06354c4 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -246,20 +246,17 @@ public function getServerRequest() $post = $this->getPost(); $cookies = $this->getCookies(); - $server = ServerRequestFactory::normalizeServer($this->params); - $headers = ServerRequestFactory::marshalHeaders($server); - $uri = ServerRequestFactory::marshalUriFromServer($server, $headers); - $method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET'); + return self::$serverRequestCreator->fromArrays( + $server, + self::$serverRequestCreator->getHeadersFromServer($server), + $cookies, + $query, + $post, + $this->uploadedFiles, + $this->stdin + ); - $files = $this->uploadedFiles; - $this->preparePsr7UploadedFiles($files); - $request = new ServerRequest($server, $files, $uri, $method, $this->stdin, $headers); - - return $request - ->withCookieParams($cookies) - ->withQueryParams($query) - ->withParsedBody($post); } /** @@ -312,15 +309,4 @@ private function addFile(array &$files, string $fieldName, string $filename, str } } } - - private function preparePsr7UploadedFiles(array &$files) - { - if (isset($files['tmp_name'])) { - $files = createUploadedFile($files); - } else { - foreach ($files as &$file) { - $this->preparePsr7UploadedFiles($file); - } - } - } }