Skip to content

Commit

Permalink
Make sure independent from ANY PSR7 implementation (#39)
Browse files Browse the repository at this point in the history
* Make sure independent from ANY PSR7 implementation

* cs

* Improved testing

* Bugfix

* Rebase fix
  • Loading branch information
Nyholm committed Aug 6, 2018
1 parent 01a8f0b commit 071246d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
4 changes: 3 additions & 1 deletion composer.json
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
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
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

0 comments on commit 071246d

Please sign in to comment.