HummingBirdAnimeClient/src/Ion/View/HttpView.php

192 lines
3.7 KiB
PHP
Raw Permalink Normal View History

<?php declare(strict_types=1);
/**
2020-03-12 11:45:11 -04:00
* Hummingbird Anime List Client
*
2020-03-12 11:45:11 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2023-07-13 11:08:05 -04:00
* PHP version 8.1
*
2023-07-13 11:08:05 -04:00
* @copyright 2015 - 2023 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2023-07-13 11:08:05 -04:00
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion\View;
use Aviat\Ion\Exception\DoubleRenderException;
2021-02-11 19:54:22 -05:00
use Aviat\Ion\HttpViewInterface;
use InvalidArgumentException;
2020-03-12 09:52:45 -04:00
use Laminas\Diactoros\Response;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
2020-07-31 16:22:32 -04:00
use Psr\Http\Message\ResponseInterface;
use \Stringable;
/**
* Base view class for Http output
*/
class HttpView implements HttpViewInterface, Stringable
{
2020-07-31 16:22:32 -04:00
/**
* HTTP response Object
*/
public ResponseInterface $response;
/**
* If the view has sent output via
* __toString or send method
*/
protected bool $hasRendered = FALSE;
/**
* Response mime type
*/
2020-04-21 19:22:56 -04:00
protected string $contentType = '';
2020-07-31 16:22:32 -04:00
/**
* Constructor.
*/
public function __construct()
{
$this->response = new Response();
}
/**
* Send output to client
*/
public function __destruct()
{
if ( ! $this->hasRendered)
{
$this->send();
}
}
/**
* Return rendered output as string. Renders the view,
* and any attempts to call again will result in a DoubleRenderException
*
* @throws DoubleRenderException
*/
public function __toString(): string
{
if ($this->hasRendered)
{
throw new DoubleRenderException();
}
2020-07-31 16:22:32 -04:00
$this->hasRendered = TRUE;
2020-07-31 16:22:32 -04:00
return $this->getOutput();
}
2023-03-16 13:03:48 -04:00
/**
* Alternate static constructor
*/
public static function new(): static
{
return new static();
}
2020-07-31 16:22:32 -04:00
/**
* Add an http header
*
* @param string|string[] $value
*/
2022-01-07 12:33:01 -05:00
public function addHeader(string $name, array|string $value): self
2020-07-31 16:22:32 -04:00
{
$this->response = $this->response->withHeader($name, $value);
2020-07-31 16:22:32 -04:00
return $this;
}
/**
* Set the output string
*/
2022-01-07 12:33:01 -05:00
public function setOutput(mixed $string): HttpViewInterface
2020-07-31 16:22:32 -04:00
{
$this->response->getBody()->write($string);
return $this;
}
/**
* Append additional output.
*/
2021-02-11 19:54:22 -05:00
public function appendOutput(string $string): HttpViewInterface
2020-07-31 16:22:32 -04:00
{
return $this->setOutput($string);
}
/**
* Get the current output as a string. Does not
* render view or send headers.
*/
public function getOutput(): string
{
return (string) $this->response->getBody();
2020-07-31 16:22:32 -04:00
}
/**
* Do a redirect
*
* @throws InvalidArgumentException
*/
public function redirect(string $url, int $code = 302, array $headers = []): self
{
$this->response = new Response\RedirectResponse($url, $code, $headers);
2020-08-26 15:25:31 -04:00
return $this;
}
/**
* Set the status code of the request
*
* @throws InvalidArgumentException
*/
2020-07-31 16:22:32 -04:00
public function setStatusCode(int $code): self
{
$this->response = $this->response->withStatus($code)
->withProtocolVersion('1.1');
return $this;
}
/**
* Send output to client. As it renders the view,
* any attempt to call again will result in a DoubleRenderException.
*
* @throws DoubleRenderException
* @throws InvalidArgumentException
*/
public function send(): void
{
$this->output();
}
/**
* Send the appropriate response
*
* @throws DoubleRenderException
* @throws InvalidArgumentException
*/
protected function output(): void
{
if ($this->hasRendered)
{
throw new DoubleRenderException();
}
$this->response = $this->response
->withHeader('Content-type', "{$this->contentType};charset=utf-8")
->withHeader('X-Content-Type-Options', 'nosniff')
->withHeader('X-XSS-Protection', '1;mode=block')
->withHeader('X-Frame-Options', 'SAMEORIGIN');
(new SapiEmitter())->emit($this->response);
$this->hasRendered = TRUE;
}
}