Skip to content
Merged
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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
51 changes: 23 additions & 28 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace PHPFastCGI\FastCGIDaemon\Http;

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
*/
Expand Down Expand Up @@ -56,6 +59,11 @@ public function __construct(array $params, $stdin)
rewind($this->stdin);
}

public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void
{
self::$serverRequestCreator = $serverRequestCreator;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -229,28 +237,26 @@ 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();

$server = ServerRequestFactory::normalizeServer($this->params);
$headers = ServerRequestFactory::marshalHeaders($server);
$uri = ServerRequestFactory::marshalUriFromServer($server, $headers);
$method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET');

$files = $this->uploadedFiles;
$this->preparePsr7UploadedFiles($files);
return self::$serverRequestCreator->fromArrays(
$server,
self::$serverRequestCreator->getHeadersFromServer($server),
$cookies,
$query,
$post,
$this->uploadedFiles,
$this->stdin
);

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

return $request
->withCookieParams($cookies)
->withQueryParams($query)
->withParsedBody($post);
}

/**
Expand Down Expand Up @@ -303,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);
}
}
}
}
16 changes: 16 additions & 0 deletions test/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down