-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b492d7c
commit f1fe4cc
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Benzine\Middleware; | ||
|
||
use Ergebnis\Json\Json; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Slim\Psr7\Response; | ||
|
||
class JsonResponse implements ResponseInterface | ||
{ | ||
protected Response $response; | ||
public function __construct(Response $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
public function getJson() : Json | ||
{ | ||
$this->getBody()->rewind(); | ||
$json = Json::fromString($this->getBody()->getContents()); | ||
$this->getBody()->rewind(); | ||
return $json; | ||
} | ||
public function setJson(Json $json) : self | ||
{ | ||
$this->getBody()->rewind(); | ||
$this->getBody()->write($json->toString()); | ||
$this->getBody()->rewind(); | ||
return $this; | ||
} | ||
|
||
public function getProtocolVersion() | ||
{ | ||
return $this->response->getProtocolVersion(); | ||
} | ||
|
||
public function withProtocolVersion(string $version) | ||
{ | ||
return $this->response->withProtocolVersion($version); | ||
} | ||
|
||
public function getHeaders() | ||
{ | ||
return $this->response->getHeaders(); | ||
} | ||
|
||
public function hasHeader(string $name) | ||
{ | ||
return $this->response->hasHeader($name); | ||
} | ||
|
||
public function getHeader(string $name) | ||
{ | ||
return $this->response->getHeader($name); | ||
} | ||
|
||
public function getHeaderLine(string $name) | ||
{ | ||
return $this->response->getHeaderLine($name); | ||
} | ||
|
||
public function withHeader(string $name, $value) | ||
{ | ||
return $this->response->withHeader($name, $value); | ||
} | ||
|
||
public function withAddedHeader(string $name, $value) | ||
{ | ||
return $this->response->withAddedHeader($name, $value); | ||
} | ||
|
||
public function withoutHeader(string $name) | ||
{ | ||
return $this->response->withoutHeader($name); | ||
} | ||
|
||
public function getBody() | ||
{ | ||
return $this->response->getBody(); | ||
} | ||
|
||
public function withBody(StreamInterface $body) | ||
{ | ||
return $this->response->withBody($body); | ||
} | ||
|
||
public function getStatusCode() | ||
{ | ||
return $this->response->getStatusCode(); | ||
} | ||
|
||
public function withStatus(int $code, string $reasonPhrase = '') | ||
{ | ||
return $this->response->withStatus($code, $reasonPhrase); | ||
} | ||
|
||
public function getReasonPhrase() | ||
{ | ||
return $this->response->getReasonPhrase(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Benzine\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Slim\Psr7\Response; | ||
|
||
class JsonResponseUnpackerMiddleware implements MiddlewareInterface | ||
{ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = $handler->handle($request); | ||
|
||
if(!($response->hasHeader("Content-Type") && $response->getHeader("Content-Type")[0] === "application/json") ) { | ||
return $response; | ||
} | ||
return new JsonResponse($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters