Skip to content

Commit 071246d

Browse files
authored
Make sure independent from ANY PSR7 implementation (#39)
* Make sure independent from ANY PSR7 implementation * cs * Improved testing * Bugfix * Rebase fix
1 parent 01a8f0b commit 071246d

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"require-dev": {
1919
"phpunit/phpunit": "^7.2.7",
2020
"symfony/http-foundation": "^3.4 || ^4.0",
21-
"zendframework/zend-diactoros": "^1.8"
21+
"zendframework/zend-diactoros": "^1.8",
22+
"nyholm/psr7-server": "^0.2",
23+
"http-interop/http-factory-diactoros": "^1.0"
2224
},
2325
"autoload": {
2426
"psr-4": {

src/Http/Request.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
namespace PHPFastCGI\FastCGIDaemon\Http;
44

5+
use Nyholm\Psr7Server\ServerRequestCreatorInterface;
56
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
6-
use function Zend\Diactoros\createUploadedFile;
7-
use Zend\Diactoros\ServerRequest;
8-
use Zend\Diactoros\ServerRequestFactory;
97

108
/**
119
* The default implementation of the RequestInterface.
1210
*/
1311
final class Request implements RequestInterface
1412
{
13+
/**
14+
* @var ServerRequestCreatorInterface|null
15+
*/
16+
private static $serverRequestCreator = null;
17+
1518
/**
1619
* @var int
1720
*/
@@ -56,6 +59,11 @@ public function __construct(array $params, $stdin)
5659
rewind($this->stdin);
5760
}
5861

62+
public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void
63+
{
64+
self::$serverRequestCreator = $serverRequestCreator;
65+
}
66+
5967
/**
6068
* {@inheritdoc}
6169
*/
@@ -229,28 +237,26 @@ public function getStdin()
229237
*/
230238
public function getServerRequest()
231239
{
232-
if (!class_exists(ServerRequest::class)) {
233-
throw new \RuntimeException('You need to install zendframework/zend-diactoros^1.8 to use PSR-7 requests.');
240+
if (null === self::$serverRequestCreator) {
241+
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');
234242
}
235243

244+
$server = $this->params;
236245
$query = $this->getQuery();
237246
$post = $this->getPost();
238247
$cookies = $this->getCookies();
239248

240-
$server = ServerRequestFactory::normalizeServer($this->params);
241-
$headers = ServerRequestFactory::marshalHeaders($server);
242-
$uri = ServerRequestFactory::marshalUriFromServer($server, $headers);
243-
$method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET');
244-
245-
$files = $this->uploadedFiles;
246-
$this->preparePsr7UploadedFiles($files);
249+
return self::$serverRequestCreator->fromArrays(
250+
$server,
251+
self::$serverRequestCreator->getHeadersFromServer($server),
252+
$cookies,
253+
$query,
254+
$post,
255+
$this->uploadedFiles,
256+
$this->stdin
257+
);
247258

248-
$request = new ServerRequest($server, $files, $uri, $method, $this->stdin, $headers);
249259

250-
return $request
251-
->withCookieParams($cookies)
252-
->withQueryParams($query)
253-
->withParsedBody($post);
254260
}
255261

256262
/**
@@ -303,15 +309,4 @@ private function addFile(array &$files, string $fieldName, string $filename, str
303309
}
304310
}
305311
}
306-
307-
private function preparePsr7UploadedFiles(array &$files)
308-
{
309-
if (isset($files['tmp_name'])) {
310-
$files = createUploadedFile($files);
311-
} else {
312-
foreach ($files as &$file) {
313-
$this->preparePsr7UploadedFiles($file);
314-
}
315-
}
316-
}
317312
}

test/Http/RequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace PHPFastCGI\Test\FastCGIDaemon\Http;
44

5+
use Http\Factory\Diactoros\ServerRequestFactory;
6+
use Http\Factory\Diactoros\StreamFactory;
7+
use Http\Factory\Diactoros\UploadedFileFactory;
8+
use Http\Factory\Diactoros\UriFactory;
9+
use Nyholm\Psr7Server\ServerRequestCreator;
510
use PHPFastCGI\FastCGIDaemon\Http\Request;
611
use PHPUnit\Framework\TestCase;
712
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -12,6 +17,17 @@
1217
*/
1318
class RequestTest extends TestCase
1419
{
20+
public static function setUpBeforeClass()
21+
{
22+
parent::setUpBeforeClass();
23+
Request::setServerRequestCreator(new ServerRequestCreator(
24+
new ServerRequestFactory(),
25+
new UriFactory(),
26+
new UploadedFileFactory(),
27+
new StreamFactory()
28+
));
29+
}
30+
1531
/**
1632
* Test that the request builder is correctly building the request messages.
1733
*/

0 commit comments

Comments
 (0)