Replace IRespondToRequests with PSR-7 ResponseInterface

This commit is contained in:
Philipp 2021-11-21 23:37:17 +01:00
parent ca5c40c97e
commit 7cd85873ee
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
17 changed files with 96 additions and 92 deletions

View file

@ -3,8 +3,8 @@
namespace Friendica\Module;
use Friendica\Capabilities\ICanCreateResponses;
use Friendica\Capabilities\IRespondToRequests;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Psr\Http\Message\ResponseInterface;
class Response implements ICanCreateResponses
{
@ -19,7 +19,7 @@ class Response implements ICanCreateResponses
/**
* @var string
*/
protected $type = IRespondToRequests::TYPE_HTML;
protected $type = ICanCreateResponses::TYPE_HTML;
/**
* {@inheritDoc}
@ -68,7 +68,7 @@ class Response implements ICanCreateResponses
*/
public function setType(string $type, ?string $content_type = null): void
{
if (!in_array($type, IRespondToRequests::ALLOWED_TYPES)) {
if (!in_array($type, ICanCreateResponses::ALLOWED_TYPES)) {
throw new InternalServerErrorException('wrong type');
}
@ -94,4 +94,25 @@ class Response implements ICanCreateResponses
{
return $this->type;
}
/**
* {@inheritDoc}
*/
public function generate(): ResponseInterface
{
$headers = [];
foreach ($this->headers as $key => $header) {
if (empty($key)) {
$headers[] = $header;
} else {
$headers[] = "$key: $header";
}
}
// Setting the response type as an X-header for direct usage
$headers['X-RESPONSE-TYPE'] = $this->type;
return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
}
}