Skip to content

Commit

Permalink
try to parse body stream before passing it to the CommandRequest, bec…
Browse files Browse the repository at this point in the history
…ause we all can only read once from php input stream.
  • Loading branch information
Carpenter0100 committed Aug 24, 2020
1 parent 0f156fc commit e34c687
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/Command/CommandRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Simovative\Zeus\Stream\StreamInterface;
use Throwable;

use function is_string;
use function json_decode;

/**
Expand All @@ -19,13 +18,13 @@ class CommandRequest {
private $valueMap;

/**
* @var null|array|StreamInterface
* @var null|array|object
*/
private $body;

/**
* @param array|mixed[] $valueMap
* @param null|array|StreamInterface $body
* @param null|array|object $body
* @author Benedikt Schaller
*/
public function __construct(array $valueMap, $body) {
Expand All @@ -42,15 +41,8 @@ public static function fromHttpRequest(HttpRequestInterface $httpRequest) {
$body = $httpRequest->getParsedBody();

if ($body instanceof StreamInterface) {
$body = $body->getContents();
}

if (is_string($body)) {
try {
$body = json_decode($body);
} catch (Throwable $exception) {
// continue
}
$body = clone $body;
$body = json_decode($body->getContents());
}

return new CommandRequest($httpRequest->all(), $body);
Expand Down Expand Up @@ -88,7 +80,7 @@ public function all() {
}

/**
* @return null|array|StreamInterface
* @return null|array|object
*/
public function getBody() {
return $this->body;
Expand Down

0 comments on commit e34c687

Please sign in to comment.