2020-03-11 23:04:01 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2020-03-12 11:45:11 -04:00
|
|
|
* Hummingbird Anime List Client
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-12-10 17:06:50 -05:00
|
|
|
* PHP version 7.4+
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* @package HummingbirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-12 11:45:11 -04:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2020-03-11 23:04:01 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2020-03-12 11:45:11 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion\View;
|
|
|
|
|
2020-07-31 16:22:32 -04:00
|
|
|
use Aviat\Ion\ViewInterface;
|
2020-03-12 09:52:45 -04:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
use Aviat\Ion\Exception\DoubleRenderException;
|
2020-07-31 16:22:32 -04:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base view class for Http output
|
|
|
|
*/
|
2020-07-31 16:22:32 -04:00
|
|
|
class HttpView implements ViewInterface{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP response Object
|
|
|
|
*
|
|
|
|
* @var ResponseInterface
|
|
|
|
*/
|
|
|
|
public ResponseInterface $response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the view has sent output via
|
|
|
|
* __toString or send method
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected bool $hasRendered = FALSE;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Response mime type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-21 19:22:56 -04:00
|
|
|
protected string $contentType = '';
|
2020-03-11 23:04:01 -04:00
|
|
|
|
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
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
if ($this->hasRendered)
|
|
|
|
{
|
|
|
|
throw new DoubleRenderException();
|
|
|
|
}
|
|
|
|
$this->hasRendered = TRUE;
|
|
|
|
return $this->getOutput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an http header
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string|string[] $value
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
|
|
|
public function addHeader(string $name, $value): self
|
|
|
|
{
|
|
|
|
$this->response = $this->response->withHeader($name, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the output string
|
|
|
|
*
|
|
|
|
* @param mixed $string
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
|
|
|
public function setOutput($string): self
|
|
|
|
{
|
|
|
|
$this->response->getBody()->write($string);
|
|
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append additional output.
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
|
|
|
public function appendOutput(string $string): self
|
|
|
|
{
|
|
|
|
return $this->setOutput($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current output as a string. Does not
|
|
|
|
* render view or send headers.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOutput(): string
|
|
|
|
{
|
|
|
|
return (string)$this->response->getBody();
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Do a redirect
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param int $code
|
|
|
|
* @param array $headers
|
|
|
|
* @throws \InvalidArgumentException
|
2020-08-05 21:46:14 -04:00
|
|
|
* @return self
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2020-08-05 21:46:14 -04:00
|
|
|
public function redirect(string $url, int $code = 302, array $headers = []): self
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$this->response = new Response\RedirectResponse($url, $code, $headers);
|
2020-08-26 15:25:31 -04:00
|
|
|
return $this;
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status code of the request
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
2020-07-31 16:22:32 -04:00
|
|
|
public function setStatusCode(int $code): self
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function send(): void
|
|
|
|
{
|
|
|
|
$this->output();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the appropriate response
|
|
|
|
*
|
2020-12-10 15:59:37 -05:00
|
|
|
* @codeCoverageIgnore
|
2020-03-11 23:04:01 -04:00
|
|
|
* @throws DoubleRenderException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|