From 1038a4151eade1d731548b4399a1c6855ad6bf08 Mon Sep 17 00:00:00 2001 From: Aurimas Niekis Date: Thu, 11 Feb 2016 01:28:38 +0100 Subject: [PATCH] Added Thruster\HttpMessage instead Guzzle/Zend PSR-7 Implementation --- composer.json | 3 +- src/RequestParser.php | 19 +-- src/ServerRequest.php | 232 -------------------------------- src/Tests/ServerRequestTest.php | 33 ----- 4 files changed, 7 insertions(+), 280 deletions(-) delete mode 100644 src/ServerRequest.php delete mode 100644 src/Tests/ServerRequestTest.php diff --git a/composer.json b/composer.json index 7de20cd..0846a3c 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ ], "require": { "php": ">=7.0", - "psr/http-message": "~1.0", - "guzzlehttp/psr7": "~1.2", + "thruster/http-message": "~1.0", "thruster/event-emitter": "~1.0" }, "require-dev": { diff --git a/src/RequestParser.php b/src/RequestParser.php index 1af6c12..2f1fadf 100644 --- a/src/RequestParser.php +++ b/src/RequestParser.php @@ -8,7 +8,7 @@ use Thruster\Component\Http\Exception\RequestEntityTooLargeException; use Thruster\Component\Http\Exception\RequestHTTPVersionNotSupported; use Thruster\Component\Http\Exception\RequestURITooLongException; -use Thruster\Component\Stream\StreamInterface; +use Thruster\Component\HttpMessage\ServerRequest; /** * Class RequestParser @@ -33,12 +33,12 @@ class RequestParser implements EventEmitterInterface protected $receivedHead; /** - * @var resource + * @var string */ protected $head; /** - * @var string + * @var resource */ protected $body; @@ -185,20 +185,13 @@ protected function buildRequest() { rewind($this->body); - $request = new ServerRequest( - [], - [], - $this->uri, + return new ServerRequest( $this->httpMethod, - $this->body, + $this->uri, $this->headers, - [], - [], - null, + $this->body, $this->protocolVersion ); - - return $request; } protected function isRequestFinished() : bool diff --git a/src/ServerRequest.php b/src/ServerRequest.php deleted file mode 100644 index 04cc21f..0000000 --- a/src/ServerRequest.php +++ /dev/null @@ -1,232 +0,0 @@ - - */ -class ServerRequest extends Request implements ServerRequestInterface -{ - /** - * @var array|object|string - */ - protected $parsedBody; - - /** - * @var array - */ - protected $serverParams; - - /** - * @var array - */ - protected $attributes; - - /** - * @var array - */ - protected $cookieParams; - - /** - * @var array - */ - protected $queryParams; - - /** - * @var UploadedFileInterface[] - */ - protected $uploadedFiles; - - /** - * @param array $serverParams Server parameters, typically from $_SERVER - * @param array $uploadedFiles Upload file information, a tree of UploadedFiles - * @param string $uri URI for the request, if any. - * @param string $method HTTP method for the request, if any. - * @param string|resource|StreamInterface $body Message body, if any. - * @param array $headers Headers for the message, if any. - * @param array $cookies Cookies for the message, if any. - * @param array $queryParams Query params for the message, if any. - * @param array|object $parsedBody The deserialized body parameters, if any. - * @param string $protocol HTTP protocol version. - * - * @throws InvalidArgumentException for any invalid value. - */ - public function __construct( - array $serverParams = [], - array $uploadedFiles = [], - $uri = null, - string $method = null, - $body = null, - array $headers = [], - array $cookies = [], - array $queryParams = [], - $parsedBody = null, - string $protocolVersion = '1.1' - ) { - $this->validateUploadedFiles($uploadedFiles); - - $this->attributes = []; - - $this->serverParams = $serverParams; - $this->uploadedFiles = $uploadedFiles; - $this->cookieParams = $cookies; - $this->queryParams = $queryParams; - $this->parsedBody = $parsedBody; - - parent::__construct($method, $uri, $headers, $body, $protocolVersion); - } - - /** - * @inheritDoc - */ - public function getServerParams() : array - { - return $this->serverParams; - } - - /** - * @inheritDoc - */ - public function getCookieParams() : array - { - return $this->cookieParams; - } - - /** - * @inheritDoc - */ - public function withCookieParams(array $cookies) - { - $new = clone $this; - $new->cookieParams = $cookies; - - return $new; - } - - /** - * @inheritDoc - */ - public function getQueryParams() : array - { - return $this->queryParams; - } - - /** - * @inheritDoc - */ - public function withQueryParams(array $query) - { - $new = clone $this; - $new->queryParams = $query; - - return $new; - } - - /** - * @inheritDoc - */ - public function getUploadedFiles() : array - { - return $this->uploadedFiles; - } - - /** - * @inheritDoc - */ - public function withUploadedFiles(array $uploadedFiles) - { - $this->validateUploadedFiles($uploadedFiles); - - $new = clone $this; - $new->uploadedFiles = $uploadedFiles; - - return $new; - } - - /** - * @inheritDoc - */ - public function getParsedBody() - { - return $this->parsedBody; - } - - /** - * @inheritDoc - */ - public function withParsedBody($data) - { - $new = clone $this; - $new->parsedBody = $data; - - return $new; - } - - /** - * @inheritDoc - */ - public function getAttributes() : array - { - return $this->attributes; - } - - /** - * @inheritDoc - */ - public function getAttribute($name, $default = null) - { - return $this->attributes[$name] ?? $default; - } - - /** - * @inheritDoc - */ - public function withAttribute($name, $value) - { - $new = clone $this; - $new->attributes[$name] = $value; - - return $new; - } - - /** - * @inheritDoc - */ - public function withoutAttribute($name) - { - $new = clone $this; - unset($new->attributes[$name]); - - return $new; - } - - /** - * Recursively validate the structure in an uploaded files array. - * - * @param array $uploadedFiles - * - * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance. - */ - protected function validateUploadedFiles(array $uploadedFiles) - { - foreach ($uploadedFiles as $file) { - if (is_array($file)) { - $this->validateUploadedFiles($file); - continue; - } - - if (!$file instanceof UploadedFileInterface) { - throw new InvalidArgumentException('Invalid leaf in uploaded files structure'); - } - } - } -} diff --git a/src/Tests/ServerRequestTest.php b/src/Tests/ServerRequestTest.php deleted file mode 100644 index 649bff6..0000000 --- a/src/Tests/ServerRequestTest.php +++ /dev/null @@ -1,33 +0,0 @@ - - */ -class ServerRequestTest extends \PHPUnit_Framework_TestCase -{ - public function testAttributes() - { - $serverRequest = new ServerRequest([], [], '/'); - - $this->assertSame('foobar', $serverRequest->getAttribute('foo', 'foobar')); - $this->assertCount(0, $serverRequest->getAttributes()); - - $serverRequest = $serverRequest->withAttribute('foo', 'bar'); - - $this->assertCount(1, $serverRequest->getAttributes()); - $this->assertEquals(['foo' => 'bar'], $serverRequest->getAttributes()); - $this->assertSame('bar', $serverRequest->getAttribute('foo')); - - $serverRequest = $serverRequest->withoutAttribute('foo'); - - $this->assertCount(0, $serverRequest->getAttributes()); - $this->assertSame('foo', $serverRequest->getAttribute('foo', 'foo')); - } -}