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
|
|
|
*
|
2023-07-13 11:08:05 -04:00
|
|
|
* PHP version 8.1
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2023-07-13 11:08:05 -04:00
|
|
|
* @copyright 2015 - 2023 Timothy J. Warren <tim@timshome.page>
|
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
|
2023-07-13 11:08:05 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion\View;
|
|
|
|
|
2023-12-21 13:19:59 -05:00
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
2020-07-31 16:22:32 -04:00
|
|
|
use Laminas\Diactoros\Response\HtmlResponse;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Throwable;
|
2020-04-24 14:14:52 -04:00
|
|
|
use const EXTR_OVERWRITE;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* View class for outputting HTML
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class HtmlView extends HttpView
|
|
|
|
{
|
2020-07-31 16:22:32 -04:00
|
|
|
use ContainerAware;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
2023-12-21 13:19:59 -05:00
|
|
|
/**
|
|
|
|
* Data to send to every template
|
|
|
|
*/
|
|
|
|
protected array $baseData = [];
|
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Response mime type
|
|
|
|
*/
|
2020-04-21 19:22:56 -04:00
|
|
|
protected string $contentType = 'text/html';
|
2020-03-11 23:04:01 -04:00
|
|
|
|
2023-12-21 13:19:59 -05:00
|
|
|
/**
|
|
|
|
* Whether to 'minify' the html output
|
|
|
|
*/
|
|
|
|
protected bool $shouldMinify = false;
|
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Create the Html View
|
|
|
|
*/
|
2023-05-19 16:34:38 -04:00
|
|
|
public function __construct()
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
2020-07-31 16:22:32 -04:00
|
|
|
parent::__construct();
|
|
|
|
|
2023-05-19 16:34:38 -04:00
|
|
|
$this->setContainer(func_get_arg(0));
|
2020-07-31 16:22:32 -04:00
|
|
|
$this->response = new HtmlResponse('');
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2023-12-21 13:19:59 -05:00
|
|
|
/**
|
|
|
|
* Set data to pass to every template
|
|
|
|
*
|
|
|
|
* @param array $data - Keys are variable names
|
|
|
|
*/
|
|
|
|
public function setBaseData(array $data): self
|
|
|
|
{
|
|
|
|
$this->baseData = $data;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should the html be 'minified'?
|
|
|
|
*/
|
|
|
|
public function setMinify(bool $shouldMinify): self
|
|
|
|
{
|
|
|
|
$this->shouldMinify = $shouldMinify;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Render a basic html Template
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws Throwable
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2023-12-21 13:19:59 -05:00
|
|
|
public function renderTemplate(string $path, array $data = []): string
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
2023-12-21 13:19:59 -05:00
|
|
|
$data = array_merge($this->baseData, $data);
|
|
|
|
|
|
|
|
return (function () use ($data, $path) {
|
|
|
|
ob_start();
|
|
|
|
extract($data, EXTR_OVERWRITE);
|
|
|
|
include_once $path;
|
|
|
|
$rawBuffer = ob_get_clean();
|
|
|
|
$buffer = ($rawBuffer === FALSE) ? '' : $rawBuffer;
|
|
|
|
|
|
|
|
// Very basic html minify, that won't affect content between html tags
|
|
|
|
if ($this->shouldMinify)
|
|
|
|
{
|
|
|
|
$buffer = preg_replace('/>\s+</', '> <', $buffer) ?? $buffer;
|
|
|
|
}
|
|
|
|
return $buffer;
|
|
|
|
})();
|
2020-03-11 23:04:01 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of HtmlView.php
|